Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,24 @@ 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};

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,
Expand Down Expand Up @@ -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);

Expand All @@ -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 },
);
}

Expand Down
18 changes: 17 additions & 1 deletion compiler/rustc_lint/src/levels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
7 changes: 7 additions & 0 deletions tests/incremental/auxiliary/scoped_unreachable_macro.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#[macro_export]
macro_rules! with_allow {
($body:block) => {
#[allow(unreachable_code)]
$body
};
}
41 changes: 41 additions & 0 deletions tests/incremental/scoped-unreachable-owner.rs
Original file line number Diff line number Diff line change
@@ -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
}
}
Loading