Skip to content

Commit 86d5939

Browse files
authored
Rollup merge of rust-lang#116215 - estebank:parse-type-angle-bracket-tweak, r=compiler-errors
Tweak wording of missing angle backets in qualified path
2 parents be51067 + 3848ffc commit 86d5939

File tree

7 files changed

+162
-32
lines changed

7 files changed

+162
-32
lines changed

compiler/rustc_parse/messages.ftl

+1-1
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ parse_maybe_fn_typo_with_impl = you might have meant to write `impl` instead of
509509
510510
parse_maybe_recover_from_bad_qpath_stage_2 =
511511
missing angle brackets in associated item path
512-
.suggestion = try: `{$ty}`
512+
.suggestion = types that don't start with an identifier need to be surrounded with angle brackets in qualified paths
513513
514514
parse_maybe_recover_from_bad_type_plus =
515515
expected a path on the left-hand side of `+`, not `{$ty}`

compiler/rustc_parse/src/errors.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,18 @@ pub(crate) enum BadTypePlusSub {
5959
#[diag(parse_maybe_recover_from_bad_qpath_stage_2)]
6060
pub(crate) struct BadQPathStage2 {
6161
#[primary_span]
62-
#[suggestion(code = "", applicability = "maybe-incorrect")]
6362
pub span: Span,
64-
pub ty: String,
63+
#[subdiagnostic]
64+
pub wrap: WrapType,
65+
}
66+
67+
#[derive(Subdiagnostic)]
68+
#[multipart_suggestion(parse_suggestion, applicability = "machine-applicable")]
69+
pub(crate) struct WrapType {
70+
#[suggestion_part(code = "<")]
71+
pub lo: Span,
72+
#[suggestion_part(code = ">")]
73+
pub hi: Span,
6574
}
6675

6776
#[derive(Diagnostic)]

compiler/rustc_parse/src/parser/diagnostics.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::errors::{
1616
StructLiteralBodyWithoutPath, StructLiteralBodyWithoutPathSugg, StructLiteralNeedingParens,
1717
StructLiteralNeedingParensSugg, SuggAddMissingLetStmt, SuggEscapeIdentifier, SuggRemoveComma,
1818
TernaryOperator, UnexpectedConstInGenericParam, UnexpectedConstParamDeclaration,
19-
UnexpectedConstParamDeclarationSugg, UnmatchedAngleBrackets, UseEqInstead,
19+
UnexpectedConstParamDeclarationSugg, UnmatchedAngleBrackets, UseEqInstead, WrapType,
2020
};
2121

2222
use crate::fluent_generated as fluent;
@@ -1589,10 +1589,9 @@ impl<'a> Parser<'a> {
15891589
self.parse_path_segments(&mut path.segments, T::PATH_STYLE, None)?;
15901590
path.span = ty_span.to(self.prev_token.span);
15911591

1592-
let ty_str = self.span_to_snippet(ty_span).unwrap_or_else(|_| pprust::ty_to_string(&ty));
15931592
self.sess.emit_err(BadQPathStage2 {
1594-
span: path.span,
1595-
ty: format!("<{}>::{}", ty_str, pprust::path_to_string(&path)),
1593+
span: ty_span,
1594+
wrap: WrapType { lo: ty_span.shrink_to_lo(), hi: ty_span.shrink_to_hi() },
15961595
});
15971596

15981597
let path_span = ty_span.shrink_to_hi(); // Use an empty path since `position == 0`.

tests/ui/did_you_mean/bad-assoc-expr.stderr

+53-9
Original file line numberDiff line numberDiff line change
@@ -2,60 +2,104 @@ error: missing angle brackets in associated item path
22
--> $DIR/bad-assoc-expr.rs:3:5
33
|
44
LL | [i32; 4]::clone(&a);
5-
| ^^^^^^^^^^^^^^^ help: try: `<[i32; 4]>::clone`
5+
| ^^^^^^^^
6+
|
7+
help: types that don't start with an identifier need to be surrounded with angle brackets in qualified paths
8+
|
9+
LL | <[i32; 4]>::clone(&a);
10+
| + +
611

712
error: missing angle brackets in associated item path
813
--> $DIR/bad-assoc-expr.rs:6:5
914
|
1015
LL | [i32]::as_ref(&a);
11-
| ^^^^^^^^^^^^^ help: try: `<[i32]>::as_ref`
16+
| ^^^^^
17+
|
18+
help: types that don't start with an identifier need to be surrounded with angle brackets in qualified paths
19+
|
20+
LL | <[i32]>::as_ref(&a);
21+
| + +
1222

1323
error: missing angle brackets in associated item path
1424
--> $DIR/bad-assoc-expr.rs:9:5
1525
|
1626
LL | (u8)::clone(&0);
17-
| ^^^^^^^^^^^ help: try: `<(u8)>::clone`
27+
| ^^^^
28+
|
29+
help: types that don't start with an identifier need to be surrounded with angle brackets in qualified paths
30+
|
31+
LL | <(u8)>::clone(&0);
32+
| + +
1833

1934
error: missing angle brackets in associated item path
2035
--> $DIR/bad-assoc-expr.rs:12:5
2136
|
2237
LL | (u8, u8)::clone(&(0, 0));
23-
| ^^^^^^^^^^^^^^^ help: try: `<(u8, u8)>::clone`
38+
| ^^^^^^^^
39+
|
40+
help: types that don't start with an identifier need to be surrounded with angle brackets in qualified paths
41+
|
42+
LL | <(u8, u8)>::clone(&(0, 0));
43+
| + +
2444

2545
error: missing angle brackets in associated item path
2646
--> $DIR/bad-assoc-expr.rs:15:6
2747
|
2848
LL | &(u8)::clone(&0);
29-
| ^^^^^^^^^^^ help: try: `<(u8)>::clone`
49+
| ^^^^
50+
|
51+
help: types that don't start with an identifier need to be surrounded with angle brackets in qualified paths
52+
|
53+
LL | &<(u8)>::clone(&0);
54+
| + +
3055

3156
error: missing angle brackets in associated item path
3257
--> $DIR/bad-assoc-expr.rs:18:10
3358
|
3459
LL | 10 + (u8)::clone(&0);
35-
| ^^^^^^^^^^^ help: try: `<(u8)>::clone`
60+
| ^^^^
61+
|
62+
help: types that don't start with an identifier need to be surrounded with angle brackets in qualified paths
63+
|
64+
LL | 10 + <(u8)>::clone(&0);
65+
| + +
3666

3767
error: missing angle brackets in associated item path
3868
--> $DIR/bad-assoc-expr.rs:32:13
3969
|
4070
LL | let _ = ty!()::clone(&0);
41-
| ^^^^^^^^^^^^ help: try: `<ty!()>::clone`
71+
| ^^^^^
72+
|
73+
help: types that don't start with an identifier need to be surrounded with angle brackets in qualified paths
74+
|
75+
LL | let _ = <ty!()>::clone(&0);
76+
| + +
4277

4378
error: missing angle brackets in associated item path
4479
--> $DIR/bad-assoc-expr.rs:34:5
4580
|
4681
LL | ty!()::clone(&0);
47-
| ^^^^^^^^^^^^ help: try: `<ty!()>::clone`
82+
| ^^^^^
83+
|
84+
help: types that don't start with an identifier need to be surrounded with angle brackets in qualified paths
85+
|
86+
LL | <ty!()>::clone(&0);
87+
| + +
4888

4989
error: missing angle brackets in associated item path
5090
--> $DIR/bad-assoc-expr.rs:23:19
5191
|
5292
LL | ($ty: ty) => ($ty::clone(&0))
53-
| ^^^^^^^^^^ help: try: `<$ty>::clone`
93+
| ^^^
5494
...
5595
LL | expr!(u8);
5696
| --------- in this macro invocation
5797
|
5898
= note: this error originates in the macro `expr` (in Nightly builds, run with -Z macro-backtrace for more info)
99+
help: types that don't start with an identifier need to be surrounded with angle brackets in qualified paths
100+
|
101+
LL | ($ty: ty) => (<$ty>::clone(&0))
102+
| + +
59103

60104
error: aborting due to 9 previous errors
61105

tests/ui/did_you_mean/bad-assoc-pat.stderr

+35-6
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,71 @@ error: missing angle brackets in associated item path
22
--> $DIR/bad-assoc-pat.rs:3:9
33
|
44
LL | [u8]::AssocItem => {}
5-
| ^^^^^^^^^^^^^^^ help: try: `<[u8]>::AssocItem`
5+
| ^^^^
6+
|
7+
help: types that don't start with an identifier need to be surrounded with angle brackets in qualified paths
8+
|
9+
LL | <[u8]>::AssocItem => {}
10+
| + +
611

712
error: missing angle brackets in associated item path
813
--> $DIR/bad-assoc-pat.rs:6:9
914
|
1015
LL | (u8, u8)::AssocItem => {}
11-
| ^^^^^^^^^^^^^^^^^^^ help: try: `<(u8, u8)>::AssocItem`
16+
| ^^^^^^^^
17+
|
18+
help: types that don't start with an identifier need to be surrounded with angle brackets in qualified paths
19+
|
20+
LL | <(u8, u8)>::AssocItem => {}
21+
| + +
1222

1323
error: missing angle brackets in associated item path
1424
--> $DIR/bad-assoc-pat.rs:9:9
1525
|
1626
LL | _::AssocItem => {}
17-
| ^^^^^^^^^^^^ help: try: `<_>::AssocItem`
27+
| ^
28+
|
29+
help: types that don't start with an identifier need to be surrounded with angle brackets in qualified paths
30+
|
31+
LL | <_>::AssocItem => {}
32+
| + +
1833

1934
error: missing angle brackets in associated item path
2035
--> $DIR/bad-assoc-pat.rs:14:10
2136
|
2237
LL | &(u8,)::AssocItem => {}
23-
| ^^^^^^^^^^^^^^^^ help: try: `<(u8,)>::AssocItem`
38+
| ^^^^^
39+
|
40+
help: types that don't start with an identifier need to be surrounded with angle brackets in qualified paths
41+
|
42+
LL | &<(u8,)>::AssocItem => {}
43+
| + +
2444

2545
error: missing angle brackets in associated item path
2646
--> $DIR/bad-assoc-pat.rs:32:9
2747
|
2848
LL | ty!()::AssocItem => {}
29-
| ^^^^^^^^^^^^^^^^ help: try: `<ty!()>::AssocItem`
49+
| ^^^^^
50+
|
51+
help: types that don't start with an identifier need to be surrounded with angle brackets in qualified paths
52+
|
53+
LL | <ty!()>::AssocItem => {}
54+
| + +
3055

3156
error: missing angle brackets in associated item path
3257
--> $DIR/bad-assoc-pat.rs:21:19
3358
|
3459
LL | ($ty: ty) => ($ty::AssocItem)
35-
| ^^^^^^^^^^^^^^ help: try: `<$ty>::AssocItem`
60+
| ^^^
3661
...
3762
LL | pat!(u8) => {}
3863
| -------- in this macro invocation
3964
|
4065
= note: this error originates in the macro `pat` (in Nightly builds, run with -Z macro-backtrace for more info)
66+
help: types that don't start with an identifier need to be surrounded with angle brackets in qualified paths
67+
|
68+
LL | ($ty: ty) => (<$ty>::AssocItem)
69+
| + +
4170

4271
error[E0599]: no associated item named `AssocItem` found for slice `[u8]` in the current scope
4372
--> $DIR/bad-assoc-pat.rs:3:15

tests/ui/did_you_mean/bad-assoc-ty.stderr

+53-9
Original file line numberDiff line numberDiff line change
@@ -2,60 +2,104 @@ error: missing angle brackets in associated item path
22
--> $DIR/bad-assoc-ty.rs:1:10
33
|
44
LL | type A = [u8; 4]::AssocTy;
5-
| ^^^^^^^^^^^^^^^^ help: try: `<[u8; 4]>::AssocTy`
5+
| ^^^^^^^
6+
|
7+
help: types that don't start with an identifier need to be surrounded with angle brackets in qualified paths
8+
|
9+
LL | type A = <[u8; 4]>::AssocTy;
10+
| + +
611

712
error: missing angle brackets in associated item path
813
--> $DIR/bad-assoc-ty.rs:5:10
914
|
1015
LL | type B = [u8]::AssocTy;
11-
| ^^^^^^^^^^^^^ help: try: `<[u8]>::AssocTy`
16+
| ^^^^
17+
|
18+
help: types that don't start with an identifier need to be surrounded with angle brackets in qualified paths
19+
|
20+
LL | type B = <[u8]>::AssocTy;
21+
| + +
1222

1323
error: missing angle brackets in associated item path
1424
--> $DIR/bad-assoc-ty.rs:9:10
1525
|
1626
LL | type C = (u8)::AssocTy;
17-
| ^^^^^^^^^^^^^ help: try: `<(u8)>::AssocTy`
27+
| ^^^^
28+
|
29+
help: types that don't start with an identifier need to be surrounded with angle brackets in qualified paths
30+
|
31+
LL | type C = <(u8)>::AssocTy;
32+
| + +
1833

1934
error: missing angle brackets in associated item path
2035
--> $DIR/bad-assoc-ty.rs:13:10
2136
|
2237
LL | type D = (u8, u8)::AssocTy;
23-
| ^^^^^^^^^^^^^^^^^ help: try: `<(u8, u8)>::AssocTy`
38+
| ^^^^^^^^
39+
|
40+
help: types that don't start with an identifier need to be surrounded with angle brackets in qualified paths
41+
|
42+
LL | type D = <(u8, u8)>::AssocTy;
43+
| + +
2444

2545
error: missing angle brackets in associated item path
2646
--> $DIR/bad-assoc-ty.rs:17:10
2747
|
2848
LL | type E = _::AssocTy;
29-
| ^^^^^^^^^^ help: try: `<_>::AssocTy`
49+
| ^
50+
|
51+
help: types that don't start with an identifier need to be surrounded with angle brackets in qualified paths
52+
|
53+
LL | type E = <_>::AssocTy;
54+
| + +
3055

3156
error: missing angle brackets in associated item path
3257
--> $DIR/bad-assoc-ty.rs:21:19
3358
|
3459
LL | type F = &'static (u8)::AssocTy;
35-
| ^^^^^^^^^^^^^ help: try: `<(u8)>::AssocTy`
60+
| ^^^^
61+
|
62+
help: types that don't start with an identifier need to be surrounded with angle brackets in qualified paths
63+
|
64+
LL | type F = &'static <(u8)>::AssocTy;
65+
| + +
3666

3767
error: missing angle brackets in associated item path
3868
--> $DIR/bad-assoc-ty.rs:27:10
3969
|
4070
LL | type G = dyn 'static + (Send)::AssocTy;
41-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `<dyn 'static + (Send)>::AssocTy`
71+
| ^^^^^^^^^^^^^^^^^^^^
72+
|
73+
help: types that don't start with an identifier need to be surrounded with angle brackets in qualified paths
74+
|
75+
LL | type G = <dyn 'static + (Send)>::AssocTy;
76+
| + +
4277

4378
error: missing angle brackets in associated item path
4479
--> $DIR/bad-assoc-ty.rs:46:10
4580
|
4681
LL | type I = ty!()::AssocTy;
47-
| ^^^^^^^^^^^^^^ help: try: `<ty!()>::AssocTy`
82+
| ^^^^^
83+
|
84+
help: types that don't start with an identifier need to be surrounded with angle brackets in qualified paths
85+
|
86+
LL | type I = <ty!()>::AssocTy;
87+
| + +
4888

4989
error: missing angle brackets in associated item path
5090
--> $DIR/bad-assoc-ty.rs:39:19
5191
|
5292
LL | ($ty: ty) => ($ty::AssocTy);
53-
| ^^^^^^^^^^^^ help: try: `<$ty>::AssocTy`
93+
| ^^^
5494
...
5595
LL | type J = ty!(u8);
5696
| ------- in this macro invocation
5797
|
5898
= note: this error originates in the macro `ty` (in Nightly builds, run with -Z macro-backtrace for more info)
99+
help: types that don't start with an identifier need to be surrounded with angle brackets in qualified paths
100+
|
101+
LL | ($ty: ty) => (<$ty>::AssocTy);
102+
| + +
59103

60104
error[E0223]: ambiguous associated type
61105
--> $DIR/bad-assoc-ty.rs:1:10

tests/ui/parser/issues/issue-89388.stderr

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ error: missing angle brackets in associated item path
22
--> $DIR/issue-89388.rs:5:24
33
|
44
LL | let _ = option.map([_]::to_vec);
5-
| ^^^^^^^^^^^ help: try: `<[_]>::to_vec`
5+
| ^^^
6+
|
7+
help: types that don't start with an identifier need to be surrounded with angle brackets in qualified paths
8+
|
9+
LL | let _ = option.map(<[_]>::to_vec);
10+
| + +
611

712
error: aborting due to previous error
813

0 commit comments

Comments
 (0)