diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs index 1cde1a7a45a..248ef8ec3fe 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs @@ -43,6 +43,7 @@ use tracing::{debug, instrument}; use crate::callee::{self, DeferredCallResolution, SplatLoweringInfo}; use crate::diagnostics::{self, CtorIsPrivate}; +use crate::diverges::UnreachableCause; use crate::method::{self, MethodCallee}; use crate::{BreakableCtxt, Diverges, Expectation, FnCtxt, LoweredTy}; @@ -50,16 +51,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { /// Produces warning on the given node, if the current point in the /// function is unreachable, and there hasn't been another warning. pub(crate) fn warn_if_unreachable(&self, id: HirId, span: Span, kind: &str) { - struct UnreachableItem<'a, 'b> { + struct UnreachableItem<'a> { kind: &'a str, span: Span, - orig_span: Span, - custom_note: Option<&'b str>, + cause: UnreachableCause, } - impl<'a, 'b, 'c> Diagnostic<'a> for UnreachableItem<'b, 'c> { + impl<'a> Diagnostic<'a> for UnreachableItem<'_> { fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a> { - let Self { kind, span, orig_span, custom_note } = self; + let Self { kind, span, cause } = self; + let (orig_span, custom_note) = cause.diagnostic(); let msg = format!("unreachable {kind}"); Diag::new(dcx, level, msg.clone()).with_span_label(span, msg).with_span_label( orig_span, @@ -89,12 +90,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // Don't emit the lint if we are in an impl marked as `#[automatically_derive]`. // This is relevant for deriving `Clone` and `PartialEq` on types containing `!`. - if self.tcx.is_automatically_derived(self.tcx.parent(id.owner.def_id.into())) { + // For an owner parent, only its attributes matter here. + let parent = self.tcx.parent(id.owner.def_id.into()); + let hir_parent = self.tcx.hir_owner_parent(id.owner); + let derived = if hir_parent.local_id == hir::ItemLocalId::ZERO + && hir_parent.owner.def_id.to_def_id() == parent + { + hir::find_attr!(self.tcx, hir_parent, AutomaticallyDerived) + } else { + self.tcx.is_automatically_derived(parent) + }; + if derived { return; } - // Only now do the diagnostic's choice of span and note become observable. - let (orig_span, custom_note) = cause.diagnostic(); // Don't warn twice. self.diverges.set(Diverges::WarnedAlways); @@ -104,7 +113,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { UNREACHABLE_CODE, id, span, - UnreachableItem { kind, span, orig_span, custom_note }, + UnreachableItem { kind, span, cause }, ); } diff --git a/compiler/rustc_lint/src/levels.rs b/compiler/rustc_lint/src/levels.rs index 95c3f8ae09f..ac603ca97e4 100644 --- a/compiler/rustc_lint/src/levels.rs +++ b/compiler/rustc_lint/src/levels.rs @@ -278,7 +278,23 @@ impl LintLevelsProvider for LintLevelQueryMap<'_> { self.specs.specs.get(&self.cur.local_id).unwrap_or(&self.empty) } - fn insert(&mut self, id: LintId, level_spec: StableLevelSpec) { + fn insert(&mut self, id: LintId, mut level_spec: StableLevelSpec) { + // Moving an owner also moves its local lint settings. Keep their exact + // positions observable when a diagnostic uses the source of an Allow. + let owner = self.cur.owner; + if self.tcx.dep_graph.is_fully_enabled() + && owner != hir::CRATE_OWNER_ID + && level_spec.level() == Level::Allow + && let LintLevelSource::Node { span, .. } = &mut level_spec.src + { + let data = span.data_untracked(); + if data.parent.is_none() + && !data.is_dummy() + && self.tcx.source_span(owner.def_id).data_untracked().contains(data) + { + *span = span.with_parent(Some(owner.def_id)); + } + } self.specs.specs.get_mut_or_insert_default(self.cur.local_id).insert(id, level_spec); } diff --git a/tests/incremental/auxiliary/scoped_unreachable_macro.rs b/tests/incremental/auxiliary/scoped_unreachable_macro.rs new file mode 100644 index 00000000000..1ef1338b388 --- /dev/null +++ b/tests/incremental/auxiliary/scoped_unreachable_macro.rs @@ -0,0 +1,7 @@ +#[macro_export] +macro_rules! with_allow { + ($body:block) => { + #[allow(unreachable_code)] + $body + }; +} diff --git a/tests/incremental/scoped-unreachable-owner.rs b/tests/incremental/scoped-unreachable-owner.rs new file mode 100644 index 00000000000..f9a2c2b1539 --- /dev/null +++ b/tests/incremental/scoped-unreachable-owner.rs @@ -0,0 +1,41 @@ +//@ revisions: bpass1 bpass2 bfail3 +//@ compile-flags: -Zquery-dep-graph +//@ aux-build: scoped_unreachable_macro.rs + +#![feature(rustc_attrs)] +#![allow(internal_features)] +#![deny(unreachable_code)] +#![crate_type = "rlib"] + +extern crate scoped_unreachable_macro; + +pub struct Local; + +impl Local { + #[rustc_clean(cfg = "bpass2", loaded_from_disk = "typeck_root")] + pub fn locally_allowed() { + #[allow(unreachable_code)] + { + return; + return; + } + } + + pub fn allowed_by_foreign_macro() { + scoped_unreachable_macro::with_allow!({ + return; + return; + }); + } + + // Adding a sibling changes the impl without changing either method body. + #[cfg(bpass2)] + pub fn added() {} + + #[cfg(bfail3)] + pub fn reported() { + return; + return; + //[bfail3]~^ ERROR unreachable statement + } +}