Skip to content

Commit c43a33e

Browse files
committed
Feed ErrorGuaranteed from late lifetime resolution to RBV
If late lifetime resolution fails for whatever reason, forward to RBV the guarantee that an error was emitted - thereby eliminating the need for a "hack" to suppress subsequent/superfluous error diagnostics.
1 parent 47611e1 commit c43a33e

17 files changed

Lines changed: 184 additions & 161 deletions

File tree

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 30 additions & 26 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
@@ -1931,26 +1931,29 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
19311931
source: LifetimeSource,
19321932
syntax: LifetimeSyntax,
19331933
) -> &'hir hir::Lifetime {
1934-
let res = self.resolver.get_lifetime_res(id).unwrap_or(LifetimeRes::Error);
1935-
let res = match res {
1936-
LifetimeRes::Param { param, .. } => hir::LifetimeKind::Param(param),
1937-
LifetimeRes::Fresh { param, .. } => {
1938-
assert_eq!(ident.name, kw::UnderscoreLifetime);
1939-
let param = self.local_def_id(param);
1940-
hir::LifetimeKind::Param(param)
1941-
}
1942-
LifetimeRes::Infer => {
1943-
assert_eq!(ident.name, kw::UnderscoreLifetime);
1944-
hir::LifetimeKind::Infer
1945-
}
1946-
LifetimeRes::Static { .. } => {
1947-
assert!(matches!(ident.name, kw::StaticLifetime | kw::UnderscoreLifetime));
1948-
hir::LifetimeKind::Static
1949-
}
1950-
LifetimeRes::Error => hir::LifetimeKind::Error,
1951-
LifetimeRes::ElidedAnchor { .. } => {
1952-
panic!("Unexpected `ElidedAnchar` {:?} at {:?}", ident, ident.span);
1934+
let res = if let Some(res) = self.resolver.get_lifetime_res(id) {
1935+
match res {
1936+
LifetimeRes::Param { param, .. } => hir::LifetimeKind::Param(param),
1937+
LifetimeRes::Fresh { param, .. } => {
1938+
assert_eq!(ident.name, kw::UnderscoreLifetime);
1939+
let param = self.local_def_id(param);
1940+
hir::LifetimeKind::Param(param)
1941+
}
1942+
LifetimeRes::Infer => {
1943+
assert_eq!(ident.name, kw::UnderscoreLifetime);
1944+
hir::LifetimeKind::Infer
1945+
}
1946+
LifetimeRes::Static { .. } => {
1947+
assert!(matches!(ident.name, kw::StaticLifetime | kw::UnderscoreLifetime));
1948+
hir::LifetimeKind::Static
1949+
}
1950+
LifetimeRes::Error(guar) => hir::LifetimeKind::Error(guar),
1951+
LifetimeRes::ElidedAnchor { .. } => {
1952+
panic!("Unexpected `ElidedAnchar` {:?} at {:?}", ident, ident.span);
1953+
}
19531954
}
1955+
} else {
1956+
hir::LifetimeKind::Error(self.dcx().span_delayed_bug(ident.span, "unresolved lifetime"))
19541957
};
19551958

19561959
debug!(?res);
@@ -2014,12 +2017,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
20142017
// AST resolution emitted an error on those parameters, so we lower them using
20152018
// `ParamName::Error`.
20162019
let ident = self.lower_ident(param.ident);
2017-
let param_name =
2018-
if let Some(LifetimeRes::Error) = self.resolver.get_lifetime_res(param.id) {
2019-
ParamName::Error(ident)
2020-
} else {
2021-
ParamName::Plain(ident)
2022-
};
2020+
let param_name = if let Some(LifetimeRes::Error(..)) =
2021+
self.resolver.get_lifetime_res(param.id)
2022+
{
2023+
ParamName::Error(ident)
2024+
} else {
2025+
ParamName::Plain(ident)
2026+
};
20232027
let kind =
20242028
hir::GenericParamKind::Lifetime { kind: hir::LifetimeParamKind::Explicit };
20252029

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,
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ pub enum LifetimeKind {
238238

239239
/// Indicates an error during lowering (usually `'_` in wrong place)
240240
/// that was already reported.
241-
Error,
241+
Error(ErrorGuaranteed),
242242

243243
/// User wrote an anonymous lifetime, either `'_` or nothing (which gets
244244
/// converted to `'_`). The semantics of this lifetime should be inferred
@@ -258,7 +258,7 @@ impl LifetimeKind {
258258
// -- but this is because, as far as the code in the compiler is
259259
// concerned -- `Fresh` variants act equivalently to "some fresh name".
260260
// They correspond to early-bound regions on an impl, in other words.
261-
LifetimeKind::Error | LifetimeKind::Param(..) | LifetimeKind::Static => false,
261+
LifetimeKind::Error(..) | LifetimeKind::Param(..) | LifetimeKind::Static => false,
262262
}
263263
}
264264
}

compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs

Lines changed: 6 additions & 4 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(..) => {}
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(..) => {}
808808
}
809809
}
810810
hir::TyKind::Ref(lifetime_ref, ref mt) => {
@@ -891,8 +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 => {}
894+
// Keep track of lifetimes about which errors have already been reported
895+
hir::LifetimeKind::Error(guar) => {
896+
self.insert_lifetime(lifetime_ref, ResolvedArg::Error(guar))
897+
}
896898
// Those will be resolved by typechecking.
897899
hir::LifetimeKind::ImplicitObjectLifetimeDefault | hir::LifetimeKind::Infer => {}
898900
}

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(span.shrink_to_hi())
464-
}
454+
RegionInferReason::ObjectLifetimeDefault(span.shrink_to_hi())
465455
} else {
466456
RegionInferReason::ExplicitObjectLifetime
467457
};

0 commit comments

Comments
 (0)