Skip to content

Commit

Permalink
Fix associated type errors too
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Feb 24, 2025
1 parent c811f8f commit b34ce33
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 0 deletions.
2 changes: 2 additions & 0 deletions compiler/rustc_hir_analysis/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,8 @@ hir_analysis_variances_of = {$variances}
hir_analysis_where_clause_on_main = `main` function is not allowed to have a `where` clause
.label = `main` cannot have a `where` clause
hir_analysis_within_macro = within this macro
hir_analysis_wrong_number_of_generic_arguments_to_intrinsic =
intrinsic has wrong number of {$descr} parameters: found {$found}, expected {$expected}
.label = expected {$expected} {$descr} {$expected ->
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_hir_analysis/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ pub(crate) struct AssocItemNotFound<'a> {
pub label: Option<AssocItemNotFoundLabel<'a>>,
#[subdiagnostic]
pub sugg: Option<AssocItemNotFoundSugg<'a>>,
#[label(hir_analysis_within_macro)]
pub within_macro_span: Option<Span>,
}

#[derive(Subdiagnostic)]
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
qself: &qself_str,
label: None,
sugg: None,
// Try to get the span of the identifier within the path's syntax context
// (if that's different).
within_macro_span: assoc_name.span.within_macro(span),
};

if is_dummy {
Expand Down
23 changes: 23 additions & 0 deletions tests/ui/associated-types/ident-from-macro-expansion.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
trait Trait {}
impl Trait for () {}

macro_rules! fully_qualified {
($id:ident) => {
<() as Trait>::$id
}
}

macro_rules! type_dependent {
($t:ident, $id:ident) => {
T::$id
}
}

fn t<T: Trait>() {
let x: fully_qualified!(Assoc);
//~^ ERROR cannot find associated type `Assoc` in trait `Trait`
let x: type_dependent!(T, Assoc);
//~^ ERROR associated type `Assoc` not found for `T`
}

fn main() {}
22 changes: 22 additions & 0 deletions tests/ui/associated-types/ident-from-macro-expansion.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
error[E0576]: cannot find associated type `Assoc` in trait `Trait`
--> $DIR/ident-from-macro-expansion.rs:17:29
|
LL | <() as Trait>::$id
| --- within this macro
...
LL | let x: fully_qualified!(Assoc);
| ^^^^^ not found in `Trait`

error[E0220]: associated type `Assoc` not found for `T`
--> $DIR/ident-from-macro-expansion.rs:19:31
|
LL | T::$id
| --- within this macro
...
LL | let x: type_dependent!(T, Assoc);
| ^^^^^ associated type `Assoc` not found

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0220, E0576.
For more information about an error, try `rustc --explain E0220`.

0 comments on commit b34ce33

Please sign in to comment.