Skip to content

Commit d9b2fec

Browse files
committed
get_all_attrs is not valid to call for all DefIds it seems.
1 parent fb74e36 commit d9b2fec

2 files changed

Lines changed: 6 additions & 2 deletions

File tree

compiler/rustc_hir_analysis/src/collect/type_of.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use core::ops::ControlFlow;
22

33
use rustc_errors::{Applicability, StashKey, Suggestions};
44
use rustc_hir::attrs::AttributeKind;
5+
use rustc_hir::def::DefKind;
56
use rustc_hir::def_id::{DefId, LocalDefId};
67
use rustc_hir::intravisit::VisitorExt;
78
use rustc_hir::{self as hir, AmbigArg, HirId, find_attr};
@@ -423,7 +424,8 @@ fn infer_placeholder_type<'tcx>(
423424
let tcx = cx.tcx();
424425

425426
fn is_type_const<'tcx>(tcx: TyCtxt<'tcx>, id: DefId) -> bool {
426-
find_attr!(tcx.get_all_attrs(id), AttributeKind::TypeConst(_))
427+
matches!(tcx.def_kind(id), DefKind::Const | DefKind::AssocConst)
428+
&& find_attr!(tcx.get_all_attrs(id), AttributeKind::TypeConst(_))
427429
}
428430

429431
// If the type is omitted on a [type_const] we can't run

compiler/rustc_mir_build/src/builder/expr/as_constant.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use rustc_abi::Size;
44
use rustc_ast::{self as ast};
55
use rustc_hir::attrs::AttributeKind;
6+
use rustc_hir::def::DefKind;
67
use rustc_hir::def_id::DefId;
78
use rustc_hir::{LangItem, find_attr};
89
use rustc_middle::mir::interpret::{CTFE_ALLOC_SALT, LitToConstInput, Scalar};
@@ -51,7 +52,8 @@ pub(crate) fn as_constant_inner<'tcx>(
5152
let Expr { ty, temp_scope_id: _, span, ref kind } = *expr;
5253

5354
fn is_type_const<'tcx>(tcx: TyCtxt<'tcx>, id: DefId) -> bool {
54-
find_attr!(tcx.get_all_attrs(id), AttributeKind::TypeConst(_))
55+
matches!(tcx.def_kind(id), DefKind::Const | DefKind::AssocConst)
56+
&& find_attr!(tcx.get_all_attrs(id), AttributeKind::TypeConst(_))
5557
}
5658

5759
match *kind {

0 commit comments

Comments
 (0)