Skip to content

Commit 6a6b0f1

Browse files
committed
always lower anon consts for bodies of const items to anon consts on stable
1 parent f10a9e0 commit 6a6b0f1

File tree

6 files changed

+91
-67
lines changed

6 files changed

+91
-67
lines changed

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
201201
|this| {
202202
let ty = this
203203
.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
204-
let body = this.lower_anon_const_to_const_arg(body.as_deref().unwrap());
204+
let body = this.lower_anon_const_to_const_arg(
205+
body.as_deref().unwrap(),
206+
!this.tcx.features().min_generic_const_args(),
207+
);
205208
(ty, body)
206209
},
207210
);
@@ -812,8 +815,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
812815
|this| {
813816
let ty = this
814817
.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
815-
let body =
816-
body.as_deref().map(|body| this.lower_anon_const_to_const_arg(body));
818+
let body = body.as_deref().map(|body| {
819+
this.lower_anon_const_to_const_arg(
820+
body,
821+
!this.tcx.features().min_generic_const_args(),
822+
)
823+
});
817824
hir::TraitItemKind::Const(ty, body)
818825
},
819826
);
@@ -1027,7 +1034,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
10271034
let ty = this
10281035
.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
10291036
this.lower_define_opaque(hir_id, &define_opaque);
1030-
let body = this.lower_anon_const_to_const_arg(body.as_deref().unwrap());
1037+
let body = this.lower_anon_const_to_const_arg(
1038+
body.as_deref().unwrap(),
1039+
!this.tcx.features().min_generic_const_args(),
1040+
);
10311041
hir::ImplItemKind::Const(ty, body)
10321042
},
10331043
),

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,7 +1084,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
10841084
AssocItemConstraintKind::Equality { term } => {
10851085
let term = match term {
10861086
Term::Ty(ty) => self.lower_ty(ty, itctx).into(),
1087-
Term::Const(c) => self.lower_anon_const_to_const_arg(c).into(),
1087+
Term::Const(c) => self.lower_anon_const_to_const_arg(c, false).into(),
10881088
};
10891089
hir::AssocItemConstraintKind::Equality { term }
10901090
}
@@ -1210,9 +1210,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
12101210
}
12111211
GenericArg::Type(self.lower_ty(ty, itctx).try_as_ambig_ty().unwrap())
12121212
}
1213-
ast::GenericArg::Const(ct) => {
1214-
GenericArg::Const(self.lower_anon_const_to_const_arg(ct).try_as_ambig_ct().unwrap())
1215-
}
1213+
ast::GenericArg::Const(ct) => GenericArg::Const(
1214+
self.lower_anon_const_to_const_arg(ct, false).try_as_ambig_ct().unwrap(),
1215+
),
12161216
}
12171217
}
12181218

@@ -2024,7 +2024,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
20242024
false
20252025
}
20262026
})
2027-
.map(|def| self.lower_anon_const_to_const_arg(def));
2027+
.map(|def| self.lower_anon_const_to_const_arg(def, false));
20282028

20292029
(
20302030
hir::ParamName::Plain(self.lower_ident(param.ident)),
@@ -2236,7 +2236,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
22362236
let ct_kind = hir::ConstArgKind::Infer(self.lower_span(c.value.span), ());
22372237
self.arena.alloc(hir::ConstArg { hir_id: self.lower_node_id(c.id), kind: ct_kind })
22382238
}
2239-
_ => self.lower_anon_const_to_const_arg(c),
2239+
_ => self.lower_anon_const_to_const_arg(c, false),
22402240
}
22412241
}
22422242

@@ -2305,12 +2305,21 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
23052305

23062306
/// See [`hir::ConstArg`] for when to use this function vs
23072307
/// [`Self::lower_anon_const_to_anon_const`].
2308-
fn lower_anon_const_to_const_arg(&mut self, anon: &AnonConst) -> &'hir hir::ConstArg<'hir> {
2309-
self.arena.alloc(self.lower_anon_const_to_const_arg_direct(anon))
2308+
fn lower_anon_const_to_const_arg(
2309+
&mut self,
2310+
anon: &AnonConst,
2311+
always_lower_to_anon_const: bool,
2312+
) -> &'hir hir::ConstArg<'hir> {
2313+
self.arena
2314+
.alloc(self.lower_anon_const_to_const_arg_direct(anon, always_lower_to_anon_const))
23102315
}
23112316

23122317
#[instrument(level = "debug", skip(self))]
2313-
fn lower_anon_const_to_const_arg_direct(&mut self, anon: &AnonConst) -> hir::ConstArg<'hir> {
2318+
fn lower_anon_const_to_const_arg_direct(
2319+
&mut self,
2320+
anon: &AnonConst,
2321+
always_lower_to_anon_const: bool,
2322+
) -> hir::ConstArg<'hir> {
23142323
let tcx = self.tcx;
23152324
// Unwrap a block, so that e.g. `{ P }` is recognised as a parameter. Const arguments
23162325
// currently have to be wrapped in curly brackets, so it's necessary to special-case.
@@ -2329,6 +2338,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
23292338
&& path.is_potential_trivial_const_arg(tcx.features().min_generic_const_args())
23302339
&& (tcx.features().min_generic_const_args()
23312340
|| matches!(maybe_res, Some(Res::Def(DefKind::ConstParam, _))))
2341+
&& !always_lower_to_anon_const
23322342
{
23332343
let qpath = self.lower_qpath(
23342344
expr.id,

compiler/rustc_ast_lowering/src/pat.rs

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -438,35 +438,36 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
438438
fn lower_ty_pat_mut(&mut self, pattern: &TyPat, base_type: Span) -> hir::TyPat<'hir> {
439439
// loop here to avoid recursion
440440
let pat_hir_id = self.lower_node_id(pattern.id);
441-
let node = match &pattern.kind {
442-
TyPatKind::Range(e1, e2, Spanned { node: end, span }) => hir::TyPatKind::Range(
443-
e1.as_deref().map(|e| self.lower_anon_const_to_const_arg(e)).unwrap_or_else(|| {
444-
self.lower_ty_pat_range_end(
445-
hir::LangItem::RangeMin,
446-
span.shrink_to_lo(),
447-
base_type,
448-
)
449-
}),
450-
e2.as_deref()
451-
.map(|e| match end {
452-
RangeEnd::Included(..) => self.lower_anon_const_to_const_arg(e),
453-
RangeEnd::Excluded => self.lower_excluded_range_end(e),
454-
})
455-
.unwrap_or_else(|| {
456-
self.lower_ty_pat_range_end(
457-
hir::LangItem::RangeMax,
458-
span.shrink_to_hi(),
459-
base_type,
460-
)
461-
}),
462-
),
463-
TyPatKind::Or(variants) => {
464-
hir::TyPatKind::Or(self.arena.alloc_from_iter(
441+
let node =
442+
match &pattern.kind {
443+
TyPatKind::Range(e1, e2, Spanned { node: end, span }) => hir::TyPatKind::Range(
444+
e1.as_deref()
445+
.map(|e| self.lower_anon_const_to_const_arg(e, false))
446+
.unwrap_or_else(|| {
447+
self.lower_ty_pat_range_end(
448+
hir::LangItem::RangeMin,
449+
span.shrink_to_lo(),
450+
base_type,
451+
)
452+
}),
453+
e2.as_deref()
454+
.map(|e| match end {
455+
RangeEnd::Included(..) => self.lower_anon_const_to_const_arg(e, false),
456+
RangeEnd::Excluded => self.lower_excluded_range_end(e),
457+
})
458+
.unwrap_or_else(|| {
459+
self.lower_ty_pat_range_end(
460+
hir::LangItem::RangeMax,
461+
span.shrink_to_hi(),
462+
base_type,
463+
)
464+
}),
465+
),
466+
TyPatKind::Or(variants) => hir::TyPatKind::Or(self.arena.alloc_from_iter(
465467
variants.iter().map(|pat| self.lower_ty_pat_mut(pat, base_type)),
466-
))
467-
}
468-
TyPatKind::Err(guar) => hir::TyPatKind::Err(*guar),
469-
};
468+
)),
469+
TyPatKind::Err(guar) => hir::TyPatKind::Err(*guar),
470+
};
470471

471472
hir::TyPat { hir_id: pat_hir_id, kind: node, span: self.lower_span(pattern.span) }
472473
}

compiler/rustc_const_eval/src/const_eval/eval_queries.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,17 @@ pub fn eval_to_allocation_raw_provider<'tcx>(
352352
{
353353
let ct = tcx.const_of_item(def_id).instantiate(tcx, key.value.instance.args);
354354
match ct.kind() {
355-
ty::ConstKind::Unevaluated(_) => {
356-
return Err(ErrorHandled::TooGeneric(DUMMY_SP));
355+
ty::ConstKind::Unevaluated(_) if tcx.features().min_generic_const_args() => {
356+
bug!("fully resolving const items under mgca for const eval is not supported yet");
357+
}
358+
ty::ConstKind::Unevaluated(uv) => {
359+
assert_eq!(tcx.def_kind(uv.def), DefKind::AnonConst);
360+
361+
let id = GlobalId {
362+
instance: ty::Instance { def: ty::InstanceKind::Item(uv.def), args: uv.args },
363+
promoted: None,
364+
};
365+
return eval_in_interpreter(tcx, id, key.typing_env);
357366
}
358367
ty::ConstKind::Value(cv) => return Ok(tcx.valtree_to_const_alloc(cv)),
359368
ty::ConstKind::Error(guar) => {
@@ -369,7 +378,7 @@ pub fn eval_to_allocation_raw_provider<'tcx>(
369378
ty::ConstKind::Infer(_) | ty::ConstKind::Bound(..) => {
370379
bug!("unexpected constant {ct:?}")
371380
}
372-
}
381+
};
373382
}
374383

375384
eval_in_interpreter(tcx, key.value, key.typing_env)

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2328,15 +2328,27 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
23282328
// generic arguments, just weaker type inference.
23292329
let ty = tcx.type_of(anon.def_id).instantiate_identity();
23302330

2331-
match self.try_lower_anon_const_lit(ty, expr) {
2332-
Some(v) => v,
2333-
None => ty::Const::new_unevaluated(
2331+
let mk_uv = || {
2332+
ty::Const::new_unevaluated(
23342333
tcx,
23352334
ty::UnevaluatedConst {
23362335
def: anon.def_id.to_def_id(),
23372336
args: ty::GenericArgs::identity_for_item(tcx, anon.def_id.to_def_id()),
23382337
},
2339-
),
2338+
)
2339+
};
2340+
2341+
// On stable we always lower the anon const on the rhs of const items
2342+
// to be an anon const.
2343+
if tcx.anon_const_kind(anon.def_id) == ty::AnonConstKind::ItemBody
2344+
&& !tcx.features().min_generic_const_args()
2345+
{
2346+
return mk_uv();
2347+
}
2348+
2349+
match self.try_lower_anon_const_lit(ty, expr) {
2350+
Some(v) => v,
2351+
None => mk_uv(),
23402352
}
23412353
}
23422354

compiler/rustc_ty_utils/src/instance.rs

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use rustc_errors::ErrorGuaranteed;
22
use rustc_hir::LangItem;
3-
use rustc_hir::def::DefKind;
43
use rustc_hir::def_id::DefId;
54
use rustc_infer::infer::TyCtxtInferExt;
65
use rustc_middle::bug;
@@ -98,24 +97,7 @@ fn resolve_instance_raw<'tcx>(
9897

9998
Ok(Some(Instance { def, args }))
10099
};
101-
if let Ok(Some(Instance { def: ty::InstanceKind::Item(def_id), args })) = result
102-
&& matches!(tcx.def_kind(def_id), DefKind::Const | DefKind::AssocConst)
103-
{
104-
debug!(" => resolved to const item");
105-
let ct = tcx.const_of_item(def_id).instantiate(tcx, args);
106-
debug!("ct={ct:?}");
107-
if let ty::ConstKind::Unevaluated(uv) = ct.kind() {
108-
if tcx.def_kind(uv.def) == DefKind::AnonConst {
109-
return Ok(Some(ty::Instance {
110-
def: ty::InstanceKind::Item(uv.def),
111-
args: uv.args,
112-
}));
113-
} else {
114-
let input = PseudoCanonicalInput { typing_env, value: (uv.def, uv.args) };
115-
return tcx.resolve_instance_raw(input);
116-
}
117-
}
118-
}
100+
119101
result
120102
}
121103

0 commit comments

Comments
 (0)