Skip to content

Commit 3e0dd24

Browse files
committed
Auto merge of #77546 - lcnr:impl-trait-closure, r=eddyb
fix def collector for impl trait fixes #77329 We now consistently make `impl Trait` a hir owner, requiring some special casing for synthetic generic params. r? `@eddyb`
2 parents 17cc9b6 + 567d55e commit 3e0dd24

File tree

9 files changed

+98
-19
lines changed

9 files changed

+98
-19
lines changed

Diff for: compiler/rustc_ast_lowering/src/lib.rs

+15-5
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
538538
}
539539
self.visit_fn_ret_ty(&f.decl.output)
540540
}
541+
TyKind::ImplTrait(def_node_id, _) => {
542+
self.lctx.allocate_hir_id_counter(def_node_id);
543+
self.with_hir_id_owner(Some(def_node_id), |this| {
544+
visit::walk_ty(this, t);
545+
});
546+
}
541547
_ => visit::walk_ty(self, t),
542548
}
543549
}
@@ -1347,10 +1353,14 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
13471353
// Add a definition for the in-band `Param`.
13481354
let def_id = self.resolver.local_def_id(def_node_id);
13491355

1350-
let hir_bounds = self.lower_param_bounds(
1351-
bounds,
1352-
ImplTraitContext::Universal(in_band_ty_params),
1353-
);
1356+
self.allocate_hir_id_counter(def_node_id);
1357+
1358+
let hir_bounds = self.with_hir_id_owner(def_node_id, |this| {
1359+
this.lower_param_bounds(
1360+
bounds,
1361+
ImplTraitContext::Universal(in_band_ty_params),
1362+
)
1363+
});
13541364
// Set the name to `impl Bound1 + Bound2`.
13551365
let ident = Ident::from_str_and_span(&pprust::ty_to_string(t), span);
13561366
in_band_ty_params.push(hir::GenericParam {
@@ -2201,7 +2211,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
22012211
.attrs
22022212
.iter()
22032213
.filter(|attr| self.sess.check_name(attr, sym::rustc_synthetic))
2204-
.map(|_| hir::SyntheticTyParamKind::ImplTrait)
2214+
.map(|_| hir::SyntheticTyParamKind::FromAttr)
22052215
.next(),
22062216
};
22072217

Diff for: compiler/rustc_hir/src/hir.rs

+2
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,8 @@ impl Generics<'hir> {
486486
#[derive(HashStable_Generic)]
487487
pub enum SyntheticTyParamKind {
488488
ImplTrait,
489+
// Created by the `#[rustc_synthetic]` attribute.
490+
FromAttr,
489491
}
490492

491493
/// A where-clause in a definition.

Diff for: compiler/rustc_middle/src/hir/map/collector.rs

+20-2
Original file line numberDiff line numberDiff line change
@@ -360,8 +360,26 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
360360
}
361361

362362
fn visit_generic_param(&mut self, param: &'hir GenericParam<'hir>) {
363-
self.insert(param.span, param.hir_id, Node::GenericParam(param));
364-
intravisit::walk_generic_param(self, param);
363+
if let hir::GenericParamKind::Type {
364+
synthetic: Some(hir::SyntheticTyParamKind::ImplTrait),
365+
..
366+
} = param.kind
367+
{
368+
debug_assert_eq!(
369+
param.hir_id.owner,
370+
self.definitions.opt_hir_id_to_local_def_id(param.hir_id).unwrap()
371+
);
372+
self.with_dep_node_owner(param.hir_id.owner, param, |this, hash| {
373+
this.insert_with_hash(param.span, param.hir_id, Node::GenericParam(param), hash);
374+
375+
this.with_parent(param.hir_id, |this| {
376+
intravisit::walk_generic_param(this, param);
377+
});
378+
});
379+
} else {
380+
self.insert(param.span, param.hir_id, Node::GenericParam(param));
381+
intravisit::walk_generic_param(self, param);
382+
}
365383
}
366384

367385
fn visit_trait_item(&mut self, ti: &'hir TraitItem<'hir>) {

Diff for: compiler/rustc_passes/src/hir_id_validator.rs

+13
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,17 @@ impl<'a, 'hir> intravisit::Visitor<'hir> for HirIdValidator<'a, 'hir> {
163163
// we are currently in. So for those it's correct that they have a
164164
// different owner.
165165
}
166+
167+
fn visit_generic_param(&mut self, param: &'hir hir::GenericParam<'hir>) {
168+
if let hir::GenericParamKind::Type {
169+
synthetic: Some(hir::SyntheticTyParamKind::ImplTrait),
170+
..
171+
} = param.kind
172+
{
173+
// Synthetic impl trait parameters are owned by the node of the desugared type.
174+
// This means it is correct for them to have a different owner.
175+
} else {
176+
intravisit::walk_generic_param(self, param);
177+
}
178+
}
166179
}

Diff for: compiler/rustc_resolve/src/def_collector.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -240,13 +240,13 @@ impl<'a, 'b> visit::Visitor<'a> for DefCollector<'a, 'b> {
240240

241241
fn visit_ty(&mut self, ty: &'a Ty) {
242242
match ty.kind {
243-
TyKind::MacCall(..) => return self.visit_macro_invoc(ty.id),
243+
TyKind::MacCall(..) => self.visit_macro_invoc(ty.id),
244244
TyKind::ImplTrait(node_id, _) => {
245-
self.create_def(node_id, DefPathData::ImplTrait, ty.span);
245+
let parent_def = self.create_def(node_id, DefPathData::ImplTrait, ty.span);
246+
self.with_parent(parent_def, |this| visit::walk_ty(this, ty));
246247
}
247-
_ => {}
248+
_ => visit::walk_ty(self, ty),
248249
}
249-
visit::walk_ty(self, ty);
250250
}
251251

252252
fn visit_stmt(&mut self, stmt: &'a Stmt) {

Diff for: compiler/rustc_save_analysis/src/dump_visitor.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,15 @@ impl<'tcx> DumpVisitor<'tcx> {
320320
for param in generics.params {
321321
match param.kind {
322322
hir::GenericParamKind::Lifetime { .. } => {}
323+
hir::GenericParamKind::Type {
324+
synthetic: Some(hir::SyntheticTyParamKind::ImplTrait),
325+
..
326+
} => {
327+
return self
328+
.nest_typeck_results(self.tcx.hir().local_def_id(param.hir_id), |this| {
329+
this.visit_generics(generics)
330+
});
331+
}
323332
hir::GenericParamKind::Type { .. } => {
324333
let param_ss = param.name.ident().span;
325334
let name = escape(self.span.snippet(param_ss));
@@ -351,7 +360,8 @@ impl<'tcx> DumpVisitor<'tcx> {
351360
hir::GenericParamKind::Const { .. } => {}
352361
}
353362
}
354-
self.visit_generics(generics);
363+
364+
self.visit_generics(generics)
355365
}
356366

357367
fn process_fn(

Diff for: compiler/rustc_typeck/src/astconv/generics.rs

+12-7
Original file line numberDiff line numberDiff line change
@@ -548,13 +548,18 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
548548
generics: &ty::Generics,
549549
) -> bool {
550550
let explicit = !seg.infer_args;
551-
let impl_trait = generics.params.iter().any(|param| match param.kind {
552-
ty::GenericParamDefKind::Type {
553-
synthetic: Some(hir::SyntheticTyParamKind::ImplTrait),
554-
..
555-
} => true,
556-
_ => false,
557-
});
551+
let impl_trait =
552+
generics.params.iter().any(|param| match param.kind {
553+
ty::GenericParamDefKind::Type {
554+
synthetic:
555+
Some(
556+
hir::SyntheticTyParamKind::ImplTrait
557+
| hir::SyntheticTyParamKind::FromAttr,
558+
),
559+
..
560+
} => true,
561+
_ => false,
562+
});
558563

559564
if explicit && impl_trait {
560565
let spans = seg

Diff for: src/test/ui/impl-trait/closure-in-impl-trait-arg.rs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// run-pass
2+
#![allow(unused_must_use)]
3+
fn bug(_: impl Iterator<Item = [(); { |x: u32| { x }; 4 }]>) {}
4+
5+
fn main() {
6+
bug(std::iter::empty());
7+
}

Diff for: src/test/ui/impl-trait/closure-in-impl-trait.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// run-pass
2+
#![allow(unused_must_use)]
3+
fn bug<T>() -> impl Iterator<Item = [(); { |x: u32| { x }; 4 }]> {
4+
std::iter::empty()
5+
}
6+
7+
fn ok<T>() -> Box<dyn Iterator<Item = [(); { |x: u32| { x }; 4 }]>> {
8+
Box::new(std::iter::empty())
9+
}
10+
11+
fn main() {
12+
for _item in ok::<u32>() {}
13+
for _item in bug::<u32>() {}
14+
}

0 commit comments

Comments
 (0)