Skip to content

Commit ad63f90

Browse files
committed
Make output more specific
1 parent 196a30e commit ad63f90

28 files changed

+391
-217
lines changed

compiler/rustc_typeck/src/astconv/mod.rs

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2108,6 +2108,46 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
21082108
extend: impl Fn(&mut DiagnosticBuilder<'tcx, ErrorGuaranteed>),
21092109
) -> bool {
21102110
let args = segments.clone().flat_map(|segment| segment.args().args);
2111+
let types_and_spans: Vec<_> = segments
2112+
.clone()
2113+
.flat_map(|segment| {
2114+
segment.res.and_then(|res| {
2115+
if segment.args().args.is_empty() {
2116+
None
2117+
} else {
2118+
let mut desc = res.descr();
2119+
if desc == "unresolved item" {
2120+
desc = "this type";
2121+
};
2122+
2123+
let name = match res {
2124+
Res::PrimTy(ty) => Some(ty.name()),
2125+
Res::Def(_, def_id) => self.tcx().opt_item_name(def_id),
2126+
_ => None,
2127+
};
2128+
Some((
2129+
match name {
2130+
Some(ty) => format!("{desc} `{ty}`"),
2131+
None => desc.to_string(),
2132+
},
2133+
segment.ident.span,
2134+
))
2135+
}
2136+
})
2137+
})
2138+
.collect();
2139+
let this_type = match &types_and_spans[..] {
2140+
[.., _, (last, _)] => format!(
2141+
"{} and {last}",
2142+
types_and_spans[..types_and_spans.len() - 1]
2143+
.iter()
2144+
.map(|(x, _)| x.as_str())
2145+
.intersperse(&", ")
2146+
.collect::<String>()
2147+
),
2148+
[(only, _)] => only.to_string(),
2149+
[] => "this type".to_string(),
2150+
};
21112151

21122152
let (lt, ty, ct, inf) =
21132153
args.clone().fold((false, false, false, false), |(lt, ty, ct, inf), arg| match arg {
@@ -2143,7 +2183,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
21432183
let (kind, s) = match types[..] {
21442184
[.., _, last] => (
21452185
format!(
2146-
"{} and `{last}`",
2186+
"{} and {last}",
21472187
types[..types.len() - 1]
21482188
.iter()
21492189
.map(|&x| x)
@@ -2161,9 +2201,12 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
21612201
self.tcx().sess,
21622202
span,
21632203
E0109,
2164-
"{kind} arguments are not allowed for this type",
2204+
"{kind} arguments are not allowed on {this_type}",
21652205
);
21662206
err.span_label(last_span, format!("{kind} argument{s} not allowed"));
2207+
for (_, span) in types_and_spans {
2208+
err.span_label(span, "not allowed on this");
2209+
}
21672210
extend(&mut err);
21682211
err.emit();
21692212
emitted = true;

src/test/ui/derives/issue-97343.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::fmt::Debug;
22

33
#[derive(Debug)]
4-
pub struct Irrelevant<Irrelevant> { //~ ERROR type arguments are not allowed for this type
4+
pub struct Irrelevant<Irrelevant> { //~ ERROR type arguments are not allowed on type parameter
55
irrelevant: Irrelevant,
66
}
77

src/test/ui/derives/issue-97343.stderr

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
error[E0109]: type arguments are not allowed for this type
1+
error[E0109]: type arguments are not allowed on type parameter `Irrelevant`
22
--> $DIR/issue-97343.rs:4:23
33
|
44
LL | #[derive(Debug)]
5-
| ----- in this derive macro expansion
5+
| -----
6+
| |
7+
| not allowed on this
8+
| in this derive macro expansion
69
LL | pub struct Irrelevant<Irrelevant> {
710
| ^^^^^^^^^^ type argument not allowed
811
|

src/test/ui/error-codes/E0109.stderr

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
error[E0109]: type arguments are not allowed for this type
1+
error[E0109]: type arguments are not allowed on this type
22
--> $DIR/E0109.rs:1:14
33
|
44
LL | type X = u32<i32>;
5-
| ^^^ type argument not allowed
5+
| --- ^^^ type argument not allowed
6+
| |
7+
| not allowed on this
68
|
79
help: primitive type `u32` doesn't have type parameters
810
|

src/test/ui/error-codes/E0110.stderr

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
error[E0109]: lifetime arguments are not allowed for this type
1+
error[E0109]: lifetime arguments are not allowed on this type
22
--> $DIR/E0110.rs:1:14
33
|
44
LL | type X = u32<'static>;
5-
| ^^^^^^^ lifetime argument not allowed
5+
| --- ^^^^^^^ lifetime argument not allowed
6+
| |
7+
| not allowed on this
68
|
79
help: primitive type `u32` doesn't have type parameters
810
|

src/test/ui/issues/issue-22706.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
fn is_copy<T: ::std::marker<i32>::Copy>() {}
2-
//~^ ERROR type arguments are not allowed for this type [E0109]
2+
//~^ ERROR type arguments are not allowed on module `marker` [E0109]
33
fn main() {}

src/test/ui/issues/issue-22706.stderr

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
error[E0109]: type arguments are not allowed for this type
1+
error[E0109]: type arguments are not allowed on module `marker`
22
--> $DIR/issue-22706.rs:1:29
33
|
44
LL | fn is_copy<T: ::std::marker<i32>::Copy>() {}
5-
| ^^^ type argument not allowed
5+
| ------ ^^^ type argument not allowed
6+
| |
7+
| not allowed on this
68

79
error: aborting due to previous error
810

src/test/ui/issues/issue-57924.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ pub struct Gcm<E>(E);
33
impl<E> Gcm<E> {
44
pub fn crash(e: E) -> Self {
55
Self::<E>(e)
6-
//~^ ERROR type arguments are not allowed for this type
6+
//~^ ERROR type arguments are not allowed on self constructor
77
}
88
}
99

src/test/ui/issues/issue-57924.stderr

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
error[E0109]: type arguments are not allowed for this type
1+
error[E0109]: type arguments are not allowed on self constructor
22
--> $DIR/issue-57924.rs:5:16
33
|
44
LL | Self::<E>(e)
5-
| ^ type argument not allowed
5+
| ---- ^ type argument not allowed
6+
| |
7+
| not allowed on this
68

79
error: aborting due to previous error
810

src/test/ui/issues/issue-60989.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ impl From<A> for B {
1010
fn main() {
1111
let c1 = ();
1212
c1::<()>;
13-
//~^ ERROR type arguments are not allowed for this type
13+
//~^ ERROR type arguments are not allowed on local variable
1414

1515
let c1 = A {};
1616
c1::<dyn Into<B>>;
17-
//~^ ERROR type arguments are not allowed for this type
17+
//~^ ERROR type arguments are not allowed on local variable
1818
}

0 commit comments

Comments
 (0)