Skip to content

Commit cd03fe1

Browse files
committed
Suggest replace parentheses with angle brackets
1 parent 9598b4b commit cd03fe1

File tree

3 files changed

+73
-9
lines changed

3 files changed

+73
-9
lines changed

compiler/rustc_ast_lowering/src/lib.rs

+38-8
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet};
4646
use rustc_data_structures::sorted_map::SortedMap;
4747
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
4848
use rustc_data_structures::sync::Lrc;
49-
use rustc_errors::struct_span_err;
49+
use rustc_errors::{struct_span_err, Applicability};
5050
use rustc_hir as hir;
5151
use rustc_hir::def::{DefKind, Namespace, PartialRes, PerNS, Res};
5252
use rustc_hir::def_id::{DefId, DefPathHash, LocalDefId, CRATE_DEF_ID};
@@ -901,7 +901,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
901901
mut itctx: ImplTraitContext<'_, 'hir>,
902902
) -> hir::TypeBinding<'hir> {
903903
debug!("lower_assoc_ty_constraint(constraint={:?}, itctx={:?})", constraint, itctx);
904-
905904
// lower generic arguments of identifier in constraint
906905
let gen_args = if let Some(ref gen_args) = constraint.gen_args {
907906
let gen_args_ctor = match gen_args {
@@ -914,12 +913,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
914913
.0
915914
}
916915
GenericArgs::Parenthesized(ref data) => {
917-
let mut err = self.sess.struct_span_err(
918-
gen_args.span(),
919-
"parenthesized generic arguments cannot be used in associated type constraints"
920-
);
921-
// FIXME: try to write a suggestion here
922-
err.emit();
916+
self.assoc_ty_contraint_param_error_emit(data);
923917
self.lower_angle_bracketed_parameter_data(
924918
&data.as_angle_bracketed_args(),
925919
ParamMode::Explicit,
@@ -1033,6 +1027,42 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
10331027
}
10341028
}
10351029

1030+
fn assoc_ty_contraint_param_error_emit(&self, data: &ParenthesizedArgs) -> () {
1031+
let mut err = self.sess.struct_span_err(
1032+
data.span,
1033+
"parenthesized generic arguments cannot be used in associated type constraints",
1034+
);
1035+
// Suggest removing empty parentheses: "Trait()" -> "Trait"
1036+
if data.inputs.is_empty() {
1037+
let parentheses_span =
1038+
data.inputs_span.shrink_to_lo().to(data.inputs_span.shrink_to_hi());
1039+
err.multipart_suggestion(
1040+
"remove these parentheses",
1041+
vec![(parentheses_span, String::new())],
1042+
Applicability::MaybeIncorrect,
1043+
);
1044+
}
1045+
// Suggest replacing parentheses with angle brackets `Trait(params...)` to `Trait<params...>`
1046+
else {
1047+
// Start of parameters to the 1st argument
1048+
let open_param = data.inputs_span.shrink_to_lo().to(data
1049+
.inputs
1050+
.first()
1051+
.unwrap()
1052+
.span
1053+
.shrink_to_lo());
1054+
// End of last argument to end of parameters
1055+
let close_param =
1056+
data.inputs.last().unwrap().span.shrink_to_hi().to(data.inputs_span.shrink_to_hi());
1057+
err.multipart_suggestion(
1058+
&format!("use angle brackets instead",),
1059+
vec![(open_param, String::from("<")), (close_param, String::from(">"))],
1060+
Applicability::MaybeIncorrect,
1061+
);
1062+
}
1063+
err.emit();
1064+
}
1065+
10361066
fn lower_generic_arg(
10371067
&mut self,
10381068
arg: &ast::GenericArg,

src/test/ui/generic-associated-types/gat-trait-path-parenthesised-args.rs

+5
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,9 @@ fn foo<'a>(arg: Box<dyn X<Y('a) = &'a ()>>) {}
1010
//~| ERROR this associated type takes 0 generic arguments but 1 generic argument
1111
//~| ERROR this associated type takes 1 lifetime argument but 0 lifetime arguments
1212

13+
14+
fn bar<'a>(arg: Box<dyn X<Y() = ()>>) {}
15+
//~^ ERROR: parenthesized generic arguments cannot be used
16+
//~| ERROR this associated type takes 1 lifetime argument but 0 lifetime arguments
17+
1318
fn main() {}

src/test/ui/generic-associated-types/gat-trait-path-parenthesised-args.stderr

+30-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,19 @@ error: parenthesized generic arguments cannot be used in associated type constra
99
|
1010
LL | fn foo<'a>(arg: Box<dyn X<Y('a) = &'a ()>>) {}
1111
| ^^^^^
12+
|
13+
help: use angle brackets instead
14+
|
15+
LL | fn foo<'a>(arg: Box<dyn X<Y<'a> = &'a ()>>) {}
16+
| ~ ~
17+
18+
error: parenthesized generic arguments cannot be used in associated type constraints
19+
--> $DIR/gat-trait-path-parenthesised-args.rs:14:27
20+
|
21+
LL | fn bar<'a>(arg: Box<dyn X<Y() = ()>>) {}
22+
| ^--
23+
| |
24+
| help: remove these parentheses
1225

1326
error[E0107]: this associated type takes 1 lifetime argument but 0 lifetime arguments were supplied
1427
--> $DIR/gat-trait-path-parenthesised-args.rs:7:27
@@ -40,6 +53,22 @@ note: associated type defined here, with 0 generic parameters
4053
LL | type Y<'a>;
4154
| ^
4255

43-
error: aborting due to 4 previous errors
56+
error[E0107]: this associated type takes 1 lifetime argument but 0 lifetime arguments were supplied
57+
--> $DIR/gat-trait-path-parenthesised-args.rs:14:27
58+
|
59+
LL | fn bar<'a>(arg: Box<dyn X<Y() = ()>>) {}
60+
| ^ expected 1 lifetime argument
61+
|
62+
note: associated type defined here, with 1 lifetime parameter: `'a`
63+
--> $DIR/gat-trait-path-parenthesised-args.rs:4:8
64+
|
65+
LL | type Y<'a>;
66+
| ^ --
67+
help: add missing lifetime argument
68+
|
69+
LL | fn bar<'a>(arg: Box<dyn X<Y('a) = ()>>) {}
70+
| ++
71+
72+
error: aborting due to 6 previous errors
4473

4574
For more information about this error, try `rustc --explain E0107`.

0 commit comments

Comments
 (0)