Skip to content

Commit a531436

Browse files
committed
Propagate all early lifetime resolution errors to RBV
1 parent 08ed1a1 commit a531436

14 files changed

Lines changed: 150 additions & 158 deletions

File tree

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -880,7 +880,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
880880

881881
(hir::ParamName::Fresh, hir::LifetimeParamKind::Elided(kind))
882882
}
883-
LifetimeRes::Static { .. } | LifetimeRes::Error { .. } => return None,
883+
LifetimeRes::Static { .. } | LifetimeRes::Error(..) => return None,
884884
res => panic!(
885885
"Unexpected lifetime resolution {:?} for {:?} at {:?}",
886886
res, ident, ident.span
@@ -1934,27 +1934,29 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
19341934
syntax: LifetimeSyntax,
19351935
) -> &'hir hir::Lifetime {
19361936
let res =
1937-
self.resolver.get_lifetime_res(id).unwrap_or(LifetimeRes::Error { undeclared: None });
1938-
let res = match res {
1939-
LifetimeRes::Param { param, .. } => hir::LifetimeKind::Param(param),
1940-
LifetimeRes::Fresh { param, .. } => {
1941-
assert_eq!(ident.name, kw::UnderscoreLifetime);
1942-
let param = self.local_def_id(param);
1943-
hir::LifetimeKind::Param(param)
1944-
}
1945-
LifetimeRes::Infer => {
1946-
assert_eq!(ident.name, kw::UnderscoreLifetime);
1947-
hir::LifetimeKind::Infer
1948-
}
1949-
LifetimeRes::Static { .. } => {
1950-
assert!(matches!(ident.name, kw::StaticLifetime | kw::UnderscoreLifetime));
1951-
hir::LifetimeKind::Static
1952-
}
1953-
LifetimeRes::Error { undeclared } => hir::LifetimeKind::Error { undeclared },
1954-
LifetimeRes::ElidedAnchor { .. } => {
1955-
panic!("Unexpected `ElidedAnchar` {:?} at {:?}", ident, ident.span);
1956-
}
1957-
};
1937+
self.resolver.get_lifetime_res(id).map_or(
1938+
hir::LifetimeKind::NotFound,
1939+
|res| match res {
1940+
LifetimeRes::Param { param, .. } => hir::LifetimeKind::Param(param),
1941+
LifetimeRes::Fresh { param, .. } => {
1942+
assert_eq!(ident.name, kw::UnderscoreLifetime);
1943+
let param = self.local_def_id(param);
1944+
hir::LifetimeKind::Param(param)
1945+
}
1946+
LifetimeRes::Infer => {
1947+
assert_eq!(ident.name, kw::UnderscoreLifetime);
1948+
hir::LifetimeKind::Infer
1949+
}
1950+
LifetimeRes::Static { .. } => {
1951+
assert!(matches!(ident.name, kw::StaticLifetime | kw::UnderscoreLifetime));
1952+
hir::LifetimeKind::Static
1953+
}
1954+
LifetimeRes::Error(guar) => hir::LifetimeKind::Error(guar),
1955+
LifetimeRes::ElidedAnchor { .. } => {
1956+
panic!("Unexpected `ElidedAnchar` {:?} at {:?}", ident, ident.span);
1957+
}
1958+
},
1959+
);
19581960

19591961
debug!(?res);
19601962
self.arena.alloc(hir::Lifetime::new(
@@ -2017,7 +2019,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
20172019
// AST resolution emitted an error on those parameters, so we lower them using
20182020
// `ParamName::Error`.
20192021
let ident = self.lower_ident(param.ident);
2020-
let param_name = if let Some(LifetimeRes::Error { .. }) =
2022+
let param_name = if let Some(LifetimeRes::Error(..)) =
20212023
self.resolver.get_lifetime_res(param.id)
20222024
{
20232025
ParamName::Error(ident)

compiler/rustc_hir/src/def.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -990,7 +990,7 @@ pub enum LifetimeRes {
990990
/// `'static` lifetime.
991991
Static,
992992
/// Resolution failure.
993-
Error { undeclared: Option<rustc_span::ErrorGuaranteed> },
993+
Error(rustc_span::ErrorGuaranteed),
994994
/// HACK: This is used to recover the NodeId of an elided lifetime.
995995
ElidedAnchor { start: NodeId, end: NodeId },
996996
}

compiler/rustc_hir/src/hir.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,8 @@ pub enum LifetimeKind {
237237

238238
/// Indicates an error during lowering (usually `'_` in wrong place)
239239
/// that was already reported.
240-
Error { undeclared: Option<ErrorGuaranteed> },
240+
Error(ErrorGuaranteed),
241+
NotFound,
241242

242243
/// User wrote an anonymous lifetime, either `'_` or nothing (which gets
243244
/// converted to `'_`). The semantics of this lifetime should be inferred
@@ -257,7 +258,10 @@ impl LifetimeKind {
257258
// -- but this is because, as far as the code in the compiler is
258259
// concerned -- `Fresh` variants act equivalently to "some fresh name".
259260
// They correspond to early-bound regions on an impl, in other words.
260-
LifetimeKind::Error { .. } | LifetimeKind::Param(..) | LifetimeKind::Static => false,
261+
LifetimeKind::Error(..)
262+
| LifetimeKind::NotFound
263+
| LifetimeKind::Param(..)
264+
| LifetimeKind::Static => false,
261265
}
262266
}
263267
}

compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
663663
LifetimeKind::Param(def_id) => {
664664
self.resolve_lifetime_ref(def_id, lt);
665665
}
666-
LifetimeKind::Error { .. } => {}
666+
LifetimeKind::Error(..) | LifetimeKind::NotFound => {}
667667
LifetimeKind::ImplicitObjectLifetimeDefault
668668
| LifetimeKind::Infer
669669
| LifetimeKind::Static => {
@@ -804,7 +804,7 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
804804
// If the user wrote an explicit name, use that.
805805
self.visit_lifetime(&*lifetime);
806806
}
807-
LifetimeKind::Error { .. } => {}
807+
LifetimeKind::Error(..) | LifetimeKind::NotFound => {}
808808
}
809809
}
810810
hir::TyKind::Ref(lifetime_ref, ref mt) => {
@@ -891,9 +891,10 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
891891
hir::LifetimeKind::Param(param_def_id) => {
892892
self.resolve_lifetime_ref(param_def_id, lifetime_ref)
893893
}
894-
// If we've already reported an error, just ignore `lifetime_ref`.
895-
hir::LifetimeKind::Error { undeclared: None } => {}
896-
hir::LifetimeKind::Error { undeclared: Some(guar) } => {
894+
// Just ignore `lifetime_ref` if it couldn't be resolved
895+
hir::LifetimeKind::NotFound => {}
896+
// Keep track of lifetimes about which errors have already been reported
897+
hir::LifetimeKind::Error(guar) => {
897898
self.insert_lifetime(lifetime_ref, ResolvedArg::Error(guar))
898899
}
899900
// Those will be resolved by typechecking.

compiler/rustc_hir_analysis/src/hir_ty_lowering/dyn_trait.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -451,17 +451,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
451451
} else {
452452
let reason =
453453
if let hir::LifetimeKind::ImplicitObjectLifetimeDefault = lifetime.kind {
454-
if let hir::Node::Ty(hir::Ty {
455-
kind: hir::TyKind::Ref(parent_lifetime, _),
456-
..
457-
}) = tcx.parent_hir_node(hir_id)
458-
&& tcx.named_bound_var(parent_lifetime.hir_id).is_none()
459-
{
460-
// Parent lifetime must have failed to resolve. Don't emit a redundant error.
461-
RegionInferReason::ExplicitObjectLifetime
462-
} else {
463-
RegionInferReason::ObjectLifetimeDefault
464-
}
454+
RegionInferReason::ObjectLifetimeDefault
465455
} else {
466456
RegionInferReason::ExplicitObjectLifetime
467457
};

0 commit comments

Comments
 (0)