Skip to content

Explain that associated types and consts can't be accessed directly on the trait's path #67747

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion src/librustc/traits/error_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2075,7 +2075,12 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
}
let mut err = self.need_type_info_err(body_id, span, self_ty, ErrorCode::E0283);
err.note(&format!("cannot resolve `{}`", predicate));
if let (Ok(ref snippet), ObligationCauseCode::BindingObligation(ref def_id, _)) =
if let ObligationCauseCode::ItemObligation(def_id) = obligation.cause.code {
self.suggest_fully_qualified_path(&mut err, def_id, span, trait_ref.def_id());
} else if let (
Ok(ref snippet),
ObligationCauseCode::BindingObligation(ref def_id, _),
) =
(self.tcx.sess.source_map().span_to_snippet(span), &obligation.cause.code)
{
let generics = self.tcx.generics_of(*def_id);
Expand Down Expand Up @@ -2173,6 +2178,30 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
err.emit();
}

fn suggest_fully_qualified_path(
&self,
err: &mut DiagnosticBuilder<'_>,
def_id: DefId,
span: Span,
trait_ref: DefId,
) {
if let Some(assoc_item) = self.tcx.opt_associated_item(def_id) {
if let ty::AssocKind::Const | ty::AssocKind::Type = assoc_item.kind {
err.note(&format!(
"{}s cannot be accessed directly on a `trait`, they can only be \
accessed through a specific `impl`",
assoc_item.kind.suggestion_descr(),
));
err.span_suggestion(
span,
"use the fully qualified path to an implementation",
format!("<Type as {}>::{}", self.tcx.def_path_str(trait_ref), assoc_item.ident),
Applicability::HasPlaceholders,
);
}
}
}

/// Returns `true` if the trait predicate may apply for *some* assignment
/// to the type parameters.
fn predicate_can_apply(
Expand Down
12 changes: 10 additions & 2 deletions src/test/ui/associated-const/issue-63496.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ LL | const C: usize;
| --------------- required by `A::C`
LL |
LL | fn f() -> ([u8; A::C], [u8; A::C]);
| ^^^^ cannot infer type
| ^^^^
| |
| cannot infer type
| help: use the fully qualified path to an implementation: `<Type as A>::C`
|
= note: cannot resolve `_: A`
= note: associated constants cannot be accessed directly on a `trait`, they can only be accessed through a specific `impl`

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

error: aborting due to 2 previous errors

Expand Down
6 changes: 5 additions & 1 deletion src/test/ui/associated-item/issue-48027.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ error[E0283]: type annotations needed
LL | const X: usize;
| --------------- required by `Bar::X`
LL | fn return_n(&self) -> [u8; Bar::X];
| ^^^^^^ cannot infer type
| ^^^^^^
| |
| cannot infer type
| help: use the fully qualified path to an implementation: `<Type as Bar>::X`
|
= note: cannot resolve `_: Bar`
= note: associated constants cannot be accessed directly on a `trait`, they can only be accessed through a specific `impl`

error: aborting due to 2 previous errors

Expand Down
6 changes: 5 additions & 1 deletion src/test/ui/issues/issue-58022.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ LL | const SIZE: usize;
| ------------------ required by `Foo::SIZE`
LL |
LL | fn new(slice: &[u8; Foo::SIZE]) -> Self;
| ^^^^^^^^^ cannot infer type
| ^^^^^^^^^
| |
| cannot infer type
| help: use the fully qualified path to an implementation: `<Type as Foo>::SIZE`
|
= note: cannot resolve `_: Foo`
= note: associated constants cannot be accessed directly on a `trait`, they can only be accessed through a specific `impl`

error: aborting due to 2 previous errors

Expand Down