Skip to content

Commit c0bedb9

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 05b8bcc + b8eedfa commit c0bedb9

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

+3-1
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

+1-1
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

+4-3
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

+2
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

+22-10
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

+9-7
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

+1
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

+1-1
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

-10
This file was deleted.

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

+6-4
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.

tests/ui/coherence/negative-coherence/generic_const_type_mismatch.rs

+2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
#![feature(with_negative_coherence)]
66
trait Trait {}
77
impl<const N: u8> Trait for [(); N] {}
8+
//~^ ERROR: mismatched types
89
impl<const N: i8> Trait for [(); N] {}
910
//~^ ERROR: conflicting implementations of trait `Trait`
11+
//~| ERROR: mismatched types
1012

1113
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
11
error[E0119]: conflicting implementations of trait `Trait` for type `[(); _]`
2-
--> $DIR/generic_const_type_mismatch.rs:8:1
2+
--> $DIR/generic_const_type_mismatch.rs:9:1
33
|
44
LL | impl<const N: u8> Trait for [(); N] {}
55
| ----------------------------------- first implementation here
6+
LL |
67
LL | impl<const N: i8> Trait for [(); N] {}
78
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `[(); _]`
89

9-
error: aborting due to 1 previous error
10+
error[E0308]: mismatched types
11+
--> $DIR/generic_const_type_mismatch.rs:7:34
12+
|
13+
LL | impl<const N: u8> Trait for [(); N] {}
14+
| ^ expected `usize`, found `u8`
15+
16+
error[E0308]: mismatched types
17+
--> $DIR/generic_const_type_mismatch.rs:9:34
18+
|
19+
LL | impl<const N: i8> Trait for [(); N] {}
20+
| ^ expected `usize`, found `i8`
21+
22+
error: aborting due to 3 previous errors
1023

11-
For more information about this error, try `rustc --explain E0119`.
24+
Some errors have detailed explanations: E0119, E0308.
25+
For more information about an error, try `rustc --explain E0119`.

tests/ui/const-generics/bad-subst-const-kind.rs

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ trait Q {
77

88
impl<const N: u64> Q for [u8; N] {
99
//~^ ERROR: the constant `N` is not of type `usize`
10+
//~| ERROR: mismatched types
1011
const ASSOC: usize = 1;
1112
}
1213

tests/ui/const-generics/bad-subst-const-kind.stderr

+9-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | impl<const N: u64> Q for [u8; N] {
55
| ^^^^^^^ expected `usize`, found `u64`
66

77
error: the constant `13` is not of type `u64`
8-
--> $DIR/bad-subst-const-kind.rs:13:24
8+
--> $DIR/bad-subst-const-kind.rs:14:24
99
|
1010
LL | pub fn test() -> [u8; <[u8; 13] as Q>::ASSOC] {
1111
| ^^^^^^^^ expected `u64`, found `usize`
@@ -18,5 +18,12 @@ LL | impl<const N: u64> Q for [u8; N] {
1818
| |
1919
| unsatisfied trait bound introduced here
2020

21-
error: aborting due to 2 previous errors
21+
error[E0308]: mismatched types
22+
--> $DIR/bad-subst-const-kind.rs:8:31
23+
|
24+
LL | impl<const N: u64> Q for [u8; N] {
25+
| ^ expected `usize`, found `u64`
26+
27+
error: aborting due to 3 previous errors
2228

29+
For more information about this error, try `rustc --explain E0308`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//@ check-pass
2+
3+
// This is a regression test for #128016.
4+
5+
macro_rules! len_inner {
6+
() => {
7+
BAR
8+
};
9+
}
10+
11+
macro_rules! len {
12+
() => {
13+
len_inner!()
14+
};
15+
}
16+
17+
const BAR: usize = 0;
18+
19+
fn main() {
20+
let val: [bool; len!()] = [];
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//@ check-pass
2+
3+
macro_rules! len {
4+
($x:ident) => {
5+
$x
6+
};
7+
}
8+
9+
fn bar<const N: usize>() {
10+
let val: [bool; len!(N)] = [true; N];
11+
}
12+
13+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// This is a regression test for #128016.
2+
3+
macro_rules! len {
4+
() => {
5+
target
6+
//~^ ERROR cannot find value `target`
7+
};
8+
}
9+
10+
fn main() {
11+
let val: [str; len!()] = [];
12+
//~^ ERROR the size for values
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
error[E0425]: cannot find value `target` in this scope
2+
--> $DIR/trivial-const-arg-macro-res-error.rs:5:9
3+
|
4+
LL | target
5+
| ^^^^^^ not found in this scope
6+
...
7+
LL | let val: [str; len!()] = [];
8+
| ------ in this macro invocation
9+
|
10+
= note: this error originates in the macro `len` (in Nightly builds, run with -Z macro-backtrace for more info)
11+
12+
error[E0277]: the size for values of type `str` cannot be known at compilation time
13+
--> $DIR/trivial-const-arg-macro-res-error.rs:11:14
14+
|
15+
LL | let val: [str; len!()] = [];
16+
| ^^^^^^^^^^^^^ doesn't have a size known at compile-time
17+
|
18+
= help: the trait `Sized` is not implemented for `str`
19+
= note: slice and array elements must have `Sized` type
20+
21+
error: aborting due to 2 previous errors
22+
23+
Some errors have detailed explanations: E0277, E0425.
24+
For more information about an error, try `rustc --explain E0277`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//@ check-pass
2+
3+
// This is a regression test for #128016.
4+
5+
macro_rules! len {
6+
() => {
7+
BAR
8+
};
9+
}
10+
11+
const BAR: usize = 0;
12+
13+
fn main() {
14+
let val: [bool; len!()] = [];
15+
}

tests/ui/const-generics/generic_const_exprs/type_mismatch.rs

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ trait Q {
88
impl<const N: u64> Q for [u8; N] {}
99
//~^ ERROR not all trait items implemented
1010
//~| ERROR the constant `N` is not of type `usize`
11+
//~| ERROR mismatched types
1112

1213
pub fn q_user() -> [u8; <[u8; 13] as Q>::ASSOC] {}
1314
//~^ ERROR the constant `13` is not of type `u64`

tests/ui/const-generics/generic_const_exprs/type_mismatch.stderr

+9-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ LL | impl<const N: u64> Q for [u8; N] {}
1414
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ missing `ASSOC` in implementation
1515

1616
error: the constant `13` is not of type `u64`
17-
--> $DIR/type_mismatch.rs:12:26
17+
--> $DIR/type_mismatch.rs:13:26
1818
|
1919
LL | pub fn q_user() -> [u8; <[u8; 13] as Q>::ASSOC] {}
2020
| ^^^^^^^^ expected `u64`, found `usize`
@@ -28,14 +28,20 @@ LL | impl<const N: u64> Q for [u8; N] {}
2828
| unsatisfied trait bound introduced here
2929

3030
error[E0308]: mismatched types
31-
--> $DIR/type_mismatch.rs:12:20
31+
--> $DIR/type_mismatch.rs:13:20
3232
|
3333
LL | pub fn q_user() -> [u8; <[u8; 13] as Q>::ASSOC] {}
3434
| ------ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `[u8; <[u8; 13] as Q>::ASSOC]`, found `()`
3535
| |
3636
| implicitly returns `()` as its body has no tail or `return` expression
3737

38-
error: aborting due to 4 previous errors
38+
error[E0308]: mismatched types
39+
--> $DIR/type_mismatch.rs:8:31
40+
|
41+
LL | impl<const N: u64> Q for [u8; N] {}
42+
| ^ expected `usize`, found `u64`
43+
44+
error: aborting due to 5 previous errors
3945

4046
Some errors have detailed explanations: E0046, E0308.
4147
For more information about an error, try `rustc --explain E0046`.

0 commit comments

Comments
 (0)