Skip to content

Commit 274ad2a

Browse files
committed
Emit suggestions when equality constraints are wronlgy used
1 parent eff958c commit 274ad2a

File tree

17 files changed

+421
-25
lines changed

17 files changed

+421
-25
lines changed

compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs

+81-7
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ use rustc_hir as hir;
1515
use rustc_hir::def_id::{DefId, LocalDefId};
1616
use rustc_infer::traits::FulfillmentError;
1717
use rustc_middle::query::Key;
18-
use rustc_middle::ty::{self, suggest_constraining_type_param, Ty, TyCtxt, TypeVisitableExt};
18+
use rustc_middle::ty::{
19+
self, suggest_constraining_type_param, GenericParamCount, Ty, TyCtxt, TypeVisitableExt,
20+
};
1921
use rustc_session::parse::feature_err;
2022
use rustc_span::edit_distance::find_best_match_for_name;
2123
use rustc_span::symbol::{sym, Ident};
22-
use rustc_span::{Span, Symbol, DUMMY_SP};
24+
use rustc_span::{BytePos, Span, Symbol, DUMMY_SP};
2325
use rustc_trait_selection::traits::object_safety_violations_for_assoc_item;
2426

2527
impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
@@ -1029,12 +1031,12 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
10291031
/// Emits an error regarding forbidden type binding associations
10301032
pub fn prohibit_assoc_item_binding(
10311033
tcx: TyCtxt<'_>,
1032-
span: Span,
1033-
segment: Option<(&hir::PathSegment<'_>, Span)>,
1034+
binding: &hir::TypeBinding<'_>,
1035+
segment: Option<(DefId, &hir::PathSegment<'_>, Span)>,
10341036
) {
1035-
tcx.dcx().emit_err(AssocTypeBindingNotAllowed {
1036-
span,
1037-
fn_trait_expansion: if let Some((segment, span)) = segment
1037+
let mut err = tcx.dcx().create_err(AssocTypeBindingNotAllowed {
1038+
span: binding.span,
1039+
fn_trait_expansion: if let Some((_, segment, span)) = segment
10381040
&& segment.args().parenthesized == hir::GenericArgsParentheses::ParenSugar
10391041
{
10401042
Some(ParenthesizedFnTraitExpansion {
@@ -1045,6 +1047,78 @@ pub fn prohibit_assoc_item_binding(
10451047
None
10461048
},
10471049
});
1050+
1051+
// Emit a suggestion if possible
1052+
if let Some((def_id, segment, _)) = segment
1053+
&& segment.args().parenthesized == hir::GenericArgsParentheses::No
1054+
&& let hir::TypeBindingKind::Equality { term: binding_arg_ty } = binding.kind
1055+
{
1056+
// Suggests removal of the offending equality constraint
1057+
let suggest_removal = |e: &mut Diag<'_>| {
1058+
let mut suggestion_span = binding.span.with_hi(binding.span.hi() + BytePos(1)); // Include the comma or the angle bracket at the end
1059+
if segment.args().bindings.len() == 1 {
1060+
// If it is the only binding specified
1061+
// include the starting angle bracket as well
1062+
suggestion_span = suggestion_span.with_lo(suggestion_span.lo() - BytePos(1))
1063+
}
1064+
if let Ok(suggestion) = tcx.sess.source_map().span_to_snippet(suggestion_span) {
1065+
e.span_suggestion_verbose(
1066+
suggestion_span,
1067+
"try removing this type binding",
1068+
suggestion,
1069+
Applicability::MaybeIncorrect,
1070+
);
1071+
}
1072+
};
1073+
1074+
// Suggests replacing the quality constraint with
1075+
// normal type argument
1076+
let suggest_direct_use = |e: &mut Diag<'_>, sp: Span, is_ty: bool| {
1077+
if let Ok(snippet) = tcx.sess.source_map().span_to_snippet(sp) {
1078+
let ty_or_const = if is_ty { "generic" } else { "const" };
1079+
e.span_suggestion_verbose(
1080+
binding.span,
1081+
format!("to use `{snippet}` as a {ty_or_const} argument specify it directly"),
1082+
snippet,
1083+
Applicability::MaybeIncorrect,
1084+
);
1085+
}
1086+
};
1087+
1088+
// Get a sense of what generic args the type expects
1089+
let generics = tcx.generics_of(def_id);
1090+
let GenericParamCount { mut types, consts, .. } = generics.own_counts();
1091+
if generics.has_self && types > 0 {
1092+
types -= 1 // Ignore the `Self` type
1093+
}
1094+
1095+
// Now emit suggestion
1096+
if types == 0 && consts == 0 {
1097+
err.note(format!("`{0}` is not a generic type", segment.ident));
1098+
suggest_removal(&mut err);
1099+
} else {
1100+
match binding_arg_ty {
1101+
hir::Term::Ty(ty) => {
1102+
if types > 0 {
1103+
suggest_direct_use(&mut err, ty.span, true);
1104+
} else {
1105+
suggest_removal(&mut err);
1106+
}
1107+
}
1108+
hir::Term::Const(c) if consts > 0 => {
1109+
if consts > 0 {
1110+
let span = tcx.hir().span(c.hir_id);
1111+
suggest_direct_use(&mut err, span, false);
1112+
} else {
1113+
suggest_removal(&mut err);
1114+
}
1115+
}
1116+
_ => {}
1117+
}
1118+
}
1119+
}
1120+
1121+
err.emit();
10481122
}
10491123

10501124
pub(crate) fn fn_trait_to_string(

compiler/rustc_hir_analysis/src/hir_ty_lowering/generics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ pub(crate) fn check_generic_arg_count(
454454
if gen_pos != GenericArgPosition::Type
455455
&& let Some(b) = gen_args.bindings.first()
456456
{
457-
prohibit_assoc_item_binding(tcx, b.span, None);
457+
prohibit_assoc_item_binding(tcx, b, None);
458458
}
459459

460460
let explicit_late_bound =

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
322322
ty::BoundConstness::NotConst,
323323
);
324324
if let Some(b) = item_segment.args().bindings.first() {
325-
prohibit_assoc_item_binding(self.tcx(), b.span, Some((item_segment, span)));
325+
prohibit_assoc_item_binding(self.tcx(), b, Some((def_id, item_segment, span)));
326326
}
327327
args
328328
}
@@ -619,7 +619,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
619619
ty::BoundConstness::NotConst,
620620
);
621621
if let Some(b) = item_segment.args().bindings.first() {
622-
prohibit_assoc_item_binding(self.tcx(), b.span, Some((item_segment, span)));
622+
prohibit_assoc_item_binding(self.tcx(), b, Some((item_def_id, item_segment, span)));
623623
}
624624
args
625625
}
@@ -758,7 +758,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
758758
constness,
759759
);
760760
if let Some(b) = trait_segment.args().bindings.first() {
761-
prohibit_assoc_item_binding(self.tcx(), b.span, Some((trait_segment, span)));
761+
prohibit_assoc_item_binding(self.tcx(), b, Some((trait_def_id, trait_segment, span)));
762762
}
763763
ty::TraitRef::new(self.tcx(), trait_def_id, generic_args)
764764
}
@@ -1724,7 +1724,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
17241724
for segment in segments {
17251725
// Only emit the first error to avoid overloading the user with error messages.
17261726
if let Some(b) = segment.args().bindings.first() {
1727-
prohibit_assoc_item_binding(self.tcx(), b.span, None);
1727+
prohibit_assoc_item_binding(self.tcx(), b, None);
17281728
return true;
17291729
}
17301730
}

tests/rustdoc-ui/invalid_associated_const.stderr

+11
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,25 @@ error[E0229]: associated type bindings are not allowed here
33
|
44
LL | type A: S<C<X = 0i32> = 34>;
55
| ^^^^^^^^ associated type not allowed here
6+
|
7+
= note: `C` is not a generic type
8+
help: try removing this type binding
9+
|
10+
LL | type A: S<C<X = 0i32> = 34>;
11+
| ~~~~~~~~~~
612

713
error[E0229]: associated type bindings are not allowed here
814
--> $DIR/invalid_associated_const.rs:4:17
915
|
1016
LL | type A: S<C<X = 0i32> = 34>;
1117
| ^^^^^^^^ associated type not allowed here
1218
|
19+
= note: `C` is not a generic type
1320
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
21+
help: try removing this type binding
22+
|
23+
LL | type A: S<C<X = 0i32> = 34>;
24+
| ~~~~~~~~~~
1425

1526
error: aborting due to 2 previous errors
1627

tests/rustdoc-ui/issue-102467.stderr

+11
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,25 @@ error[E0229]: associated type bindings are not allowed here
33
|
44
LL | type A: S<C<X = 0i32> = 34>;
55
| ^^^^^^^^ associated type not allowed here
6+
|
7+
= note: `C` is not a generic type
8+
help: try removing this type binding
9+
|
10+
LL | type A: S<C<X = 0i32> = 34>;
11+
| ~~~~~~~~~~
612

713
error[E0229]: associated type bindings are not allowed here
814
--> $DIR/issue-102467.rs:7:17
915
|
1016
LL | type A: S<C<X = 0i32> = 34>;
1117
| ^^^^^^^^ associated type not allowed here
1218
|
19+
= note: `C` is not a generic type
1320
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
21+
help: try removing this type binding
22+
|
23+
LL | type A: S<C<X = 0i32> = 34>;
24+
| ~~~~~~~~~~
1425

1526
error: aborting due to 2 previous errors
1627

tests/ui/associated-consts/issue-102335-const.stderr

+11
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,25 @@ error[E0229]: associated type bindings are not allowed here
33
|
44
LL | type A: S<C<X = 0i32> = 34>;
55
| ^^^^^^^^ associated type not allowed here
6+
|
7+
= note: `C` is not a generic type
8+
help: try removing this type binding
9+
|
10+
LL | type A: S<C<X = 0i32> = 34>;
11+
| ~~~~~~~~~~
612

713
error[E0229]: associated type bindings are not allowed here
814
--> $DIR/issue-102335-const.rs:4:17
915
|
1016
LL | type A: S<C<X = 0i32> = 34>;
1117
| ^^^^^^^^ associated type not allowed here
1218
|
19+
= note: `C` is not a generic type
1320
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
21+
help: try removing this type binding
22+
|
23+
LL | type A: S<C<X = 0i32> = 34>;
24+
| ~~~~~~~~~~
1425

1526
error: aborting due to 2 previous errors
1627

tests/ui/associated-type-bounds/issue-102335-ty.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
trait T {
2-
type A: S<C<i32 = u32> = ()>;
2+
type A: S<C<i32 = u32> = ()>; // Just one erroneous equality constraint
3+
//~^ ERROR associated type bindings are not allowed here
4+
//~| ERROR associated type bindings are not allowed here
5+
}
6+
7+
trait T2 {
8+
type A: S<C<i32 = u32, X = i32> = ()>; // More than one erroneous equality constraints
39
//~^ ERROR associated type bindings are not allowed here
410
//~| ERROR associated type bindings are not allowed here
511
}
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,53 @@
11
error[E0229]: associated type bindings are not allowed here
22
--> $DIR/issue-102335-ty.rs:2:17
33
|
4-
LL | type A: S<C<i32 = u32> = ()>;
4+
LL | type A: S<C<i32 = u32> = ()>; // Just one erroneous equality constraint
55
| ^^^^^^^^^ associated type not allowed here
6+
|
7+
= note: `C` is not a generic type
8+
help: try removing this type binding
9+
|
10+
LL | type A: S<C<i32 = u32> = ()>; // Just one erroneous equality constraint
11+
| ~~~~~~~~~~~
612

713
error[E0229]: associated type bindings are not allowed here
814
--> $DIR/issue-102335-ty.rs:2:17
915
|
10-
LL | type A: S<C<i32 = u32> = ()>;
16+
LL | type A: S<C<i32 = u32> = ()>; // Just one erroneous equality constraint
17+
| ^^^^^^^^^ associated type not allowed here
18+
|
19+
= note: `C` is not a generic type
20+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
21+
help: try removing this type binding
22+
|
23+
LL | type A: S<C<i32 = u32> = ()>; // Just one erroneous equality constraint
24+
| ~~~~~~~~~~~
25+
26+
error[E0229]: associated type bindings are not allowed here
27+
--> $DIR/issue-102335-ty.rs:8:17
28+
|
29+
LL | type A: S<C<i32 = u32, X = i32> = ()>; // More than one erroneous equality constraints
30+
| ^^^^^^^^^ associated type not allowed here
31+
|
32+
= note: `C` is not a generic type
33+
help: try removing this type binding
34+
|
35+
LL | type A: S<C<i32 = u32, X = i32> = ()>; // More than one erroneous equality constraints
36+
| ~~~~~~~~~~
37+
38+
error[E0229]: associated type bindings are not allowed here
39+
--> $DIR/issue-102335-ty.rs:8:17
40+
|
41+
LL | type A: S<C<i32 = u32, X = i32> = ()>; // More than one erroneous equality constraints
1142
| ^^^^^^^^^ associated type not allowed here
1243
|
44+
= note: `C` is not a generic type
1345
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
46+
help: try removing this type binding
47+
|
48+
LL | type A: S<C<i32 = u32, X = i32> = ()>; // More than one erroneous equality constraints
49+
| ~~~~~~~~~~
1450

15-
error: aborting due to 2 previous errors
51+
error: aborting due to 4 previous errors
1652

1753
For more information about this error, try `rustc --explain E0229`.
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,69 @@
11
// Test equality constraints on associated types. Check we get an error when an
2-
// equality constraint is used in a qualified path.
2+
// equality constraint is used outside of type parameter declarations
33

44
pub trait Foo {
55
type A;
66
fn boo(&self) -> <Self as Foo>::A;
77
}
88

99
struct Bar;
10+
struct Qux;
1011

1112
impl Foo for isize {
1213
type A = usize;
1314
fn boo(&self) -> usize { 42 }
1415
}
1516

16-
fn baz<I: Foo>(x: &<I as Foo<A=Bar>>::A) {}
17+
fn baz<I: Foo>(_x: &<I as Foo<A=Bar>>::A) {}
1718
//~^ ERROR associated type bindings are not allowed here
1819

20+
21+
trait Tr1<T1> {
22+
}
23+
24+
impl Tr1<T1 = String> for Bar {
25+
//~^ ERROR associated type bindings are not allowed here
26+
//~| ERROR trait takes 1 generic argument but 0 generic arguments were supplied
27+
}
28+
29+
30+
trait Tr2<T1, T2, T3> {
31+
}
32+
33+
// E0229 is emitted only for the first erroneous equality
34+
// constraint (T2) not for any subequent ones (e.g. T3)
35+
impl Tr2<i32, T2 = Qux, T3 = usize> for Bar {
36+
//~^ ERROR associated type bindings are not allowed here
37+
//~| ERROR trait takes 3 generic arguments but 1 generic argument was supplied
38+
}
39+
40+
struct GenericStruct<T> { _t: T }
41+
42+
impl Tr2<i32, Qux, T3 = GenericStruct<i32>> for Bar {
43+
//~^ ERROR associated type bindings are not allowed here
44+
//~| ERROR trait takes 3 generic arguments but 2 generic arguments were supplied
45+
}
46+
47+
48+
// Covers the case when the type has a const param
49+
trait Tr3<const N: i32, T2, T3> {
50+
}
51+
52+
impl Tr3<N = 42, T2 = Qux, T3 = usize> for Bar {
53+
//~^ ERROR associated type bindings are not allowed here
54+
//~| ERROR associated const equality is incomplete
55+
//~| ERROR trait takes 3 generic arguments but 0 generic arguments were supplied
56+
}
57+
58+
59+
// Covers the case when the type
60+
// in question has no generic params
61+
trait Tr4 {
62+
}
63+
64+
impl Tr4<T = Qux, T2 = usize> for Bar {
65+
//~^ ERROR associated type bindings are not allowed here
66+
}
67+
68+
1969
pub fn main() {}

0 commit comments

Comments
 (0)