Skip to content

Commit 7938112

Browse files
committed
Actually print the overly generic type
1 parent e664d8a commit 7938112

7 files changed

+37
-4
lines changed

compiler/rustc_hir_analysis/messages.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,8 @@ hir_analysis_transparent_non_zero_sized_enum = the variant of a transparent {$de
416416
.label = needs at most one field with non-trivial size or alignment, but has {$field_count}
417417
.labels = this field has non-zero size or requires alignment
418418
419+
hir_analysis_ty_of_assoc_const_binding_note = `{$assoc_const}` has type `{$ty}`
420+
419421
hir_analysis_ty_param_first_local = type parameter `{$param_ty}` must be covered by another type when it appears before the first local type (`{$local_type}`)
420422
.label = type parameter `{$param_ty}` must be covered by another type when it appears before the first local type (`{$local_type}`)
421423
.note = implementing a foreign trait is only possible if at least one of the types for which it is implemented is local, and no uncovered type parameters appear before that first local type

compiler/rustc_hir_analysis/src/collect/type_of.rs

+5
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,9 @@ fn report_overly_generic_assoc_const_binding_type<'tcx>(
322322
}
323323

324324
let mut reported = None;
325+
let ty_note = ty
326+
.make_suggestable(tcx, false)
327+
.map(|ty| crate::errors::TyOfAssocConstBindingNote { assoc_const, ty });
325328

326329
let body_owner = tcx.hir().enclosing_body_owner(hir_id);
327330
let generics = tcx.generics_of(body_owner);
@@ -334,6 +337,7 @@ fn report_overly_generic_assoc_const_binding_type<'tcx>(
334337
param_def_kind: tcx.def_descr(param_def.def_id),
335338
synthetic: param_def.kind.is_synthetic(),
336339
param_defined_here_label: tcx.def_ident_span(param_def.def_id).unwrap(),
340+
ty_note,
337341
}));
338342
}
339343
for (var_def_id, var_name) in collector.vars {
@@ -344,6 +348,7 @@ fn report_overly_generic_assoc_const_binding_type<'tcx>(
344348
var_name,
345349
var_def_kind: tcx.def_descr(var_def_id),
346350
var_defined_here_label: tcx.def_ident_span(var_def_id).unwrap(),
351+
ty_note,
347352
},
348353
));
349354
}

compiler/rustc_hir_analysis/src/errors.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ pub struct AssocTypeBindingNotAllowed {
256256

257257
#[derive(Diagnostic)]
258258
#[diag(hir_analysis_param_in_ty_of_assoc_const_binding)]
259-
pub(crate) struct ParamInTyOfAssocConstBinding {
259+
pub(crate) struct ParamInTyOfAssocConstBinding<'tcx> {
260260
#[primary_span]
261261
#[label]
262262
pub span: Span,
@@ -266,11 +266,13 @@ pub(crate) struct ParamInTyOfAssocConstBinding {
266266
pub synthetic: bool,
267267
#[label(hir_analysis_param_defined_here_label)]
268268
pub param_defined_here_label: Span,
269+
#[subdiagnostic]
270+
pub ty_note: Option<TyOfAssocConstBindingNote<'tcx>>,
269271
}
270272

271273
#[derive(Diagnostic)]
272274
#[diag(hir_analysis_escaping_bound_var_in_ty_of_assoc_const_binding)]
273-
pub(crate) struct EscapingBoundVarInTyOfAssocConstBinding {
275+
pub(crate) struct EscapingBoundVarInTyOfAssocConstBinding<'tcx> {
274276
#[primary_span]
275277
#[label]
276278
pub span: Span,
@@ -279,6 +281,15 @@ pub(crate) struct EscapingBoundVarInTyOfAssocConstBinding {
279281
pub var_def_kind: &'static str,
280282
#[label(hir_analysis_var_defined_here_label)]
281283
pub var_defined_here_label: Span,
284+
#[subdiagnostic]
285+
pub ty_note: Option<TyOfAssocConstBindingNote<'tcx>>,
286+
}
287+
288+
#[derive(Subdiagnostic, Clone, Copy)]
289+
#[note(hir_analysis_ty_of_assoc_const_binding_note)]
290+
pub(crate) struct TyOfAssocConstBindingNote<'tcx> {
291+
pub assoc_const: Ident,
292+
pub ty: Ty<'tcx>,
282293
}
283294

284295
#[derive(Subdiagnostic)]

tests/ui/associated-consts/assoc-const-eq-esc-bound-var-in-ty.rs

+1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ fn take(_: impl for<'r> Trait<'r, K = { &() }>) {}
1010
//~^ ERROR the type of the associated constant `K` cannot capture late-bound generic parameters
1111
//~| NOTE its type cannot capture the late-bound lifetime parameter `'r`
1212
//~| NOTE the late-bound lifetime parameter `'r` is defined here
13+
//~| NOTE `K` has type `&'r ()`
1314

1415
fn main() {}

tests/ui/associated-consts/assoc-const-eq-esc-bound-var-in-ty.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ LL | fn take(_: impl for<'r> Trait<'r, K = { &() }>) {}
55
| -- ^ its type cannot capture the late-bound lifetime parameter `'r`
66
| |
77
| the late-bound lifetime parameter `'r` is defined here
8+
|
9+
= note: `K` has type `&'r ()`
810

911
error: aborting due to 1 previous error
1012

tests/ui/associated-consts/assoc-const-eq-param-in-ty.rs

+4
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,15 @@ fn take0<'r, A: 'r, const Q: usize>(_: impl Trait<'r, A, Q, K = { loop {} }>) {}
1010
//~^ ERROR the type of the associated constant `K` must not depend on generic parameters
1111
//~| NOTE its type must not depend on the lifetime parameter `'r`
1212
//~| NOTE the lifetime parameter `'r` is defined here
13+
//~| NOTE `K` has type `&'r [A; Q]`
1314
//~| ERROR the type of the associated constant `K` must not depend on generic parameters
1415
//~| NOTE its type must not depend on the type parameter `A`
1516
//~| NOTE the type parameter `A` is defined here
17+
//~| NOTE `K` has type `&'r [A; Q]`
1618
//~| ERROR the type of the associated constant `K` must not depend on generic parameters
1719
//~| NOTE its type must not depend on the const parameter `Q`
1820
//~| NOTE the const parameter `Q` is defined here
21+
//~| NOTE `K` has type `&'r [A; Q]`
1922

2023
trait Project {
2124
const SELF: Self;
@@ -30,5 +33,6 @@ fn take2<P: Project<SELF = {}>>(_: P) {}
3033
//~^ ERROR the type of the associated constant `SELF` must not depend on generic parameters
3134
//~| NOTE its type must not depend on the type parameter `P`
3235
//~| NOTE the type parameter `P` is defined here
36+
//~| NOTE `SELF` has type `P`
3337

3438
fn main() {}

tests/ui/associated-consts/assoc-const-eq-param-in-ty.stderr

+10-2
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@ error: the type of the associated constant `K` must not depend on generic parame
33
|
44
LL | fn take0<'r, A: 'r, const Q: usize>(_: impl Trait<'r, A, Q, K = { loop {} }>) {}
55
| -- the lifetime parameter `'r` is defined here ^ its type must not depend on the lifetime parameter `'r`
6+
|
7+
= note: `K` has type `&'r [A; Q]`
68

79
error: the type of the associated constant `K` must not depend on generic parameters
810
--> $DIR/assoc-const-eq-param-in-ty.rs:9:61
911
|
1012
LL | fn take0<'r, A: 'r, const Q: usize>(_: impl Trait<'r, A, Q, K = { loop {} }>) {}
1113
| - the type parameter `A` is defined here ^ its type must not depend on the type parameter `A`
14+
|
15+
= note: `K` has type `&'r [A; Q]`
1216

1317
error: the type of the associated constant `K` must not depend on generic parameters
1418
--> $DIR/assoc-const-eq-param-in-ty.rs:9:61
@@ -17,9 +21,11 @@ LL | fn take0<'r, A: 'r, const Q: usize>(_: impl Trait<'r, A, Q, K = { loop {} }
1721
| - ^ its type must not depend on the const parameter `Q`
1822
| |
1923
| the const parameter `Q` is defined here
24+
|
25+
= note: `K` has type `&'r [A; Q]`
2026

2127
error: the type of the associated constant `SELF` must not depend on `impl Trait`
22-
--> $DIR/assoc-const-eq-param-in-ty.rs:24:26
28+
--> $DIR/assoc-const-eq-param-in-ty.rs:27:26
2329
|
2430
LL | fn take1(_: impl Project<SELF = {}>) {}
2531
| -------------^^^^------
@@ -28,12 +34,14 @@ LL | fn take1(_: impl Project<SELF = {}>) {}
2834
| the `impl Trait` is specified here
2935

3036
error: the type of the associated constant `SELF` must not depend on generic parameters
31-
--> $DIR/assoc-const-eq-param-in-ty.rs:29:21
37+
--> $DIR/assoc-const-eq-param-in-ty.rs:32:21
3238
|
3339
LL | fn take2<P: Project<SELF = {}>>(_: P) {}
3440
| - ^^^^ its type must not depend on the type parameter `P`
3541
| |
3642
| the type parameter `P` is defined here
43+
|
44+
= note: `SELF` has type `P`
3745

3846
error: aborting due to 5 previous errors
3947

0 commit comments

Comments
 (0)