Skip to content

Commit adb2ac0

Browse files
committed
Mark all extraneous generic args as errors
1 parent 2e3842b commit adb2ac0

8 files changed

+77
-63
lines changed

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

+19
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,25 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
442442
) -> ty::GenericArg<'tcx> {
443443
let tcx = self.lowerer.tcx();
444444

445+
if let Err(incorrect) = self.incorrect_args {
446+
if incorrect.invalid_args.contains(&(param.index as usize)) {
447+
return match param.kind {
448+
GenericParamDefKind::Lifetime => {
449+
ty::Region::new_error(tcx, incorrect.reported).into()
450+
}
451+
GenericParamDefKind::Type { .. } => {
452+
Ty::new_error(tcx, incorrect.reported).into()
453+
}
454+
GenericParamDefKind::Const { .. } => ty::Const::new_error(
455+
tcx,
456+
incorrect.reported,
457+
Ty::new_error(tcx, incorrect.reported),
458+
)
459+
.into(),
460+
};
461+
}
462+
}
463+
445464
let mut handle_ty_args = |has_default, ty: &hir::Ty<'tcx>| {
446465
if has_default {
447466
tcx.check_optional_stability(

tests/crashes/121134.rs

-20
This file was deleted.

tests/ui/traits/associated_type_bound/116464-invalid-assoc-type-suggestion-in-trait-impl.rs

-2
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,12 @@ impl<T, S> Trait<T> for i32 {
1414
// Should not not trigger suggestion here...
1515
impl<T, S> Trait<T, S> for () {}
1616
//~^ ERROR trait takes 1 generic argument but 2 generic arguments were supplied
17-
//~| ERROR `S` is not constrained
1817

1918
//... but should do so in all of the below cases except the last one
2019
fn func<T: Trait<u32, String>>(t: T) -> impl Trait<(), i32> {
2120
//~^ ERROR trait takes 1 generic argument but 2 generic arguments were supplied
2221
//~| ERROR trait takes 1 generic argument but 2 generic arguments were supplied
2322
//~| ERROR trait takes 1 generic argument but 2 generic arguments were supplied
24-
//~| ERROR type annotations needed
2523
3
2624
}
2725

tests/ui/traits/associated_type_bound/116464-invalid-assoc-type-suggestion-in-trait-impl.stderr

+10-22
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,8 @@ note: trait defined here, with 1 generic parameter: `T`
1616
LL | pub trait Trait<T> {
1717
| ^^^^^ -
1818

19-
error[E0207]: the type parameter `S` is not constrained by the impl trait, self type, or predicates
20-
--> $DIR/116464-invalid-assoc-type-suggestion-in-trait-impl.rs:15:9
21-
|
22-
LL | impl<T, S> Trait<T, S> for () {}
23-
| ^ unconstrained type parameter
24-
2519
error[E0107]: trait takes 1 generic argument but 2 generic arguments were supplied
26-
--> $DIR/116464-invalid-assoc-type-suggestion-in-trait-impl.rs:20:12
20+
--> $DIR/116464-invalid-assoc-type-suggestion-in-trait-impl.rs:19:12
2721
|
2822
LL | fn func<T: Trait<u32, String>>(t: T) -> impl Trait<(), i32> {
2923
| ^^^^^ expected 1 generic argument
@@ -39,7 +33,7 @@ LL | fn func<T: Trait<u32, Assoc = String>>(t: T) -> impl Trait<(), i32> {
3933
| +++++++
4034

4135
error[E0107]: trait takes 1 generic argument but 2 generic arguments were supplied
42-
--> $DIR/116464-invalid-assoc-type-suggestion-in-trait-impl.rs:20:46
36+
--> $DIR/116464-invalid-assoc-type-suggestion-in-trait-impl.rs:19:46
4337
|
4438
LL | fn func<T: Trait<u32, String>>(t: T) -> impl Trait<(), i32> {
4539
| ^^^^^ expected 1 generic argument
@@ -55,7 +49,7 @@ LL | fn func<T: Trait<u32, String>>(t: T) -> impl Trait<(), Assoc = i32> {
5549
| +++++++
5650

5751
error[E0107]: trait takes 1 generic argument but 2 generic arguments were supplied
58-
--> $DIR/116464-invalid-assoc-type-suggestion-in-trait-impl.rs:20:46
52+
--> $DIR/116464-invalid-assoc-type-suggestion-in-trait-impl.rs:19:46
5953
|
6054
LL | fn func<T: Trait<u32, String>>(t: T) -> impl Trait<(), i32> {
6155
| ^^^^^ expected 1 generic argument
@@ -71,14 +65,8 @@ help: replace the generic bound with the associated type
7165
LL | fn func<T: Trait<u32, String>>(t: T) -> impl Trait<(), Assoc = i32> {
7266
| +++++++
7367

74-
error[E0282]: type annotations needed
75-
--> $DIR/116464-invalid-assoc-type-suggestion-in-trait-impl.rs:20:41
76-
|
77-
LL | fn func<T: Trait<u32, String>>(t: T) -> impl Trait<(), i32> {
78-
| ^^^^^^^^^^^^^^^^^^^ cannot infer type
79-
8068
error[E0107]: trait takes 1 generic argument but 2 generic arguments were supplied
81-
--> $DIR/116464-invalid-assoc-type-suggestion-in-trait-impl.rs:28:18
69+
--> $DIR/116464-invalid-assoc-type-suggestion-in-trait-impl.rs:26:18
8270
|
8371
LL | struct Struct<T: Trait<u32, String>> {
8472
| ^^^^^ expected 1 generic argument
@@ -94,7 +82,7 @@ LL | struct Struct<T: Trait<u32, Assoc = String>> {
9482
| +++++++
9583

9684
error[E0107]: trait takes 1 generic argument but 2 generic arguments were supplied
97-
--> $DIR/116464-invalid-assoc-type-suggestion-in-trait-impl.rs:33:23
85+
--> $DIR/116464-invalid-assoc-type-suggestion-in-trait-impl.rs:31:23
9886
|
9987
LL | trait AnotherTrait<T: Trait<T, i32>> {}
10088
| ^^^^^ expected 1 generic argument
@@ -110,7 +98,7 @@ LL | trait AnotherTrait<T: Trait<T, Assoc = i32>> {}
11098
| +++++++
11199

112100
error[E0107]: trait takes 1 generic argument but 2 generic arguments were supplied
113-
--> $DIR/116464-invalid-assoc-type-suggestion-in-trait-impl.rs:36:9
101+
--> $DIR/116464-invalid-assoc-type-suggestion-in-trait-impl.rs:34:9
114102
|
115103
LL | impl<T: Trait<u32, String>> Struct<T> {}
116104
| ^^^^^ expected 1 generic argument
@@ -126,20 +114,20 @@ LL | impl<T: Trait<u32, Assoc = String>> Struct<T> {}
126114
| +++++++
127115

128116
error[E0107]: struct takes 1 generic argument but 2 generic arguments were supplied
129-
--> $DIR/116464-invalid-assoc-type-suggestion-in-trait-impl.rs:42:58
117+
--> $DIR/116464-invalid-assoc-type-suggestion-in-trait-impl.rs:40:58
130118
|
131119
LL | impl<T: Trait<u32, Assoc=String>, U> YetAnotherTrait for Struct<T, U> {}
132120
| ^^^^^^ - help: remove this generic argument
133121
| |
134122
| expected 1 generic argument
135123
|
136124
note: struct defined here, with 1 generic parameter: `T`
137-
--> $DIR/116464-invalid-assoc-type-suggestion-in-trait-impl.rs:28:8
125+
--> $DIR/116464-invalid-assoc-type-suggestion-in-trait-impl.rs:26:8
138126
|
139127
LL | struct Struct<T: Trait<u32, String>> {
140128
| ^^^^^^ -
141129

142-
error: aborting due to 11 previous errors
130+
error: aborting due to 9 previous errors
143131

144-
Some errors have detailed explanations: E0107, E0207, E0282.
132+
Some errors have detailed explanations: E0107, E0207.
145133
For more information about an error, try `rustc --explain E0107`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//! This test used to ICE: #121134
2+
//! The issue is that we're trying to prove a projection, but there's
3+
//! no bound for the projection's trait, and the projection has the wrong
4+
//! kind of generic parameter (lifetime vs type).
5+
//! When actually calling the function with those broken bounds, trying to
6+
//! instantiate the bounds with inference vars would ICE.
7+
#![feature(unboxed_closures)]
8+
9+
trait Output<'a> {
10+
type Type;
11+
}
12+
13+
struct Wrapper;
14+
15+
impl Wrapper {
16+
fn do_something_wrapper<O, F>(&mut self, _: F)
17+
where
18+
F: for<'a> FnOnce(<F as Output<i32>>::Type),
19+
//~^ ERROR: trait takes 0 generic arguments but 1 generic argument was supplied
20+
{
21+
}
22+
}
23+
24+
fn main() {
25+
let mut wrapper = Wrapper;
26+
wrapper.do_something_wrapper(|value| ());
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error[E0107]: trait takes 0 generic arguments but 1 generic argument was supplied
2+
--> $DIR/generic_param_mismatch_in_unsatisfied_projection.rs:18:33
3+
|
4+
LL | F: for<'a> FnOnce(<F as Output<i32>>::Type),
5+
| ^^^^^^ expected 0 generic arguments
6+
|
7+
note: trait defined here, with 0 generic parameters
8+
--> $DIR/generic_param_mismatch_in_unsatisfied_projection.rs:9:7
9+
|
10+
LL | trait Output<'a> {
11+
| ^^^^^^
12+
help: replace the generic bound with the associated type
13+
|
14+
LL | F: for<'a> FnOnce(<F as Output<Type = i32>>::Type),
15+
| ++++++
16+
17+
error: aborting due to 1 previous error
18+
19+
For more information about this error, try `rustc --explain E0107`.

tests/ui/transmutability/issue-101739-2.rs

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ mod assert {
1515
>()
1616
where
1717
Dst: BikeshedIntrinsicFrom< //~ ERROR trait takes at most 2 generic arguments but 5 generic arguments were supplied
18-
//~^ ERROR: the constant `ASSUME_ALIGNMENT` is not of type `Assume`
1918
Src,
2019
ASSUME_ALIGNMENT, //~ ERROR: mismatched types
2120
ASSUME_LIFETIMES,

tests/ui/transmutability/issue-101739-2.stderr

+2-18
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,13 @@ LL | | ASSUME_VALIDITY,
99
LL | | ASSUME_VISIBILITY,
1010
| |_____________________________- help: remove these generic arguments
1111

12-
error: the constant `ASSUME_ALIGNMENT` is not of type `Assume`
13-
--> $DIR/issue-101739-2.rs:17:14
14-
|
15-
LL | Dst: BikeshedIntrinsicFrom<
16-
| ______________^
17-
LL | |
18-
LL | | Src,
19-
LL | | ASSUME_ALIGNMENT,
20-
... |
21-
LL | | ASSUME_VISIBILITY,
22-
LL | | >,
23-
| |_________^ expected `Assume`, found `bool`
24-
|
25-
note: required by a bound in `BikeshedIntrinsicFrom`
26-
--> $SRC_DIR/core/src/mem/transmutability.rs:LL:COL
27-
2812
error[E0308]: mismatched types
29-
--> $DIR/issue-101739-2.rs:20:13
13+
--> $DIR/issue-101739-2.rs:19:13
3014
|
3115
LL | ASSUME_ALIGNMENT,
3216
| ^^^^^^^^^^^^^^^^ expected `Assume`, found `bool`
3317

34-
error: aborting due to 3 previous errors
18+
error: aborting due to 2 previous errors
3519

3620
Some errors have detailed explanations: E0107, E0308.
3721
For more information about an error, try `rustc --explain E0107`.

0 commit comments

Comments
 (0)