Skip to content

Commit ac3d4cc

Browse files
committed
Explain that associated types and consts can't be accessed directly on the trait's path
1 parent ed6468d commit ac3d4cc

File tree

4 files changed

+44
-5
lines changed

4 files changed

+44
-5
lines changed

src/librustc/traits/error_reporting.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2075,7 +2075,30 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
20752075
}
20762076
let mut err = self.need_type_info_err(body_id, span, self_ty, ErrorCode::E0283);
20772077
err.note(&format!("cannot resolve `{}`", predicate));
2078-
if let (Ok(ref snippet), ObligationCauseCode::BindingObligation(ref def_id, _)) =
2078+
if let ObligationCauseCode::ItemObligation(def_id) = obligation.cause.code {
2079+
if let Some(assoc_item) = self.tcx.opt_associated_item(def_id) {
2080+
if let ty::AssocKind::Const | ty::AssocKind::Type = assoc_item.kind {
2081+
err.note(&format!(
2082+
"{}s cannot be accessed directly on a `trait`, they can only be \
2083+
accessed through a specific `impl`",
2084+
assoc_item.kind.suggestion_descr(),
2085+
));
2086+
err.span_suggestion(
2087+
span,
2088+
"use the fully qualified path to an implementation",
2089+
format!(
2090+
"<Type as {}>::{}",
2091+
self.tcx.def_path_str(trait_ref.def_id()),
2092+
assoc_item.ident
2093+
),
2094+
Applicability::HasPlaceholders,
2095+
);
2096+
}
2097+
}
2098+
} else if let (
2099+
Ok(ref snippet),
2100+
ObligationCauseCode::BindingObligation(ref def_id, _),
2101+
) =
20792102
(self.tcx.sess.source_map().span_to_snippet(span), &obligation.cause.code)
20802103
{
20812104
let generics = self.tcx.generics_of(*def_id);

src/test/ui/associated-const/issue-63496.stderr

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@ LL | const C: usize;
55
| --------------- required by `A::C`
66
LL |
77
LL | fn f() -> ([u8; A::C], [u8; A::C]);
8-
| ^^^^ cannot infer type
8+
| ^^^^
9+
| |
10+
| cannot infer type
11+
| help: use the fully qualified path to an implementation: `<Type as A>::C`
912
|
1013
= note: cannot resolve `_: A`
14+
= note: associated constants cannot be accessed directly on a `trait`, they can only be accessed through a specific `impl`
1115

1216
error[E0283]: type annotations needed
1317
--> $DIR/issue-63496.rs:4:33
@@ -16,9 +20,13 @@ LL | const C: usize;
1620
| --------------- required by `A::C`
1721
LL |
1822
LL | fn f() -> ([u8; A::C], [u8; A::C]);
19-
| ^^^^ cannot infer type
23+
| ^^^^
24+
| |
25+
| cannot infer type
26+
| help: use the fully qualified path to an implementation: `<Type as A>::C`
2027
|
2128
= note: cannot resolve `_: A`
29+
= note: associated constants cannot be accessed directly on a `trait`, they can only be accessed through a specific `impl`
2230

2331
error: aborting due to 2 previous errors
2432

src/test/ui/associated-item/issue-48027.stderr

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,13 @@ error[E0283]: type annotations needed
1313
LL | const X: usize;
1414
| --------------- required by `Bar::X`
1515
LL | fn return_n(&self) -> [u8; Bar::X];
16-
| ^^^^^^ cannot infer type
16+
| ^^^^^^
17+
| |
18+
| cannot infer type
19+
| help: use the fully qualified path to an implementation: `<Type as Bar>::X`
1720
|
1821
= note: cannot resolve `_: Bar`
22+
= note: associated constants cannot be accessed directly on a `trait`, they can only be accessed through a specific `impl`
1923

2024
error: aborting due to 2 previous errors
2125

src/test/ui/issues/issue-58022.stderr

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@ LL | const SIZE: usize;
1111
| ------------------ required by `Foo::SIZE`
1212
LL |
1313
LL | fn new(slice: &[u8; Foo::SIZE]) -> Self;
14-
| ^^^^^^^^^ cannot infer type
14+
| ^^^^^^^^^
15+
| |
16+
| cannot infer type
17+
| help: use the fully qualified path to an implementation: `<Type as Foo>::SIZE`
1518
|
1619
= note: cannot resolve `_: Foo`
20+
= note: associated constants cannot be accessed directly on a `trait`, they can only be accessed through a specific `impl`
1721

1822
error: aborting due to 2 previous errors
1923

0 commit comments

Comments
 (0)