Skip to content

Commit 9135d8f

Browse files
committed
Lower lifetime resolution failures that are suppressed by Rustdoc
Rather than emit errors in cases that were previously suppressed by Rustdoc, we lower such name resolution failures to a new `RustdocSuppressedError` variant.
1 parent f9725d5 commit 9135d8f

File tree

5 files changed

+32
-10
lines changed

5 files changed

+32
-10
lines changed

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1952,6 +1952,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
19521952
assert!(matches!(ident.name, kw::StaticLifetime | kw::UnderscoreLifetime));
19531953
hir::LifetimeKind::Static
19541954
}
1955+
LifetimeRes::RustdocSuppressedError => hir::LifetimeKind::RustdocSuppressedError,
19551956
LifetimeRes::Error(guar) => hir::LifetimeKind::Error(guar),
19561957
LifetimeRes::ElidedAnchor { .. } => {
19571958
panic!("Unexpected `ElidedAnchar` {:?} at {:?}", ident, ident.span);

compiler/rustc_hir/src/def.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -989,6 +989,8 @@ pub enum LifetimeRes {
989989
Infer,
990990
/// `'static` lifetime.
991991
Static,
992+
/// A resolution failure that is ignored by Rustdoc.
993+
RustdocSuppressedError,
992994
/// Resolution failure.
993995
Error(rustc_span::ErrorGuaranteed),
994996
/// HACK: This is used to recover the NodeId of an elided lifetime.

compiler/rustc_hir/src/hir.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,9 @@ pub enum LifetimeKind {
236236
/// parameter.
237237
ImplicitObjectLifetimeDefault,
238238

239+
/// Indicates an error during lowering that was suppressed by rustdoc.
240+
RustdocSuppressedError,
241+
239242
/// Indicates an error during lowering (usually `'_` in wrong place)
240243
/// that was already reported.
241244
Error(ErrorGuaranteed),
@@ -258,7 +261,10 @@ impl LifetimeKind {
258261
// -- but this is because, as far as the code in the compiler is
259262
// concerned -- `Fresh` variants act equivalently to "some fresh name".
260263
// They correspond to early-bound regions on an impl, in other words.
261-
LifetimeKind::Error(..) | LifetimeKind::Param(..) | LifetimeKind::Static => false,
264+
LifetimeKind::RustdocSuppressedError
265+
| LifetimeKind::Error(..)
266+
| LifetimeKind::Param(..)
267+
| LifetimeKind::Static => false,
262268
}
263269
}
264270
}

compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs

Lines changed: 4 additions & 2 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::RustdocSuppressedError | 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::RustdocSuppressedError | LifetimeKind::Error(..) => {}
808808
}
809809
}
810810
hir::TyKind::Ref(lifetime_ref, ref mt) => {
@@ -891,6 +891,8 @@ 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+
// Just ignore `lifetime_ref` if it couldn't be resolved
895+
hir::LifetimeKind::RustdocSuppressedError => {}
894896
// Keep track of lifetimes about which errors have already been reported
895897
hir::LifetimeKind::Error(guar) => {
896898
self.insert_lifetime(lifetime_ref, ResolvedArg::Error(guar))

compiler/rustc_resolve/src/late.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2342,7 +2342,10 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
23422342
candidates.push((res, candidate));
23432343
}
23442344
}
2345-
LifetimeRes::Infer | LifetimeRes::Error(..) | LifetimeRes::ElidedAnchor { .. } => {}
2345+
LifetimeRes::Infer
2346+
| LifetimeRes::RustdocSuppressedError
2347+
| LifetimeRes::Error(..)
2348+
| LifetimeRes::ElidedAnchor { .. } => {}
23462349
}
23472350
}
23482351

@@ -3068,11 +3071,17 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
30683071
Entry::Occupied(entry) => {
30693072
let span = *entry.get();
30703073
let err = ResolutionError::NameAlreadyUsedInParameterList(ident, span);
3071-
let guar = self.r.report_error(param.ident.span, err);
3074+
let guar = self.report_error(param.ident.span, err);
30723075
let rib = match param.kind {
30733076
GenericParamKind::Lifetime => {
30743077
// Record lifetime res, so lowering knows there is something fishy.
3075-
self.record_lifetime_param(param.id, LifetimeRes::Error(guar));
3078+
self.record_lifetime_param(
3079+
param.id,
3080+
guar.map_or(
3081+
LifetimeRes::RustdocSuppressedError,
3082+
LifetimeRes::Error,
3083+
),
3084+
);
30763085
continue;
30773086
}
30783087
GenericParamKind::Type { .. } => &mut function_type_rib,
@@ -4701,10 +4710,12 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
47014710
/// A wrapper around [`Resolver::report_error`].
47024711
///
47034712
/// This doesn't emit errors for function bodies if this is rustdoc.
4704-
fn report_error(&mut self, span: Span, resolution_error: ResolutionError<'ra>) {
4705-
if self.should_report_errs() {
4706-
self.r.report_error(span, resolution_error);
4707-
}
4713+
fn report_error(
4714+
&mut self,
4715+
span: Span,
4716+
resolution_error: ResolutionError<'ra>,
4717+
) -> Option<ErrorGuaranteed> {
4718+
self.should_report_errs().then(|| self.r.report_error(span, resolution_error))
47084719
}
47094720

47104721
#[inline]

0 commit comments

Comments
 (0)