Skip to content

Commit f71807d

Browse files
committed
make parent_id not optional
It'd be pretty buggy if `None` were supplied anyway; we'd skip over in-band lifetimes completely!
1 parent a5743b3 commit f71807d

File tree

1 file changed

+62
-80
lines changed

1 file changed

+62
-80
lines changed

src/librustc/hir/lowering.rs

+62-80
Original file line numberDiff line numberDiff line change
@@ -547,11 +547,7 @@ impl<'a> LoweringContext<'a> {
547547
// Creates a new hir::GenericParam for every new lifetime and type parameter
548548
// encountered while evaluating `f`. Definitions are created with the parent
549549
// provided. If no `parent_id` is provided, no definitions will be returned.
550-
fn collect_in_band_defs<T, F>(
551-
&mut self,
552-
parent_id: Option<DefId>,
553-
f: F,
554-
) -> (Vec<hir::GenericParam>, T)
550+
fn collect_in_band_defs<T, F>(&mut self, parent_id: DefId, f: F) -> (Vec<hir::GenericParam>, T)
555551
where
556552
F: FnOnce(&mut LoweringContext) -> T,
557553
{
@@ -568,42 +564,38 @@ impl<'a> LoweringContext<'a> {
568564
let in_band_ty_params = self.in_band_ty_params.split_off(0);
569565
let lifetimes_to_define = self.lifetimes_to_define.split_off(0);
570566

571-
let mut params = match parent_id {
572-
Some(parent_id) => lifetimes_to_define
573-
.into_iter()
574-
.map(|(span, name)| {
575-
let def_node_id = self.next_id().node_id;
567+
let params = lifetimes_to_define
568+
.into_iter()
569+
.map(|(span, name)| {
570+
let def_node_id = self.next_id().node_id;
571+
572+
// Add a definition for the in-band lifetime def
573+
self.resolver.definitions().create_def_with_parent(
574+
parent_id.index,
575+
def_node_id,
576+
DefPathData::LifetimeDef(name.as_str()),
577+
DefIndexAddressSpace::High,
578+
Mark::root(),
579+
span,
580+
);
576581

577-
// Add a definition for the in-band lifetime def
578-
self.resolver.definitions().create_def_with_parent(
579-
parent_id.index,
580-
def_node_id,
581-
DefPathData::LifetimeDef(name.as_str()),
582-
DefIndexAddressSpace::High,
583-
Mark::root(),
582+
hir::GenericParam::Lifetime(hir::LifetimeDef {
583+
lifetime: hir::Lifetime {
584+
id: def_node_id,
584585
span,
585-
);
586-
587-
hir::GenericParam::Lifetime(hir::LifetimeDef {
588-
lifetime: hir::Lifetime {
589-
id: def_node_id,
590-
span,
591-
name: hir::LifetimeName::Name(name),
592-
},
593-
bounds: Vec::new().into(),
594-
pure_wrt_drop: false,
595-
in_band: true,
596-
})
586+
name: hir::LifetimeName::Name(name),
587+
},
588+
bounds: Vec::new().into(),
589+
pure_wrt_drop: false,
590+
in_band: true,
597591
})
598-
.collect(),
599-
None => Vec::new(),
600-
};
601-
602-
params.extend(
603-
in_band_ty_params
604-
.into_iter()
605-
.map(|tp| hir::GenericParam::Type(tp)),
606-
);
592+
})
593+
.chain(
594+
in_band_ty_params
595+
.into_iter()
596+
.map(|tp| hir::GenericParam::Type(tp)),
597+
)
598+
.collect();
607599

608600
(params, res)
609601
}
@@ -654,20 +646,17 @@ impl<'a> LoweringContext<'a> {
654646
fn add_in_band_defs<F, T>(
655647
&mut self,
656648
generics: &Generics,
657-
parent_id: Option<DefId>,
649+
parent_id: DefId,
658650
f: F,
659651
) -> (hir::Generics, T)
660652
where
661653
F: FnOnce(&mut LoweringContext) -> T,
662654
{
663655
let (in_band_defs, (mut lowered_generics, res)) = self.with_in_scope_lifetime_defs(
664-
generics
665-
.params
666-
.iter()
667-
.filter_map(|p| match p {
668-
GenericParam::Lifetime(ld) => Some(ld),
669-
_ => None,
670-
}),
656+
generics.params.iter().filter_map(|p| match p {
657+
GenericParam::Lifetime(ld) => Some(ld),
658+
_ => None,
659+
}),
671660
|this| {
672661
this.collect_in_band_defs(parent_id, |this| {
673662
(this.lower_generics(generics), f(this))
@@ -926,12 +915,10 @@ impl<'a> LoweringContext<'a> {
926915
hir::TyRptr(lifetime, self.lower_mt(mt, itctx))
927916
}
928917
TyKind::BareFn(ref f) => self.with_in_scope_lifetime_defs(
929-
f.generic_params
930-
.iter()
931-
.filter_map(|p| match p {
932-
GenericParam::Lifetime(ld) => Some(ld),
933-
_ => None,
934-
}),
918+
f.generic_params.iter().filter_map(|p| match p {
919+
GenericParam::Lifetime(ld) => Some(ld),
920+
_ => None,
921+
}),
935922
|this| {
936923
hir::TyBareFn(P(hir::BareFnTy {
937924
generic_params: this.lower_generic_params(&f.generic_params, &NodeMap()),
@@ -1876,12 +1863,10 @@ impl<'a> LoweringContext<'a> {
18761863
span,
18771864
}) => {
18781865
self.with_in_scope_lifetime_defs(
1879-
bound_generic_params
1880-
.iter()
1881-
.filter_map(|p| match p {
1882-
GenericParam::Lifetime(ld) => Some(ld),
1883-
_ => None,
1884-
}),
1866+
bound_generic_params.iter().filter_map(|p| match p {
1867+
GenericParam::Lifetime(ld) => Some(ld),
1868+
_ => None,
1869+
}),
18851870
|this| {
18861871
hir::WherePredicate::BoundPredicate(hir::WhereBoundPredicate {
18871872
bound_generic_params: this.lower_generic_params(
@@ -2097,14 +2082,14 @@ impl<'a> LoweringContext<'a> {
20972082
hir::ItemConst(self.lower_ty(t, ImplTraitContext::Disallowed), value)
20982083
}
20992084
ItemKind::Fn(ref decl, unsafety, constness, abi, ref generics, ref body) => {
2100-
let fn_def_id = self.resolver.definitions().opt_local_def_id(id);
2085+
let fn_def_id = self.resolver.definitions().local_def_id(id);
21012086
self.with_new_scopes(|this| {
21022087
let body_id = this.lower_body(Some(decl), |this| {
21032088
let body = this.lower_block(body, false);
21042089
this.expr_block(body, ThinVec::new())
21052090
});
21062091
let (generics, fn_decl) = this.add_in_band_defs(generics, fn_def_id, |this| {
2107-
this.lower_fn_decl(decl, fn_def_id, true)
2092+
this.lower_fn_decl(decl, Some(fn_def_id), true)
21082093
});
21092094

21102095
hir::ItemFn(
@@ -2151,7 +2136,7 @@ impl<'a> LoweringContext<'a> {
21512136
ref ty,
21522137
ref impl_items,
21532138
) => {
2154-
let def_id = self.resolver.definitions().opt_local_def_id(id);
2139+
let def_id = self.resolver.definitions().local_def_id(id);
21552140
let (generics, (ifce, lowered_ty)) =
21562141
self.add_in_band_defs(ast_generics, def_id, |this| {
21572142
let ifce = ifce.as_ref().map(|trait_ref| {
@@ -2170,13 +2155,10 @@ impl<'a> LoweringContext<'a> {
21702155
});
21712156

21722157
let new_impl_items = self.with_in_scope_lifetime_defs(
2173-
ast_generics
2174-
.params
2175-
.iter()
2176-
.filter_map(|p| match p {
2177-
GenericParam::Lifetime(ld) => Some(ld),
2178-
_ => None,
2179-
}),
2158+
ast_generics.params.iter().filter_map(|p| match p {
2159+
GenericParam::Lifetime(ld) => Some(ld),
2160+
_ => None,
2161+
}),
21802162
|this| {
21812163
impl_items
21822164
.iter()
@@ -2341,7 +2323,7 @@ impl<'a> LoweringContext<'a> {
23412323
fn lower_trait_item(&mut self, i: &TraitItem) -> hir::TraitItem {
23422324
self.with_parent_def(i.id, |this| {
23432325
let LoweredNodeId { node_id, hir_id } = this.lower_node_id(i.id);
2344-
let fn_def_id = this.resolver.definitions().opt_local_def_id(node_id);
2326+
let trait_item_def_id = this.resolver.definitions().local_def_id(node_id);
23452327

23462328
let (generics, node) = match i.node {
23472329
TraitItemKind::Const(ref ty, ref default) => (
@@ -2355,9 +2337,9 @@ impl<'a> LoweringContext<'a> {
23552337
),
23562338
TraitItemKind::Method(ref sig, None) => {
23572339
let names = this.lower_fn_args_to_names(&sig.decl);
2358-
this.add_in_band_defs(&i.generics, fn_def_id, |this| {
2340+
this.add_in_band_defs(&i.generics, trait_item_def_id, |this| {
23592341
hir::TraitItemKind::Method(
2360-
this.lower_method_sig(sig, fn_def_id, false),
2342+
this.lower_method_sig(sig, trait_item_def_id, false),
23612343
hir::TraitMethod::Required(names),
23622344
)
23632345
})
@@ -2368,9 +2350,9 @@ impl<'a> LoweringContext<'a> {
23682350
this.expr_block(body, ThinVec::new())
23692351
});
23702352

2371-
this.add_in_band_defs(&i.generics, fn_def_id, |this| {
2353+
this.add_in_band_defs(&i.generics, trait_item_def_id, |this| {
23722354
hir::TraitItemKind::Method(
2373-
this.lower_method_sig(sig, fn_def_id, false),
2355+
this.lower_method_sig(sig, trait_item_def_id, false),
23742356
hir::TraitMethod::Provided(body_id),
23752357
)
23762358
})
@@ -2427,7 +2409,7 @@ impl<'a> LoweringContext<'a> {
24272409
fn lower_impl_item(&mut self, i: &ImplItem) -> hir::ImplItem {
24282410
self.with_parent_def(i.id, |this| {
24292411
let LoweredNodeId { node_id, hir_id } = this.lower_node_id(i.id);
2430-
let fn_def_id = this.resolver.definitions().opt_local_def_id(node_id);
2412+
let impl_item_def_id = this.resolver.definitions().local_def_id(node_id);
24312413

24322414
let (generics, node) = match i.node {
24332415
ImplItemKind::Const(ref ty, ref expr) => {
@@ -2447,9 +2429,9 @@ impl<'a> LoweringContext<'a> {
24472429
});
24482430
let impl_trait_return_allow = !this.is_in_trait_impl;
24492431

2450-
this.add_in_band_defs(&i.generics, fn_def_id, |this| {
2432+
this.add_in_band_defs(&i.generics, impl_item_def_id, |this| {
24512433
hir::ImplItemKind::Method(
2452-
this.lower_method_sig(sig, fn_def_id, impl_trait_return_allow),
2434+
this.lower_method_sig(sig, impl_item_def_id, impl_trait_return_allow),
24532435
body_id,
24542436
)
24552437
})
@@ -2575,10 +2557,10 @@ impl<'a> LoweringContext<'a> {
25752557
attrs: this.lower_attrs(&i.attrs),
25762558
node: match i.node {
25772559
ForeignItemKind::Fn(ref fdec, ref generics) => {
2578-
// Disallow impl Trait in foreign items
25792560
let (generics, (fn_dec, fn_args)) =
2580-
this.add_in_band_defs(generics, Some(def_id), |this| {
2561+
this.add_in_band_defs(generics, def_id, |this| {
25812562
(
2563+
// Disallow impl Trait in foreign items
25822564
this.lower_fn_decl(fdec, None, false),
25832565
this.lower_fn_args_to_names(fdec),
25842566
)
@@ -2600,14 +2582,14 @@ impl<'a> LoweringContext<'a> {
26002582
fn lower_method_sig(
26012583
&mut self,
26022584
sig: &MethodSig,
2603-
fn_def_id: Option<DefId>,
2585+
fn_def_id: DefId,
26042586
impl_trait_return_allow: bool,
26052587
) -> hir::MethodSig {
26062588
hir::MethodSig {
26072589
abi: sig.abi,
26082590
unsafety: self.lower_unsafety(sig.unsafety),
26092591
constness: self.lower_constness(sig.constness),
2610-
decl: self.lower_fn_decl(&sig.decl, fn_def_id, impl_trait_return_allow),
2592+
decl: self.lower_fn_decl(&sig.decl, Some(fn_def_id), impl_trait_return_allow),
26112593
}
26122594
}
26132595

0 commit comments

Comments
 (0)