Repository: rust-lang/rust
File: compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs (Rust)

Code before:
            }
            _ => expr,
        };

        let lit_input = match expr.kind {
            hir::ExprKind::Lit(lit) => Some(LitToConstInput { lit: lit.node, ty, neg: false }),
            hir::ExprKind::Unary(hir::UnOp::Neg, expr) => match expr.kind {
                hir::ExprKind::Lit(lit) => Some(LitToConstInput { lit: lit.node, ty, neg: true }),
                _ => None,
            },
            _ => None,
        };

        lit_input
            // Allow the `ty` to be an alias type, though we cannot handle it here, we just go through
            // the more expensive anon const code path.
            .filter(|l| !l.ty.has_aliases())
            .map(|l| tcx.at(expr.span).lit_to_const(l))
    }

    fn require_type_const_attribute(
        &self,
        def_id: DefId,
        span: Span,
    ) -> Result<(), ErrorGuaranteed> {
        if find_attr!(self.tcx().get_all_attrs(def_id), AttributeKind::TypeConst(_)) {
            Ok(())
        } else {
            let mut err = self
                .dcx()
                .struct_span_err(span, "use of `const` in the type system without `#[type_const]`");
            err.span_note(
                self.tcx().def_span(def_id),
                "the declaration must be marked with `#[type_const]`",
            );
            Err(err.emit())
        }
    }

    fn lower_delegation_ty(&self, idx: hir::InferDelegationKind) -> Ty<'tcx> {
        let delegation_sig = self.tcx().inherit_sig_for_delegation_item(self.item_def_id());
        match idx {
            hir::InferDelegationKind::Input(idx) => delegation_sig[idx],
            hir::InferDelegationKind::Output => *delegation_sig.last().unwrap(),
        }
    }

    /// Lower a type from the HIR to our internal notion of a type.
    #[instrument(level = "debug", skip(self), ret)]
    pub fn lower_ty(&self, hir_ty: &hir::Ty<'tcx>) -> Ty<'tcx> {
        let tcx = self.tcx();

Review comment (suggestion):
```suggestion
        if self.tcx().is_type_const(def_id) {
```
should work instead and be a bit shorter/nicer :3

Code after:
            _ => expr,
        };

        let lit_input = match expr.kind {
            hir::ExprKind::Lit(lit) => Some(LitToConstInput { lit: lit.node, ty, neg: false }),
            hir::ExprKind::Unary(hir::UnOp::Neg, expr) => match expr.kind {
                hir::ExprKind::Lit(lit) => Some(LitToConstInput { lit: lit.node, ty, neg: true }),
                _ => None,
            },
            _ => None,
        };

        lit_input
            // Allow the `ty` to be an alias type, though we cannot handle it here, we just go through
            // the more expensive anon const code path.
            .filter(|l| !l.ty.has_aliases())
            .map(|l| tcx.at(expr.span).lit_to_const(l))
    }

    fn require_type_const_attribute(
        &self,
        def_id: DefId,
        span: Span,
    ) -> Result<(), ErrorGuaranteed> {
        let tcx = self.tcx();
        if tcx.is_type_const(def_id) {
            Ok(())
        } else {
            let mut err = self
                .dcx()
                .struct_span_err(span, "use of `const` in the type system without `#[type_const]`");
            if def_id.is_local() {
                let name = tcx.def_path_str(def_id);
                err.span_suggestion(
                    tcx.def_span(def_id).shrink_to_lo(),
                    format!("add `#[type_const]` attribute to `{name}`"),
                    format!("#[type_const]\n"),
                    Applicability::MaybeIncorrect,
                );
            } else {
                err.note("only consts marked with `#[type_const]` may be used in types");
            }
            Err(err.emit())
        }
    }

    fn lower_delegation_ty(&self, idx: hir::InferDelegationKind) -> Ty<'tcx> {
        let delegation_sig = self.tcx().inherit_sig_for_delegation_item(self.item_def_id());
        match idx {
            hir::InferDelegationKind::Input(idx) => delegation_sig[idx],
            hir::InferDelegationKind::Output => *delegation_sig.last().unwrap(),