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

Sign up
Appearance settings

Commit 996a185

Browse files
committed
Introduce tcx.anon_const_kind query
1 parent 356f2d0 commit 996a185

File tree

11 files changed

+86
-46
lines changed

11 files changed

+86
-46
lines changed

‎compiler/rustc_hir_analysis/src/collect.rs‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ pub(crate) fn provide(providers: &mut Providers) {
8989
opaque_ty_origin,
9090
rendered_precise_capturing_args,
9191
const_param_default,
92+
anon_const_kind,
9293
..*providers
9394
};
9495
}
@@ -1828,3 +1829,27 @@ fn const_param_default<'tcx>(
18281829
.lower_const_arg(default_ct, FeedConstTy::Param(def_id.to_def_id(), identity_args));
18291830
ty::EarlyBinder::bind(ct)
18301831
}
1832+
1833+
fn anon_const_kind<'tcx>(tcx: TyCtxt<'tcx>, def: LocalDefId) -> ty::AnonConstKind {
1834+
let hir_id = tcx.local_def_id_to_hir_id(def);
1835+
let const_arg_id = tcx.parent_hir_id(hir_id);
1836+
match tcx.hir_node(const_arg_id) {
1837+
hir::Node::ConstArg(_) => {
1838+
if tcx.features().generic_const_exprs() {
1839+
ty::AnonConstKind::GCEConst
1840+
} else if tcx.features().min_generic_const_args() {
1841+
ty::AnonConstKind::MCGConst
1842+
} else if let hir::Node::Expr(hir::Expr {
1843+
kind: hir::ExprKind::Repeat(_, repeat_count),
1844+
..
1845+
}) = tcx.hir_node(tcx.parent_hir_id(const_arg_id))
1846+
&& repeat_count.hir_id == const_arg_id
1847+
{
1848+
ty::AnonConstKind::RepeatExprCount
1849+
} else {
1850+
ty::AnonConstKind::MCGConst
1851+
}
1852+
}
1853+
_ => ty::AnonConstKind::NonTypeSystem,
1854+
}
1855+
}

‎compiler/rustc_hir_analysis/src/collect/generics_of.rs‎

Lines changed: 30 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -104,19 +104,27 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
104104
}
105105
}
106106

107-
if in_param_ty {
108-
// We do not allow generic parameters in anon consts if we are inside
109-
// of a const parameter type, e.g. `struct Foo<const N: usize, const M: [u8; N]>` is not allowed.
110-
None
111-
} else if tcx.features().generic_const_exprs() {
112-
let parent_node = tcx.parent_hir_node(hir_id);
113-
debug!(?parent_node);
114-
if let Node::Variant(Variant { disr_expr: Some(constant), .. }) = parent_node
115-
&& constant.hir_id == hir_id
116-
{
117-
// enum variant discriminants are not allowed to use any kind of generics
118-
None
119-
} else if let Some(param_id) = tcx.hir_opt_const_param_default_param_def_id(hir_id)
107+
match tcx.anon_const_kind(def_id) {
108+
// Stable: anon consts are not able to use any generic parameters...
109+
ty::AnonConstKind::MCGConst => None,
110+
// we provide generics to repeat expr counts as a backwards compatibility hack. #76200
111+
ty::AnonConstKind::RepeatExprCount => Some(parent_did),
112+
113+
// Even GCE anon const should not be allowed to use generic parameters as it would be
114+
// trivially forward declared uses once desugared. E.g. `const N: [u8; ANON::<N>]`.
115+
//
116+
// We could potentially mirror the hack done for defaults of generic parameters but
117+
// this case just doesn't come up much compared to `const N: u32 = ...`. Long term the
118+
// hack for defaulted parameters should be removed eventually anyway.
119+
ty::AnonConstKind::GCEConst if in_param_ty => None,
120+
// GCE anon consts as a default for a generic parameter should have their provided generics
121+
// "truncated" up to whatever generic parameter this anon const is within the default of.
122+
//
123+
// FIXME(generic_const_exprs): This only handles `const N: usize = /*defid*/` but not type
124+
// parameter defaults, e.g. `T = Foo</*defid*/>`.
125+
ty::AnonConstKind::GCEConst
126+
if let Some(param_id) =
127+
tcx.hir_opt_const_param_default_param_def_id(hir_id) =>
120128
{
121129
// If the def_id we are calling generics_of on is an anon ct default i.e:
122130
//
@@ -160,36 +168,17 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
160168
has_self: generics.has_self,
161169
has_late_bound_regions: generics.has_late_bound_regions,
162170
};
163-
} else {
164-
// HACK(eddyb) this provides the correct generics when
165-
// `feature(generic_const_expressions)` is enabled, so that const expressions
166-
// used with const generics, e.g. `Foo<{N+1}>`, can work at all.
167-
//
168-
// Note that we do not supply the parent generics when using
169-
// `min_const_generics`.
170-
Some(parent_did)
171171
}
172-
} else {
173-
let parent_node = tcx.parent_hir_node(hir_id);
174-
let parent_node = match parent_node {
175-
Node::ConstArg(ca) => tcx.parent_hir_node(ca.hir_id),
176-
_ => parent_node,
177-
};
178-
match parent_node {
179-
// HACK(eddyb) this provides the correct generics for repeat
180-
// expressions' count (i.e. `N` in `[x; N]`), and explicit
181-
// `enum` discriminants (i.e. `D` in `enum Foo { Bar = D }`),
182-
// as they shouldn't be able to cause query cycle errors.
183-
Node::Expr(Expr { kind: ExprKind::Repeat(_, ct), .. })
184-
if ct.anon_const_hir_id() == Some(hir_id) =>
185-
{
186-
Some(parent_did)
187-
}
188-
Node::TyPat(_) => Some(parent_did),
189-
// Field default values inherit the ADT's generics.
190-
Node::Field(_) => Some(parent_did),
191-
_ => None,
172+
ty::AnonConstKind::GCEConst => Some(parent_did),
173+
174+
// Field defaults are allowed to use generic parameters, e.g. `field: u32 = /*defid: N + 1*/`
175+
ty::AnonConstKind::NonTypeSystem
176+
if matches!(tcx.parent_hir_node(hir_id), Node::TyPat(_) | Node::Field(_)) =>
177+
{
178+
Some(parent_did)
192179
}
180+
// Default to no generic parameters for other kinds of anon consts
181+
ty::AnonConstKind::NonTypeSystem => None,
193182
}
194183
}
195184
Node::ConstBlock(_)

‎compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ provide! { tcx, def_id, other, cdata,
425425
doc_link_traits_in_scope => {
426426
tcx.arena.alloc_from_iter(cdata.get_doc_link_traits_in_scope(def_id.index))
427427
}
428+
anon_const_kind => { table }
428429
}
429430

430431
pub(in crate::rmeta) fn provide(providers: &mut Providers) {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1569,6 +1569,9 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
15691569
<- tcx.explicit_implied_const_bounds(def_id).skip_binder());
15701570
}
15711571
}
1572+
if let DefKind::AnonConst = def_kind {
1573+
record!(self.tables.anon_const_kind[def_id] <- self.tcx.anon_const_kind(def_id));
1574+
}
15721575
if tcx.impl_method_has_trait_impl_trait_tys(def_id)
15731576
&& let Ok(table) = self.tcx.collect_return_position_impl_trait_in_trait_tys(def_id)
15741577
{

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,7 @@ define_tables! {
480480
doc_link_traits_in_scope: Table<DefIndex, LazyArray<DefId>>,
481481
assumed_wf_types_for_rpitit: Table<DefIndex, LazyArray<(Ty<'static>, Span)>>,
482482
opaque_ty_origin: Table<DefIndex, LazyValue<hir::OpaqueTyOrigin<DefId>>>,
483+
anon_const_kind: Table<DefIndex, LazyValue<ty::AnonConstKind>>,
483484
}
484485

485486
#[derive(TyEncodable, TyDecodable)]

‎compiler/rustc_middle/src/query/erase.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ trivial! {
311311
rustc_middle::ty::Asyncness,
312312
rustc_middle::ty::AsyncDestructor,
313313
rustc_middle::ty::BoundVariableKind,
314+
rustc_middle::ty::AnonConstKind,
314315
rustc_middle::ty::DeducedParamAttrs,
315316
rustc_middle::ty::Destructor,
316317
rustc_middle::ty::fast_reject::SimplifiedType,

‎compiler/rustc_middle/src/query/mod.rs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2586,6 +2586,12 @@ rustc_queries! {
25862586
desc { "estimating codegen size of `{}`", key }
25872587
cache_on_disk_if { true }
25882588
}
2589+
2590+
query anon_const_kind(def_id: DefId) -> ty::AnonConstKind {
2591+
desc { |tcx| "looking up anon const kind of `{}`", tcx.def_path_str(def_id) }
2592+
cache_on_disk_if { def_id.is_local() }
2593+
separate_provide_extern
2594+
}
25892595
}
25902596

25912597
rustc_with_all_queries! { define_callbacks! }

‎compiler/rustc_middle/src/ty/consts.rs‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::borrow::Cow;
22

33
use rustc_data_structures::intern::Interned;
44
use rustc_error_messages::MultiSpan;
5-
use rustc_macros::HashStable;
5+
use rustc_macros::{HashStable,TyDecodable,TyEncodable};
66
use rustc_type_ir::walk::TypeWalker;
77
use rustc_type_ir::{self as ir, TypeFlags, WithCachedTypeInfo};
88

@@ -259,3 +259,11 @@ impl<'tcx> Const<'tcx> {
259259
TypeWalker::new(self.into())
260260
}
261261
}
262+
263+
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, TyEncodable, TyDecodable, HashStable)]
264+
pub enum AnonConstKind {
265+
GCEConst,
266+
MCGConst,
267+
RepeatExprCount,
268+
NonTypeSystem,
269+
}

‎compiler/rustc_middle/src/ty/mod.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ pub use self::closure::{
7474
place_to_string_for_capture,
7575
};
7676
pub use self::consts::{
77-
Const, ConstInt, ConstKind, Expr, ExprKind, ScalarInt, UnevaluatedConst,ValTree,ValTreeKind,
78-
Value,
77+
AnonConstKind,Const, ConstInt, ConstKind, Expr, ExprKind, ScalarInt, UnevaluatedConst,
78+
ValTree,ValTreeKind,Value,
7979
};
8080
pub use self::context::{
8181
CtxtInterners, CurrentGcx, DeducedParamAttrs, Feed, FreeRegionInfo, GlobalCtxt, Lift, TyCtxt,

‎compiler/rustc_middle/src/ty/parameterized.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ trivially_parameterized_over_tcx! {
6868
ty::AsyncDestructor,
6969
ty::AssocItemContainer,
7070
ty::Asyncness,
71+
ty::AnonConstKind,
7172
ty::DeducedParamAttrs,
7273
ty::Destructor,
7374
ty::Generics,

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /