Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c763ebc

Browse filesBrowse files
authored
Rollup merge of #102830 - compiler-errors:constness-parity, r=fee1-dead
Unify `tcx.constness` query and param env constness checks The checks that we do in the `constness` query seem inconsistent with the checks that we do to determine if an item's param-env is const, so I merged them into the `constness` query and call that from the `param_env` query. I'm not sure if this totally makes sense -- is there a case where `tcx.param_env()` would return a const param-env for an item whose `tcx.constness()` is `Constness::NotConst`? Because if not, it seems a bit dangerous that these two differ. Luckily, not many places actually use `tcx.constness()`, and the checks in `tcx.param_env()` seem stricter than the checks in `tcx.constness()` (at least for the types of items we type-check). Also, due to the way that `tcx.param_env()` is implemented, it _never_ used to return a const param-env for a item coming from a different crate, which also seems dangerous (though also probably not weaponizable currently, because we seldom actually compute the param-env for a non-local item).
2 parents 40deece + bef8681 commit c763ebc
Copy full SHA for c763ebc

File tree

Expand file treeCollapse file tree

3 files changed

+98
-99
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+98
-99
lines changed

‎compiler/rustc_const_eval/src/const_eval/fn_queries.rs

Copy file name to clipboardExpand all lines: compiler/rustc_const_eval/src/const_eval/fn_queries.rs
+56-16Lines changed: 56 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,10 @@ pub fn is_parent_const_impl_raw(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
2525
/// report whether said intrinsic has a `rustc_const_{un,}stable` attribute. Otherwise, return
2626
/// `Constness::NotConst`.
2727
fn constness(tcx: TyCtxt<'_>, def_id: DefId) -> hir::Constness {
28-
let def_id = def_id.expect_local();
29-
let node = tcx.hir().get_by_def_id(def_id);
30-
31-
match node {
28+
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
29+
match tcx.hir().get(hir_id) {
3230
hir::Node::Ctor(_) => hir::Constness::Const,
33-
hir::Node::Item(hir::Item { kind: hir::ItemKind::Impl(impl_), .. }) => impl_.constness,
31+
3432
hir::Node::ForeignItem(hir::ForeignItem { kind: hir::ForeignItemKind::Fn(..), .. }) => {
3533
// Intrinsics use `rustc_const_{un,}stable` attributes to indicate constness. All other
3634
// foreign items cannot be evaluated at compile-time.
@@ -41,20 +39,62 @@ fn constness(tcx: TyCtxt<'_>, def_id: DefId) -> hir::Constness {
4139
};
4240
if is_const { hir::Constness::Const } else { hir::Constness::NotConst }
4341
}
44-
_ => {
45-
if let Some(fn_kind) = node.fn_kind() {
46-
if fn_kind.constness() == hir::Constness::Const {
47-
return hir::Constness::Const;
48-
}
4942

50-
// If the function itself is not annotated with `const`, it may still be a `const fn`
51-
// if it resides in a const trait impl.
52-
let is_const = is_parent_const_impl_raw(tcx, def_id);
53-
if is_const { hir::Constness::Const } else { hir::Constness::NotConst }
54-
} else {
55-
hir::Constness::NotConst
43+
hir::Node::TraitItem(hir::TraitItem { kind: hir::TraitItemKind::Fn(..), .. })
44+
if tcx.is_const_default_method(def_id) =>
45+
{
46+
hir::Constness::Const
47+
}
48+
49+
hir::Node::Item(hir::Item { kind: hir::ItemKind::Const(..), .. })
50+
| hir::Node::Item(hir::Item { kind: hir::ItemKind::Static(..), .. })
51+
| hir::Node::TraitItem(hir::TraitItem { kind: hir::TraitItemKind::Const(..), .. })
52+
| hir::Node::AnonConst(_)
53+
| hir::Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Const(..), .. })
54+
| hir::Node::ImplItem(hir::ImplItem {
55+
kind:
56+
hir::ImplItemKind::Fn(
57+
hir::FnSig {
58+
header: hir::FnHeader { constness: hir::Constness::Const, .. },
59+
..
60+
},
61+
..,
62+
),
63+
..
64+
}) => hir::Constness::Const,
65+
66+
hir::Node::ImplItem(hir::ImplItem {
67+
kind: hir::ImplItemKind::Type(..) | hir::ImplItemKind::Fn(..),
68+
..
69+
}) => {
70+
let parent_hir_id = tcx.hir().get_parent_node(hir_id);
71+
match tcx.hir().get(parent_hir_id) {
72+
hir::Node::Item(hir::Item {
73+
kind: hir::ItemKind::Impl(hir::Impl { constness, .. }),
74+
..
75+
}) => *constness,
76+
_ => span_bug!(
77+
tcx.def_span(parent_hir_id.owner),
78+
"impl item's parent node is not an impl",
79+
),
5680
}
5781
}
82+
83+
hir::Node::Item(hir::Item {
84+
kind: hir::ItemKind::Fn(hir::FnSig { header: hir::FnHeader { constness, .. }, .. }, ..),
85+
..
86+
})
87+
| hir::Node::TraitItem(hir::TraitItem {
88+
kind:
89+
hir::TraitItemKind::Fn(hir::FnSig { header: hir::FnHeader { constness, .. }, .. }, ..),
90+
..
91+
})
92+
| hir::Node::Item(hir::Item {
93+
kind: hir::ItemKind::Impl(hir::Impl { constness, .. }),
94+
..
95+
}) => *constness,
96+
97+
_ => hir::Constness::NotConst,
5898
}
5999
}
60100

‎compiler/rustc_metadata/src/rmeta/encoder.rs

Copy file name to clipboardExpand all lines: compiler/rustc_metadata/src/rmeta/encoder.rs
+41-15Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,6 +1059,43 @@ fn should_encode_const(def_kind: DefKind) -> bool {
10591059
}
10601060
}
10611061

1062+
fn should_encode_constness(def_kind: DefKind) -> bool {
1063+
match def_kind {
1064+
DefKind::Struct
1065+
| DefKind::Union
1066+
| DefKind::Enum
1067+
| DefKind::Trait
1068+
| DefKind::AssocTy
1069+
| DefKind::Fn
1070+
| DefKind::Const
1071+
| DefKind::Static(..)
1072+
| DefKind::Ctor(..)
1073+
| DefKind::AssocFn
1074+
| DefKind::AssocConst
1075+
| DefKind::AnonConst
1076+
| DefKind::InlineConst
1077+
| DefKind::OpaqueTy
1078+
| DefKind::ImplTraitPlaceholder
1079+
| DefKind::Impl
1080+
| DefKind::Closure
1081+
| DefKind::Generator
1082+
| DefKind::TyAlias => true,
1083+
DefKind::Variant
1084+
| DefKind::TraitAlias
1085+
| DefKind::ForeignTy
1086+
| DefKind::Field
1087+
| DefKind::TyParam
1088+
| DefKind::Mod
1089+
| DefKind::ForeignMod
1090+
| DefKind::ConstParam
1091+
| DefKind::Macro(..)
1092+
| DefKind::Use
1093+
| DefKind::LifetimeParam
1094+
| DefKind::GlobalAsm
1095+
| DefKind::ExternCrate => false,
1096+
}
1097+
}
1098+
10621099
fn should_encode_trait_impl_trait_tys<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> bool {
10631100
if tcx.def_kind(def_id) != DefKind::AssocFn {
10641101
return false;
@@ -1165,6 +1202,9 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
11651202
{
11661203
record!(self.tables.trait_impl_trait_tys[def_id] <- table);
11671204
}
1205+
if should_encode_constness(def_kind) {
1206+
self.tables.constness.set(def_id.index, tcx.constness(def_id));
1207+
}
11681208
}
11691209
let inherent_impls = tcx.crate_inherent_impls(());
11701210
for (def_id, implementations) in inherent_impls.inherent_impls.iter() {
@@ -1192,7 +1232,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
11921232
};
11931233

11941234
record!(self.tables.variant_data[def_id] <- data);
1195-
self.tables.constness.set(def_id.index, hir::Constness::Const);
11961235
record_array!(self.tables.children[def_id] <- variant.fields.iter().map(|f| {
11971236
assert!(f.did.is_local());
11981237
f.did.index
@@ -1220,7 +1259,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
12201259
};
12211260

12221261
record!(self.tables.variant_data[def_id] <- data);
1223-
self.tables.constness.set(def_id.index, hir::Constness::Const);
12241262
if variant.ctor_kind == CtorKind::Fn {
12251263
record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(def_id));
12261264
}
@@ -1284,7 +1322,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
12841322

12851323
record!(self.tables.repr_options[def_id] <- adt_def.repr());
12861324
record!(self.tables.variant_data[def_id] <- data);
1287-
self.tables.constness.set(def_id.index, hir::Constness::Const);
12881325
if variant.ctor_kind == CtorKind::Fn {
12891326
record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(def_id));
12901327
}
@@ -1320,7 +1357,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
13201357
}
13211358
};
13221359
self.tables.asyncness.set(def_id.index, m_sig.header.asyncness);
1323-
self.tables.constness.set(def_id.index, hir::Constness::NotConst);
13241360
}
13251361
ty::AssocKind::Type => {
13261362
self.encode_explicit_item_bounds(def_id);
@@ -1345,13 +1381,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
13451381
let hir::ImplItemKind::Fn(ref sig, body) = ast_item.kind else { bug!() };
13461382
self.tables.asyncness.set(def_id.index, sig.header.asyncness);
13471383
record_array!(self.tables.fn_arg_names[def_id] <- self.tcx.hir().body_param_names(body));
1348-
// Can be inside `impl const Trait`, so using sig.header.constness is not reliable
1349-
let constness = if self.tcx.is_const_fn_raw(def_id) {
1350-
hir::Constness::Const
1351-
} else {
1352-
hir::Constness::NotConst
1353-
};
1354-
self.tables.constness.set(def_id.index, constness);
13551384
}
13561385
ty::AssocKind::Const | ty::AssocKind::Type => {}
13571386
}
@@ -1474,7 +1503,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
14741503
hir::ItemKind::Fn(ref sig, .., body) => {
14751504
self.tables.asyncness.set(def_id.index, sig.header.asyncness);
14761505
record_array!(self.tables.fn_arg_names[def_id] <- self.tcx.hir().body_param_names(body));
1477-
self.tables.constness.set(def_id.index, sig.header.constness);
14781506
}
14791507
hir::ItemKind::Macro(ref macro_def, _) => {
14801508
if macro_def.macro_rules {
@@ -1495,7 +1523,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
14951523
hir::ItemKind::Struct(ref struct_def, _) => {
14961524
let adt_def = self.tcx.adt_def(def_id);
14971525
record!(self.tables.repr_options[def_id] <- adt_def.repr());
1498-
self.tables.constness.set(def_id.index, hir::Constness::Const);
14991526

15001527
// Encode def_ids for each field and method
15011528
// for methods, write all the stuff get_trait_method
@@ -1524,9 +1551,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
15241551
is_non_exhaustive: variant.is_field_list_non_exhaustive(),
15251552
});
15261553
}
1527-
hir::ItemKind::Impl(hir::Impl { defaultness, constness, .. }) => {
1554+
hir::ItemKind::Impl(hir::Impl { defaultness, .. }) => {
15281555
self.tables.impl_defaultness.set(def_id.index, *defaultness);
1529-
self.tables.constness.set(def_id.index, *constness);
15301556

15311557
let trait_ref = self.tcx.impl_trait_ref(def_id);
15321558
if let Some(trait_ref) = trait_ref {

‎compiler/rustc_ty_utils/src/ty.rs

Copy file name to clipboardExpand all lines: compiler/rustc_ty_utils/src/ty.rs
+1-68Lines changed: 1 addition & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -137,77 +137,10 @@ fn param_env(tcx: TyCtxt<'_>, def_id: DefId) -> ty::ParamEnv<'_> {
137137
let local_did = def_id.as_local();
138138
let hir_id = local_did.map(|def_id| tcx.hir().local_def_id_to_hir_id(def_id));
139139

140-
let constness = match hir_id {
141-
Some(hir_id) => match tcx.hir().get(hir_id) {
142-
hir::Node::TraitItem(hir::TraitItem { kind: hir::TraitItemKind::Fn(..), .. })
143-
if tcx.is_const_default_method(def_id) =>
144-
{
145-
hir::Constness::Const
146-
}
147-
148-
hir::Node::Item(hir::Item { kind: hir::ItemKind::Const(..), .. })
149-
| hir::Node::Item(hir::Item { kind: hir::ItemKind::Static(..), .. })
150-
| hir::Node::TraitItem(hir::TraitItem {
151-
kind: hir::TraitItemKind::Const(..), ..
152-
})
153-
| hir::Node::AnonConst(_)
154-
| hir::Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Const(..), .. })
155-
| hir::Node::ImplItem(hir::ImplItem {
156-
kind:
157-
hir::ImplItemKind::Fn(
158-
hir::FnSig {
159-
header: hir::FnHeader { constness: hir::Constness::Const, .. },
160-
..
161-
},
162-
..,
163-
),
164-
..
165-
}) => hir::Constness::Const,
166-
167-
hir::Node::ImplItem(hir::ImplItem {
168-
kind: hir::ImplItemKind::Type(..) | hir::ImplItemKind::Fn(..),
169-
..
170-
}) => {
171-
let parent_hir_id = tcx.hir().get_parent_node(hir_id);
172-
match tcx.hir().get(parent_hir_id) {
173-
hir::Node::Item(hir::Item {
174-
kind: hir::ItemKind::Impl(hir::Impl { constness, .. }),
175-
..
176-
}) => *constness,
177-
_ => span_bug!(
178-
tcx.def_span(parent_hir_id.owner),
179-
"impl item's parent node is not an impl",
180-
),
181-
}
182-
}
183-
184-
hir::Node::Item(hir::Item {
185-
kind:
186-
hir::ItemKind::Fn(hir::FnSig { header: hir::FnHeader { constness, .. }, .. }, ..),
187-
..
188-
})
189-
| hir::Node::TraitItem(hir::TraitItem {
190-
kind:
191-
hir::TraitItemKind::Fn(
192-
hir::FnSig { header: hir::FnHeader { constness, .. }, .. },
193-
..,
194-
),
195-
..
196-
})
197-
| hir::Node::Item(hir::Item {
198-
kind: hir::ItemKind::Impl(hir::Impl { constness, .. }),
199-
..
200-
}) => *constness,
201-
202-
_ => hir::Constness::NotConst,
203-
},
204-
None => hir::Constness::NotConst,
205-
};
206-
207140
let unnormalized_env = ty::ParamEnv::new(
208141
tcx.intern_predicates(&predicates),
209142
traits::Reveal::UserFacing,
210-
constness,
143+
tcx.constness(def_id),
211144
);
212145

213146
let body_id =

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.