Skip to content

Commit 560d463

Browse files
committed
Emit suggestions when equality constraints are wronlgy used
1 parent 0ad927c commit 560d463

File tree

14 files changed

+382
-25
lines changed

14 files changed

+382
-25
lines changed

compiler/rustc_hir_analysis/src/astconv/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 AstConv<'tcx> + '_ {
@@ -1029,12 +1031,12 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
10291031
/// Emits an error regarding forbidden type binding associations
10301032
pub fn prohibit_assoc_ty_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_ty_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 {
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/astconv/generics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ pub(crate) fn check_generic_arg_count(
456456
if gen_pos != GenericArgPosition::Type
457457
&& let Some(b) = gen_args.bindings.first()
458458
{
459-
prohibit_assoc_ty_binding(tcx, b.span, None);
459+
prohibit_assoc_ty_binding(tcx, b, None);
460460
}
461461

462462
let explicit_late_bound =

compiler/rustc_hir_analysis/src/astconv/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
288288
ty::BoundConstness::NotConst,
289289
);
290290
if let Some(b) = item_segment.args().bindings.first() {
291-
prohibit_assoc_ty_binding(self.tcx(), b.span, Some((item_segment, span)));
291+
prohibit_assoc_ty_binding(self.tcx(), b, Some((def_id, item_segment, span)));
292292
}
293293

294294
args
@@ -591,7 +591,7 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
591591
);
592592

593593
if let Some(b) = item_segment.args().bindings.first() {
594-
prohibit_assoc_ty_binding(self.tcx(), b.span, Some((item_segment, span)));
594+
prohibit_assoc_ty_binding(self.tcx(), b, Some((item_def_id, item_segment, span)));
595595
}
596596

597597
args
@@ -727,7 +727,7 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
727727
constness,
728728
);
729729
if let Some(b) = trait_segment.args().bindings.first() {
730-
prohibit_assoc_ty_binding(self.tcx(), b.span, Some((trait_segment, span)));
730+
prohibit_assoc_ty_binding(self.tcx(), b, Some((trait_def_id, trait_segment, span)));
731731
}
732732
ty::TraitRef::new(self.tcx(), trait_def_id, generic_args)
733733
}
@@ -1676,7 +1676,7 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
16761676
for segment in segments {
16771677
// Only emit the first error to avoid overloading the user with error messages.
16781678
if let Some(b) = segment.args().bindings.first() {
1679-
prohibit_assoc_ty_binding(self.tcx(), b.span, None);
1679+
prohibit_assoc_ty_binding(self.tcx(), b, None);
16801680
return true;
16811681
}
16821682
}

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,49 @@
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+
help: to use `u32` as a generic argument specify it directly
8+
|
9+
LL | type A: S<C<u32> = ()>; // Just one erroneous equality constraint
10+
| ~~~
611

712
error[E0229]: associated type bindings are not allowed here
813
--> $DIR/issue-102335-ty.rs:2:17
914
|
10-
LL | type A: S<C<i32 = u32> = ()>;
15+
LL | type A: S<C<i32 = u32> = ()>; // Just one erroneous equality constraint
16+
| ^^^^^^^^^ associated type not allowed here
17+
|
18+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
19+
help: to use `u32` as a generic argument specify it directly
20+
|
21+
LL | type A: S<C<u32> = ()>; // Just one erroneous equality constraint
22+
| ~~~
23+
24+
error[E0229]: associated type bindings are not allowed here
25+
--> $DIR/issue-102335-ty.rs:8:17
26+
|
27+
LL | type A: S<C<i32 = u32, X = i32> = ()>; // More than one erroneous equality constraints
28+
| ^^^^^^^^^ associated type not allowed here
29+
|
30+
help: to use `u32` as a generic argument specify it directly
31+
|
32+
LL | type A: S<C<u32, X = i32> = ()>; // More than one erroneous equality constraints
33+
| ~~~
34+
35+
error[E0229]: associated type bindings are not allowed here
36+
--> $DIR/issue-102335-ty.rs:8:17
37+
|
38+
LL | type A: S<C<i32 = u32, X = i32> = ()>; // More than one erroneous equality constraints
1139
| ^^^^^^^^^ associated type not allowed here
1240
|
1341
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
42+
help: to use `u32` as a generic argument specify it directly
43+
|
44+
LL | type A: S<C<u32, X = i32> = ()>; // More than one erroneous equality constraints
45+
| ~~~
1446

15-
error: aborting due to 2 previous errors
47+
error: aborting due to 4 previous errors
1648

1749
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)