Skip to content

Commit 09bfb30

Browse files
committed
Add fix to error msg which relied on order
Previously types needed to be ordered before consts. Since relaxing that restriction, this created a bug wherever relying on that. This produces a strange error message, so it's not awful but should still be fixed. Add ParamKindOrd instead of &str Switches from using strings to ParamKindOrd, as dealing with enums provides better guarantees to not miss things. Add test Add new loop for checking types/consts Update to check const & type in 1 loop
1 parent a601302 commit 09bfb30

14 files changed

+782
-160
lines changed

compiler/rustc_middle/src/ty/mod.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,8 @@ pub struct GenericParamCount {
826826
pub lifetimes: usize,
827827
pub types: usize,
828828
pub consts: usize,
829+
830+
pub type_defaults: usize,
829831
}
830832

831833
/// Information about the formal type/lifetime parameters associated
@@ -861,7 +863,10 @@ impl<'tcx> Generics {
861863
for param in &self.params {
862864
match param.kind {
863865
GenericParamDefKind::Lifetime => own_counts.lifetimes += 1,
864-
GenericParamDefKind::Type { .. } => own_counts.types += 1,
866+
GenericParamDefKind::Type { has_default, .. } => {
867+
own_counts.types += 1;
868+
own_counts.type_defaults += has_default as usize;
869+
}
865870
GenericParamDefKind::Const => own_counts.consts += 1,
866871
};
867872
}

compiler/rustc_typeck/src/astconv/generics.rs

+249-145
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Check that ordering of errors is correctly reported even with consts preceding types.
2+
3+
#![feature(const_generics)]
4+
#![allow(incomplete_features)]
5+
6+
struct Example<'a, const N: usize, T=f32> {
7+
s: &'a T,
8+
}
9+
10+
type Consts = Example<3, 3, 3>;
11+
//~^ ERROR missing lifetime specifier
12+
//~| ERROR wrong number of const arguments
13+
type Types = Example<f32, f32, f32>;
14+
//~^ ERROR missing lifetime specifier
15+
//~| ERROR wrong number of const arguments
16+
//~| ERROR wrong number of type arguments
17+
type Lifetimes = Example<'static, 'static, 'static>;
18+
//~^ ERROR wrong number of const arguments
19+
//~| ERROR misplaced type arguments
20+
//~| wrong number of lifetime
21+
22+
type LtConst1 = Example<'static, 3, 3>;
23+
//~^ ERROR wrong number of const arguments
24+
type LtConst2 = Example<3, 'static, 3>;
25+
//~^ ERROR wrong number of const arguments
26+
type LtConst3 = Example<3, 3, 'static>;
27+
//~^ ERROR misplaced type arguments
28+
29+
type LtTy1 = Example<'static, f32, f32>;
30+
//~^ ERROR wrong number of const arguments
31+
//~| ERROR wrong number of type arguments
32+
type LtTy2 = Example<f32, 'static, f32>;
33+
//~^ ERROR wrong number of const arguments
34+
//~| ERROR wrong number of type arguments
35+
type LtTy3 = Example<f32, f32, 'static>;
36+
//~^ ERROR wrong number of const arguments
37+
//~| ERROR wrong number of type arguments
38+
39+
type ConstTy1 = Example<3, f32, f32>;
40+
//~^ ERROR missing lifetime specifier
41+
//~| ERROR wrong number of type arguments
42+
type ConstTy2 = Example<f32, 3, f32>;
43+
//~^ ERROR missing lifetime specifier
44+
//~| ERROR wrong number of type arguments
45+
type ConstTy3 = Example<f32, f32, 3>;
46+
//~^ ERROR missing lifetime specifier
47+
//~| ERROR wrong number of type arguments
48+
49+
type ConstLt1 = Example<3, 'static, 'static>;
50+
//~^ ERROR wrong number of lifetime
51+
type ConstLt2 = Example<'static, 3, 'static>;
52+
//~^ ERROR wrong number of lifetime
53+
type ConstLt3 = Example<'static, 'static, 3>;
54+
//~^ ERROR wrong number of lifetime
55+
56+
type TyLt1 = Example<f32, 'static, 'static>;
57+
//~^ ERROR wrong number of lifetime
58+
//~| ERROR wrong number of const
59+
//~| ERROR misplaced type arguments
60+
type TyLt2 = Example<'static, f32, 'static>;
61+
//~^ ERROR wrong number of lifetime
62+
//~| ERROR wrong number of const
63+
//~| ERROR misplaced type arguments
64+
type TyLt3 = Example<'static, 'static, f32>;
65+
//~^ ERROR wrong number of const
66+
//~| ERROR wrong number of lifetime
67+
68+
type TyConst1 = Example<f32, 3, 3>;
69+
//~^ ERROR missing lifetime specifier
70+
//~| ERROR wrong number of const
71+
//~| ERROR misplaced type arguments
72+
type TyConst2 = Example<3, f32, 3>;
73+
//~^ ERROR missing lifetime specifier
74+
//~| ERROR wrong number of const
75+
type TyConst3 = Example<3, 3, f32>;
76+
//~^ ERROR missing lifetime specifier
77+
//~| ERROR wrong number of const
78+
//~| ERROR misplaced type arguments
79+
80+
type Intermixed1 = Example<'static, 3, f32>; // ok
81+
82+
83+
type Intermixed2 = Example<f32, 'static, 3>;
84+
//~^ ERROR type provided when a constant was expected
85+
type Intermixed3 = Example<3, f32, 'static>;
86+
//~^ ERROR constant provided when a lifetime
87+
88+
fn main() {}

0 commit comments

Comments
 (0)