Skip to content

Commit 8e83a57

Browse files
committed
Propagate all early lifetime resolution errors to RBV
1 parent fa5cccf commit 8e83a57

File tree

14 files changed

+150
-158
lines changed

14 files changed

+150
-158
lines changed

compiler/rustc_ast_lowering/src/lib.rs

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

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

19571959
debug!(?res);
19581960
self.arena.alloc(hir::Lifetime::new(
@@ -2015,7 +2017,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
20152017
// AST resolution emitted an error on those parameters, so we lower them using
20162018
// `ParamName::Error`.
20172019
let ident = self.lower_ident(param.ident);
2018-
let param_name = if let Some(LifetimeRes::Error { .. }) =
2020+
let param_name = if let Some(LifetimeRes::Error(..)) =
20192021
self.resolver.get_lifetime_res(param.id)
20202022
{
20212023
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
@@ -238,7 +238,8 @@ pub enum LifetimeKind {
238238

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

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

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)