Skip to content

Commit 10be0dd

Browse files
committedJul 26, 2022
Replace LifetimeRes::Anonymous by LifetimeRes::Infer.
1 parent ab63591 commit 10be0dd

File tree

9 files changed

+51
-105
lines changed

9 files changed

+51
-105
lines changed
 

‎compiler/rustc_ast_lowering/src/lib.rs

+1-19
Original file line numberDiff line numberDiff line change
@@ -1883,25 +1883,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
18831883
}
18841884
hir::LifetimeName::Param(param, ParamName::Fresh)
18851885
}
1886-
LifetimeRes::Anonymous { binder } => {
1887-
let mut l_name = None;
1888-
if let Some(mut captured_lifetimes) = self.captured_lifetimes.take() {
1889-
if !captured_lifetimes.binders_to_ignore.contains(&binder) {
1890-
let p_id = self.next_node_id();
1891-
let p_def_id = self.create_def(
1892-
captured_lifetimes.parent_def_id,
1893-
p_id,
1894-
DefPathData::LifetimeNs(kw::UnderscoreLifetime),
1895-
);
1896-
captured_lifetimes
1897-
.captures
1898-
.insert(p_def_id, (span, p_id, ParamName::Fresh, res));
1899-
l_name = Some(hir::LifetimeName::Param(p_def_id, ParamName::Fresh));
1900-
}
1901-
self.captured_lifetimes = Some(captured_lifetimes);
1902-
};
1903-
l_name.unwrap_or(hir::LifetimeName::Underscore)
1904-
}
1886+
LifetimeRes::Infer => hir::LifetimeName::Infer,
19051887
LifetimeRes::Static => hir::LifetimeName::Static,
19061888
LifetimeRes::Error => hir::LifetimeName::Error,
19071889
res => panic!("Unexpected lifetime resolution {:?} for {:?} at {:?}", res, ident, span),

‎compiler/rustc_borrowck/src/diagnostics/region_name.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
589589

590590
hir::LifetimeName::Param(_, hir::ParamName::Fresh)
591591
| hir::LifetimeName::ImplicitObjectLifetimeDefault
592-
| hir::LifetimeName::Underscore => {
592+
| hir::LifetimeName::Infer => {
593593
// In this case, the user left off the lifetime; so
594594
// they wrote something like:
595595
//

‎compiler/rustc_hir/src/def.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -738,11 +738,8 @@ pub enum LifetimeRes {
738738
binder: NodeId,
739739
},
740740
/// This variant is used for anonymous lifetimes that we did not resolve during
741-
/// late resolution. Shifting the work to the HIR lifetime resolver.
742-
Anonymous {
743-
/// Id of the introducing place. See `Param`.
744-
binder: NodeId,
745-
},
741+
/// late resolution. Those lifetimes will be inferred by typechecking.
742+
Infer,
746743
/// Explicit `'static` lifetime.
747744
Static,
748745
/// Resolution failure.

‎compiler/rustc_hir/src/hir.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ pub enum LifetimeName {
108108
Error,
109109

110110
/// User wrote an anonymous lifetime, either `'_` or nothing.
111-
Underscore,
111+
/// The semantics of this lifetime should be inferred by typechecking code.
112+
Infer,
112113

113114
/// User wrote `'static`.
114115
Static,
@@ -118,7 +119,7 @@ impl LifetimeName {
118119
pub fn ident(&self) -> Ident {
119120
match *self {
120121
LifetimeName::ImplicitObjectLifetimeDefault | LifetimeName::Error => Ident::empty(),
121-
LifetimeName::Underscore => Ident::with_dummy_span(kw::UnderscoreLifetime),
122+
LifetimeName::Infer => Ident::with_dummy_span(kw::UnderscoreLifetime),
122123
LifetimeName::Static => Ident::with_dummy_span(kw::StaticLifetime),
123124
LifetimeName::Param(_, param_name) => param_name.ident(),
124125
}
@@ -127,7 +128,7 @@ impl LifetimeName {
127128
pub fn is_anonymous(&self) -> bool {
128129
match *self {
129130
LifetimeName::ImplicitObjectLifetimeDefault
130-
| LifetimeName::Underscore
131+
| LifetimeName::Infer
131132
| LifetimeName::Param(_, ParamName::Fresh)
132133
| LifetimeName::Error => true,
133134
LifetimeName::Static | LifetimeName::Param(..) => false,
@@ -136,7 +137,7 @@ impl LifetimeName {
136137

137138
pub fn is_elided(&self) -> bool {
138139
match self {
139-
LifetimeName::ImplicitObjectLifetimeDefault | LifetimeName::Underscore => true,
140+
LifetimeName::ImplicitObjectLifetimeDefault | LifetimeName::Infer => true,
140141

141142
// It might seem surprising that `Fresh` counts as
142143
// *not* elided -- but this is because, as far as the code

‎compiler/rustc_hir/src/intravisit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ pub fn walk_lifetime<'v, V: Visitor<'v>>(visitor: &mut V, lifetime: &'v Lifetime
497497
| LifetimeName::Static
498498
| LifetimeName::Error
499499
| LifetimeName::ImplicitObjectLifetimeDefault
500-
| LifetimeName::Underscore => {}
500+
| LifetimeName::Infer => {}
501501
}
502502
}
503503

‎compiler/rustc_resolve/src/late.rs

+37-71
Original file line numberDiff line numberDiff line change
@@ -865,7 +865,7 @@ impl<'a: 'ast, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {
865865
let previous_state = replace(&mut this.in_func_body, true);
866866
// Resolve the function body, potentially inside the body of an async closure
867867
this.with_lifetime_rib(
868-
LifetimeRibKind::Elided(LifetimeRes::Anonymous { binder: fn_id }),
868+
LifetimeRibKind::Elided(LifetimeRes::Infer),
869869
|this| this.visit_block(body),
870870
);
871871

@@ -893,9 +893,7 @@ impl<'a: 'ast, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {
893893
this.with_lifetime_rib(
894894
match binder {
895895
ClosureBinder::NotPresent => {
896-
LifetimeRibKind::Elided(LifetimeRes::Anonymous {
897-
binder: fn_id,
898-
})
896+
LifetimeRibKind::Elided(LifetimeRes::Infer)
899897
}
900898
ClosureBinder::For { .. } => LifetimeRibKind::AnonymousReportError,
901899
},
@@ -907,7 +905,7 @@ impl<'a: 'ast, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {
907905
let previous_state = replace(&mut this.in_func_body, true);
908906
// Resolve the function body, potentially inside the body of an async closure
909907
this.with_lifetime_rib(
910-
LifetimeRibKind::Elided(LifetimeRes::Anonymous { binder: fn_id }),
908+
LifetimeRibKind::Elided(LifetimeRes::Infer),
911909
|this| this.visit_expr(body),
912910
);
913911

@@ -1645,35 +1643,12 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
16451643

16461644
if !missing {
16471645
// Do not create a parameter for patterns and expressions.
1648-
for rib in self.lifetime_ribs.iter().rev() {
1649-
match rib.kind {
1650-
LifetimeRibKind::Elided(res @ LifetimeRes::Anonymous { .. }) => {
1651-
for id in node_ids {
1652-
self.record_lifetime_res(id, res, LifetimeElisionCandidate::Named);
1653-
}
1654-
break;
1655-
}
1656-
// `LifetimeRes::Error`, which would usually be used in the case of
1657-
// `ReportError`, is unsuitable here, as we don't emit an error yet. Instead,
1658-
// we simply resolve to an implicit lifetime, which will be checked later, at
1659-
// which point a suitable error will be emitted.
1660-
LifetimeRibKind::AnonymousReportError | LifetimeRibKind::Item => {
1661-
// FIXME(cjgillot) This resolution is wrong, but this does not matter
1662-
// since these cases are erroneous anyway. Lifetime resolution should
1663-
// emit a "missing lifetime specifier" diagnostic.
1664-
let res = LifetimeRes::Anonymous { binder: DUMMY_NODE_ID };
1665-
for id in node_ids {
1666-
self.record_lifetime_res(id, res, LifetimeElisionCandidate::Named);
1667-
}
1668-
break;
1669-
}
1670-
LifetimeRibKind::AnonymousCreateParameter { .. }
1671-
| LifetimeRibKind::Elided(_)
1672-
| LifetimeRibKind::ElisionFailure
1673-
| LifetimeRibKind::Generics { .. }
1674-
| LifetimeRibKind::ConstGeneric
1675-
| LifetimeRibKind::AnonConst => {}
1676-
}
1646+
for id in node_ids {
1647+
self.record_lifetime_res(
1648+
id,
1649+
LifetimeRes::Infer,
1650+
LifetimeElisionCandidate::Named,
1651+
);
16771652
}
16781653
continue;
16791654
}
@@ -1814,15 +1789,12 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
18141789
)
18151790
}
18161791
match res {
1817-
LifetimeRes::Param { .. }
1818-
| LifetimeRes::Fresh { .. }
1819-
| LifetimeRes::Anonymous { .. }
1820-
| LifetimeRes::Static => {
1792+
LifetimeRes::Param { .. } | LifetimeRes::Fresh { .. } | LifetimeRes::Static => {
18211793
if let Some(ref mut candidates) = self.lifetime_elision_candidates {
18221794
candidates.insert(res, candidate);
18231795
}
18241796
}
1825-
LifetimeRes::Error | LifetimeRes::ElidedAnchor { .. } => {}
1797+
LifetimeRes::Infer | LifetimeRes::Error | LifetimeRes::ElidedAnchor { .. } => {}
18261798
}
18271799
}
18281800

@@ -2245,26 +2217,23 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
22452217
this.with_lifetime_rib(LifetimeRibKind::Elided(LifetimeRes::Static), |this| {
22462218
this.visit_ty(ty);
22472219
});
2248-
this.with_lifetime_rib(
2249-
LifetimeRibKind::Elided(LifetimeRes::Anonymous { binder: item.id }),
2250-
|this| {
2251-
if let Some(expr) = expr {
2252-
let constant_item_kind = match item.kind {
2253-
ItemKind::Const(..) => ConstantItemKind::Const,
2254-
ItemKind::Static(..) => ConstantItemKind::Static,
2255-
_ => unreachable!(),
2256-
};
2257-
// We already forbid generic params because of the above item rib,
2258-
// so it doesn't matter whether this is a trivial constant.
2259-
this.with_constant_rib(
2260-
IsRepeatExpr::No,
2261-
HasGenericParams::Yes,
2262-
Some((item.ident, constant_item_kind)),
2263-
|this| this.visit_expr(expr),
2264-
);
2265-
}
2266-
},
2267-
);
2220+
this.with_lifetime_rib(LifetimeRibKind::Elided(LifetimeRes::Infer), |this| {
2221+
if let Some(expr) = expr {
2222+
let constant_item_kind = match item.kind {
2223+
ItemKind::Const(..) => ConstantItemKind::Const,
2224+
ItemKind::Static(..) => ConstantItemKind::Static,
2225+
_ => unreachable!(),
2226+
};
2227+
// We already forbid generic params because of the above item rib,
2228+
// so it doesn't matter whether this is a trivial constant.
2229+
this.with_constant_rib(
2230+
IsRepeatExpr::No,
2231+
HasGenericParams::Yes,
2232+
Some((item.ident, constant_item_kind)),
2233+
|this| this.visit_expr(expr),
2234+
);
2235+
}
2236+
});
22682237
});
22692238
}
22702239

@@ -2521,7 +2490,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
25212490
// Type parameters can already be used and as associated consts are
25222491
// not used as part of the type system, this is far less surprising.
25232492
self.with_lifetime_rib(
2524-
LifetimeRibKind::Elided(LifetimeRes::Anonymous { binder: item.id }),
2493+
LifetimeRibKind::Elided(LifetimeRes::Infer),
25252494
|this| {
25262495
this.with_constant_rib(
25272496
IsRepeatExpr::No,
@@ -2694,17 +2663,14 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
26942663
//
26952664
// Type parameters can already be used and as associated consts are
26962665
// not used as part of the type system, this is far less surprising.
2697-
self.with_lifetime_rib(
2698-
LifetimeRibKind::Elided(LifetimeRes::Anonymous { binder: item.id }),
2699-
|this| {
2700-
this.with_constant_rib(
2701-
IsRepeatExpr::No,
2702-
HasGenericParams::Yes,
2703-
None,
2704-
|this| this.visit_expr(expr),
2705-
)
2706-
},
2707-
);
2666+
self.with_lifetime_rib(LifetimeRibKind::Elided(LifetimeRes::Infer), |this| {
2667+
this.with_constant_rib(
2668+
IsRepeatExpr::No,
2669+
HasGenericParams::Yes,
2670+
None,
2671+
|this| this.visit_expr(expr),
2672+
)
2673+
});
27082674
}
27092675
}
27102676
AssocItemKind::Fn(box Fn { generics, .. }) => {

‎compiler/rustc_resolve/src/late/lifetimes.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
819819
// `Box<dyn Debug + 'static>`.
820820
self.resolve_object_lifetime_default(lifetime)
821821
}
822-
LifetimeName::Underscore => {
822+
LifetimeName::Infer => {
823823
// If the user writes `'_`, we use the *ordinary* elision
824824
// rules. So the `'_` in e.g., `Box<dyn Debug + '_>` will be
825825
// resolved the same as the `'_` in `&'_ Foo`.
@@ -1135,7 +1135,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
11351135
#[tracing::instrument(level = "debug", skip(self))]
11361136
fn visit_lifetime(&mut self, lifetime_ref: &'tcx hir::Lifetime) {
11371137
match lifetime_ref.name {
1138-
hir::LifetimeName::ImplicitObjectLifetimeDefault | hir::LifetimeName::Underscore => {
1138+
hir::LifetimeName::ImplicitObjectLifetimeDefault | hir::LifetimeName::Infer => {
11391139
self.resolve_elided_lifetimes(&[lifetime_ref])
11401140
}
11411141
hir::LifetimeName::Static => self.insert_lifetime(lifetime_ref, Region::Static),

‎src/tools/clippy/clippy_lints/src/manual_async_fn.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ fn captures_all_lifetimes(inputs: &[Ty<'_>], output_lifetimes: &[LifetimeName])
166166
// - There's only one output lifetime bound using `+ '_`
167167
// - All input lifetimes are explicitly bound to the output
168168
input_lifetimes.is_empty()
169-
|| (output_lifetimes.len() == 1 && matches!(output_lifetimes[0], LifetimeName::Underscore))
169+
|| (output_lifetimes.len() == 1 && matches!(output_lifetimes[0], LifetimeName::Infer))
170170
|| input_lifetimes
171171
.iter()
172172
.all(|in_lt| output_lifetimes.iter().any(|out_lt| in_lt == out_lt))

‎src/tools/clippy/clippy_lints/src/ptr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ impl fmt::Display for RefPrefix {
351351
name.fmt(f)?;
352352
f.write_char(' ')?;
353353
},
354-
LifetimeName::Underscore => f.write_str("'_ ")?,
354+
LifetimeName::Infer => f.write_str("'_ ")?,
355355
LifetimeName::Static => f.write_str("'static ")?,
356356
_ => (),
357357
}

0 commit comments

Comments
 (0)
Please sign in to comment.