Skip to content

Commit 5a24dd4

Browse files
authored
Rollup merge of rust-lang#129246 - BoxyUwU:feature_gate_const_arg_path, r=cjgillot
Retroactively feature gate `ConstArgKind::Path` This puts the lowering introduced by rust-lang#125915 under a feature gate until we fix the regressions introduced by it. Alternative to whole sale reverting the PR since it didn't seem like a very clean revert and I think this is generally a step in the right direction and don't want to get stuck landing and reverting the PR over and over :) cc rust-lang#129137 `@camelid,` tests taken from there. beta is branching soon so I think it makes sense to not try and rush that fix through since it wont have much time to bake and if it has issues we can't simply revert it on beta. Fixes rust-lang#128016
2 parents 5bca6e2 + b8eedfa commit 5a24dd4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+425
-206
lines changed

compiler/rustc_ast_lowering/src/asm.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
220220
let parent_def_id = self.current_def_id_parent;
221221
let node_id = self.next_node_id();
222222
// HACK(min_generic_const_args): see lower_anon_const
223-
if !expr.is_potential_trivial_const_arg() {
223+
if !self.tcx.features().const_arg_path
224+
|| !expr.is_potential_trivial_const_arg()
225+
{
224226
self.create_def(
225227
parent_def_id,
226228
node_id,

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
387387
let node_id = self.next_node_id();
388388

389389
// HACK(min_generic_const_args): see lower_anon_const
390-
if !arg.is_potential_trivial_const_arg() {
390+
if !self.tcx.features().const_arg_path || !arg.is_potential_trivial_const_arg() {
391391
// Add a definition for the in-band const def.
392392
self.create_def(parent_def_id, node_id, kw::Empty, DefKind::AnonConst, f.span);
393393
}

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2358,7 +2358,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
23582358
span: Span,
23592359
) -> &'hir hir::ConstArg<'hir> {
23602360
let ct_kind = match res {
2361-
Res::Def(DefKind::ConstParam, _) => {
2361+
Res::Def(DefKind::ConstParam, _) if self.tcx.features().const_arg_path => {
23622362
let qpath = self.lower_qpath(
23632363
ty_id,
23642364
&None,
@@ -2433,7 +2433,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
24332433
self.resolver.get_partial_res(expr.id).and_then(|partial_res| partial_res.full_res());
24342434
debug!("res={:?}", maybe_res);
24352435
// FIXME(min_generic_const_args): for now we only lower params to ConstArgKind::Path
2436-
if let Some(res) = maybe_res
2436+
if self.tcx.features().const_arg_path
2437+
&& let Some(res) = maybe_res
24372438
&& let Res::Def(DefKind::ConstParam, _) = res
24382439
&& let ExprKind::Path(qself, path) = &expr.kind
24392440
{
@@ -2464,7 +2465,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
24642465
/// See [`hir::ConstArg`] for when to use this function vs
24652466
/// [`Self::lower_anon_const_to_const_arg`].
24662467
fn lower_anon_const_to_anon_const(&mut self, c: &AnonConst) -> &'hir hir::AnonConst {
2467-
if c.value.is_potential_trivial_const_arg() {
2468+
if self.tcx.features().const_arg_path && c.value.is_potential_trivial_const_arg() {
24682469
// HACK(min_generic_const_args): see DefCollector::visit_anon_const
24692470
// Over there, we guess if this is a bare param and only create a def if
24702471
// we think it's not. However we may can guess wrong (see there for example)

compiler/rustc_feature/src/unstable.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,8 @@ declare_features! (
193193
(unstable, anonymous_lifetime_in_impl_trait, "1.63.0", None),
194194
/// Allows identifying the `compiler_builtins` crate.
195195
(internal, compiler_builtins, "1.13.0", None),
196+
/// Gating for a new desugaring of const arguments of usages of const parameters
197+
(internal, const_arg_path, "1.81.0", None),
196198
/// Allows writing custom MIR
197199
(internal, custom_mir, "1.65.0", None),
198200
/// Outputs useful `assert!` messages

compiler/rustc_middle/src/ty/consts.rs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ impl<'tcx> Const<'tcx> {
240240

241241
let ty = tcx.type_of(def).no_bound_vars().expect("const parameter types cannot be generic");
242242

243-
match Self::try_from_lit(tcx, ty, expr) {
243+
match Self::try_from_lit_or_param(tcx, ty, expr) {
244244
Some(v) => v,
245245
None => ty::Const::new_unevaluated(
246246
tcx,
@@ -281,7 +281,11 @@ impl<'tcx> Const<'tcx> {
281281
}
282282

283283
#[instrument(skip(tcx), level = "debug")]
284-
fn try_from_lit(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, expr: &'tcx hir::Expr<'tcx>) -> Option<Self> {
284+
fn try_from_lit_or_param(
285+
tcx: TyCtxt<'tcx>,
286+
ty: Ty<'tcx>,
287+
expr: &'tcx hir::Expr<'tcx>,
288+
) -> Option<Self> {
285289
// Unwrap a block, so that e.g. `{ P }` is recognised as a parameter. Const arguments
286290
// currently have to be wrapped in curly brackets, so it's necessary to special-case.
287291
let expr = match &expr.kind {
@@ -291,6 +295,22 @@ impl<'tcx> Const<'tcx> {
291295
_ => expr,
292296
};
293297

298+
if let hir::ExprKind::Path(
299+
qpath @ hir::QPath::Resolved(
300+
_,
301+
&hir::Path { res: Res::Def(DefKind::ConstParam, _), .. },
302+
),
303+
) = expr.kind
304+
{
305+
if tcx.features().const_arg_path {
306+
span_bug!(
307+
expr.span,
308+
"try_from_lit: received const param which shouldn't be possible"
309+
);
310+
}
311+
return Some(Const::from_param(tcx, qpath, expr.hir_id));
312+
};
313+
294314
let lit_input = match expr.kind {
295315
hir::ExprKind::Lit(lit) => Some(LitToConstInput { lit: &lit.node, ty, neg: false }),
296316
hir::ExprKind::Unary(hir::UnOp::Neg, expr) => match expr.kind {
@@ -318,14 +338,6 @@ impl<'tcx> Const<'tcx> {
318338
}
319339
}
320340

321-
if let hir::ExprKind::Path(hir::QPath::Resolved(
322-
_,
323-
&hir::Path { res: Res::Def(DefKind::ConstParam, _), .. },
324-
)) = expr.kind
325-
{
326-
span_bug!(expr.span, "try_from_lit: received const param which shouldn't be possible")
327-
}
328-
329341
None
330342
}
331343

compiler/rustc_resolve/src/def_collector.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -314,13 +314,15 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> {
314314
}
315315

316316
fn visit_anon_const(&mut self, constant: &'a AnonConst) {
317-
// HACK(min_generic_const_args): don't create defs for anon consts if we think they will
318-
// later be turned into ConstArgKind::Path's. because this is before resolve is done, we
319-
// may accidentally identify a construction of a unit struct as a param and not create a
320-
// def. we'll then create a def later in ast lowering in this case. the parent of nested
321-
// items will be messed up, but that's ok because there can't be any if we're just looking
322-
// for bare idents.
323-
if constant.value.is_potential_trivial_const_arg() {
317+
if self.resolver.tcx.features().const_arg_path
318+
&& constant.value.is_potential_trivial_const_arg()
319+
{
320+
// HACK(min_generic_const_args): don't create defs for anon consts if we think they will
321+
// later be turned into ConstArgKind::Path's. because this is before resolve is done, we
322+
// may accidentally identify a construction of a unit struct as a param and not create a
323+
// def. we'll then create a def later in ast lowering in this case. the parent of nested
324+
// items will be messed up, but that's ok because there can't be any if we're just looking
325+
// for bare idents.
324326
visit::walk_anon_const(self, constant)
325327
} else {
326328
let def =

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,7 @@ symbols! {
595595
conservative_impl_trait,
596596
console,
597597
const_allocate,
598+
const_arg_path,
598599
const_async_blocks,
599600
const_closures,
600601
const_compare_raw_pointers,

tests/crashes/127972.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//@ known-bug: #127962
2-
#![feature(generic_const_exprs)]
2+
#![feature(generic_const_exprs, const_arg_path)]
33

44
fn zero_init<const usize: usize>() -> Substs1<{ (N) }> {
55
Substs1([0; { (usize) }])

tests/crashes/128016.rs

Lines changed: 0 additions & 10 deletions
This file was deleted.

tests/ui-fulldeps/stable-mir/check_instance.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ extern crate rustc_driver;
1717
extern crate rustc_interface;
1818
extern crate stable_mir;
1919

20-
use mir::{mono::Instance, TerminatorKind::*};
20+
use std::io::Write;
21+
use std::ops::ControlFlow;
22+
23+
use mir::mono::Instance;
24+
use mir::TerminatorKind::*;
2125
use rustc_smir::rustc_internal;
2226
use stable_mir::ty::{RigidTy, TyKind};
2327
use stable_mir::*;
24-
use std::io::Write;
25-
use std::ops::ControlFlow;
2628

2729
const CRATE_NAME: &str = "input";
2830

@@ -33,7 +35,7 @@ fn test_stable_mir() -> ControlFlow<()> {
3335
// Get all items and split generic vs monomorphic items.
3436
let (generic, mono): (Vec<_>, Vec<_>) =
3537
items.into_iter().partition(|item| item.requires_monomorphization());
36-
assert_eq!(mono.len(), 3, "Expected 3 mono functions");
38+
assert_eq!(mono.len(), 4, "Expected 3 mono functions");
3739
assert_eq!(generic.len(), 2, "Expected 2 generic functions");
3840

3941
// For all monomorphic items, get the correspondent instances.

0 commit comments

Comments
 (0)