Skip to content

Commit 3b47d33

Browse files
authored
Rollup merge of #85800 - BoxyUwU:const-param-default-diagnostics, r=oli-obk
Fix some diagnostic issues with const_generics_defaults feature gate This PR makes a few changes: - print out const param defaults in "lifetime ordering" errors rather than discarding them - update `is_simple_text` to account for const params when checking if a type has no generics, this was causing a note to be failed to add to an error message - fixes some diagnostic wording that incorrectly said there was ordering restrictions between type/const params despite the `const_generics_defaults` feature gate is active
2 parents dddebf9 + 47fe696 commit 3b47d33

File tree

8 files changed

+47
-16
lines changed

8 files changed

+47
-16
lines changed

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -938,8 +938,11 @@ fn validate_generic_param_order(
938938
}
939939
GenericParamKind::Type { default: None } => (),
940940
GenericParamKind::Lifetime => (),
941-
// FIXME(const_generics_defaults)
942-
GenericParamKind::Const { ty: _, kw_span: _, default: _ } => (),
941+
GenericParamKind::Const { ty: _, kw_span: _, default: Some(default) } => {
942+
ordered_params += " = ";
943+
ordered_params += &pprust::expr_to_string(&*default.value);
944+
}
945+
GenericParamKind::Const { ty: _, kw_span: _, default: None } => (),
943946
}
944947
first = false;
945948
}
@@ -959,7 +962,7 @@ fn validate_generic_param_order(
959962
span,
960963
&format!(
961964
"reorder the parameters: lifetimes, {}",
962-
if sess.features_untracked().const_generics {
965+
if sess.features_untracked().unordered_const_ty_params() {
963966
"then consts and types"
964967
} else {
965968
"then types, then consts"

compiler/rustc_middle/src/ty/diagnostics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ impl<'tcx> TyS<'tcx> {
5454
/// ADTs with no type arguments.
5555
pub fn is_simple_text(&self) -> bool {
5656
match self.kind() {
57-
Adt(_, substs) => substs.types().next().is_none(),
57+
Adt(_, substs) => substs.non_erasable_generics().next().is_none(),
5858
Ref(_, ty, _) => ty.is_simple_text(),
5959
_ => self.is_simple_ty(),
6060
}

src/test/ui/const-generics/defaults/intermixed-lifetime.min.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ error: lifetime parameters must be declared prior to const parameters
22
--> $DIR/intermixed-lifetime.rs:7:28
33
|
44
LL | struct Foo<const N: usize, 'a, T = u32>(&'a (), T);
5-
| -----------------^^---------- help: reorder the parameters: lifetimes, then types, then consts: `<'a, const N: usize, T = u32>`
5+
| -----------------^^---------- help: reorder the parameters: lifetimes, then consts and types: `<'a, const N: usize, T = u32>`
66

77
error: lifetime parameters must be declared prior to type parameters
88
--> $DIR/intermixed-lifetime.rs:10:37
99
|
1010
LL | struct Bar<const N: usize, T = u32, 'a>(&'a (), T);
11-
| --------------------------^^- help: reorder the parameters: lifetimes, then types, then consts: `<'a, const N: usize, T = u32>`
11+
| --------------------------^^- help: reorder the parameters: lifetimes, then consts and types: `<'a, const N: usize, T = u32>`
1212

1313
error: aborting due to 2 previous errors
1414

src/test/ui/const-generics/defaults/mismatch.full.stderr

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ LL | let e: Example::<13> = ();
55
| ------------- ^^ expected struct `Example`, found `()`
66
| |
77
| expected due to this
8+
|
9+
= note: expected struct `Example`
10+
found unit type `()`
811

912
error[E0308]: mismatched types
10-
--> $DIR/mismatch.rs:14:34
13+
--> $DIR/mismatch.rs:15:34
1114
|
1215
LL | let e: Example2::<u32, 13> = ();
1316
| ------------------- ^^ expected struct `Example2`, found `()`
@@ -18,7 +21,7 @@ LL | let e: Example2::<u32, 13> = ();
1821
found unit type `()`
1922

2023
error[E0308]: mismatched types
21-
--> $DIR/mismatch.rs:16:34
24+
--> $DIR/mismatch.rs:18:34
2225
|
2326
LL | let e: Example3::<13, u32> = ();
2427
| ------------------- ^^ expected struct `Example3`, found `()`
@@ -29,7 +32,7 @@ LL | let e: Example3::<13, u32> = ();
2932
found unit type `()`
3033

3134
error[E0308]: mismatched types
32-
--> $DIR/mismatch.rs:18:28
35+
--> $DIR/mismatch.rs:21:28
3336
|
3437
LL | let e: Example3::<7> = ();
3538
| ------------- ^^ expected struct `Example3`, found `()`
@@ -40,12 +43,15 @@ LL | let e: Example3::<7> = ();
4043
found unit type `()`
4144

4245
error[E0308]: mismatched types
43-
--> $DIR/mismatch.rs:22:28
46+
--> $DIR/mismatch.rs:24:28
4447
|
4548
LL | let e: Example4::<7> = ();
4649
| ------------- ^^ expected struct `Example4`, found `()`
4750
| |
4851
| expected due to this
52+
|
53+
= note: expected struct `Example4<7_usize>`
54+
found unit type `()`
4955

5056
error: aborting due to 5 previous errors
5157

src/test/ui/const-generics/defaults/mismatch.min.stderr

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ LL | let e: Example::<13> = ();
55
| ------------- ^^ expected struct `Example`, found `()`
66
| |
77
| expected due to this
8+
|
9+
= note: expected struct `Example`
10+
found unit type `()`
811

912
error[E0308]: mismatched types
10-
--> $DIR/mismatch.rs:14:34
13+
--> $DIR/mismatch.rs:15:34
1114
|
1215
LL | let e: Example2::<u32, 13> = ();
1316
| ------------------- ^^ expected struct `Example2`, found `()`
@@ -18,7 +21,7 @@ LL | let e: Example2::<u32, 13> = ();
1821
found unit type `()`
1922

2023
error[E0308]: mismatched types
21-
--> $DIR/mismatch.rs:16:34
24+
--> $DIR/mismatch.rs:18:34
2225
|
2326
LL | let e: Example3::<13, u32> = ();
2427
| ------------------- ^^ expected struct `Example3`, found `()`
@@ -29,7 +32,7 @@ LL | let e: Example3::<13, u32> = ();
2932
found unit type `()`
3033

3134
error[E0308]: mismatched types
32-
--> $DIR/mismatch.rs:18:28
35+
--> $DIR/mismatch.rs:21:28
3336
|
3437
LL | let e: Example3::<7> = ();
3538
| ------------- ^^ expected struct `Example3`, found `()`
@@ -40,12 +43,15 @@ LL | let e: Example3::<7> = ();
4043
found unit type `()`
4144

4245
error[E0308]: mismatched types
43-
--> $DIR/mismatch.rs:22:28
46+
--> $DIR/mismatch.rs:24:28
4447
|
4548
LL | let e: Example4::<7> = ();
4649
| ------------- ^^ expected struct `Example4`, found `()`
4750
| |
4851
| expected due to this
52+
|
53+
= note: expected struct `Example4<7_usize>`
54+
found unit type `()`
4955

5056
error: aborting due to 5 previous errors
5157

src/test/ui/const-generics/defaults/mismatch.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,17 @@ pub struct Example4<const N: usize=13, const M: usize=4>;
1111
fn main() {
1212
let e: Example::<13> = ();
1313
//~^ Error: mismatched types
14+
//~| expected struct `Example`
1415
let e: Example2::<u32, 13> = ();
1516
//~^ Error: mismatched types
17+
//~| expected struct `Example2`
1618
let e: Example3::<13, u32> = ();
1719
//~^ Error: mismatched types
20+
//~| expected struct `Example3`
1821
let e: Example3::<7> = ();
1922
//~^ Error: mismatched types
20-
// FIXME(const_generics_defaults): There should be a note for the error below, but it is
21-
// missing.
23+
//~| expected struct `Example3<7_usize>`
2224
let e: Example4::<7> = ();
2325
//~^ Error: mismatched types
26+
//~| expected struct `Example4<7_usize>`
2427
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#![feature(const_generics_defaults)]
2+
struct Foo<const M: usize = 10, 'a>(&'a u32);
3+
//~^ Error lifetime parameters must be declared prior to const parameters
4+
5+
fn main() {}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: lifetime parameters must be declared prior to const parameters
2+
--> $DIR/param-order-err-pretty-prints-default.rs:2:33
3+
|
4+
LL | struct Foo<const M: usize = 10, 'a>(&'a u32);
5+
| ----------------------^^- help: reorder the parameters: lifetimes, then consts and types: `<'a, const M: usize = 10>`
6+
7+
error: aborting due to previous error
8+

0 commit comments

Comments
 (0)