Skip to content

Commit 1d72ed9

Browse files
committed
fix(hygiene): Resolve lifetime params from impl blocks in generic args
Early-bound lifetime parameters no longer store the def id of the parameter definition directly. Instead, the parameter index has to be applied to the correct binding item manually. This is almost always the closest owner node, which we now use instead.
1 parent f2ae76d commit 1d72ed9

2 files changed

Lines changed: 26 additions & 3 deletions

File tree

mutest-emit/src/codegen/hygiene.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,7 @@ impl<'tcx, 'op> MacroExpansionSanitizer<'tcx, 'op> {
718718
let parent_def_id = self.tcx.parent(qres.def_id());
719719
match self.tcx.def_kind(parent_def_id) {
720720
hir::DefKind::Trait | hir::DefKind::TraitAlias => {
721-
let qself_ty_ast = self.sanitize_ty(qself_ty, parent_def_id, qself_ty_hir.span);
721+
let qself_ty_ast = self.sanitize_ty(qself_ty, node_hir_id.owner.to_def_id(), qself_ty_hir.span);
722722

723723
let parent_path_segment_res = match &path.segments[..] {
724724
[.., parent_path_segment, _] => self.def_res.node_res(parent_path_segment.id),
@@ -737,7 +737,7 @@ impl<'tcx, 'op> MacroExpansionSanitizer<'tcx, 'op> {
737737
let trait_generics = self.tcx.generics_of(parent_def_id);
738738
let trait_args = &node_args[(trait_generics.has_self as usize)..trait_generics.count()];
739739

740-
self.sanitize_generic_args(trait_args, parent_def_id, qself_ty_hir.span)
740+
self.sanitize_generic_args(trait_args, node_hir_id.owner.to_def_id(), qself_ty_hir.span)
741741
}
742742

743743
// Bound params from local trait bounds corresponding to parameter types to the trait subpath.
@@ -864,7 +864,7 @@ impl<'tcx, 'op> MacroExpansionSanitizer<'tcx, 'op> {
864864
}
865865

866866
hir::DefKind::Impl { of_trait: false } => {
867-
let qself_ty_ast = self.sanitize_ty(qself_ty, parent_def_id, qself_ty_hir.span);
867+
let qself_ty_ast = self.sanitize_ty(qself_ty, node_hir_id.owner.to_def_id(), qself_ty_hir.span);
868868

869869
// NOTE: We always add a qualified self type, so we can safely ignore the indicator return value.
870870
let _ = self.sanitize_path(path, qres.expect_non_local(), None);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//@ build
2+
//@ stderr: empty
3+
4+
#![feature(decl_macro)]
5+
6+
#![allow(unused)]
7+
8+
macro m() {
9+
trait TraitWithLifetimeParam<'a> {
10+
type Value;
11+
fn f() -> Self::Value;
12+
}
13+
14+
// TEST: Early-bound lifetime parameter from impl block.
15+
struct StructWithLifetimeParam<'a>(std::marker::PhantomData<&'a ()>);
16+
impl<'a> TraitWithLifetimeParam<'a> for StructWithLifetimeParam<'a> {
17+
type Value = ();
18+
// NOTE: `Self` triggers the hygienic expansion into `StructWithLifetimeParam<'a>`.
19+
fn f() -> Self::Value {}
20+
}
21+
}
22+
23+
m!();

0 commit comments

Comments
 (0)