Skip to content

Commit 7db9c3c

Browse files
committed
Tests passing
1 parent 8ad46b4 commit 7db9c3c

File tree

3 files changed

+58
-16
lines changed

3 files changed

+58
-16
lines changed

compiler/rustc_passes/src/dead.rs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,12 @@ impl<'tcx> DeadVisitor<'tcx> {
723723
ShouldWarnAboutField::Yes(is_positional)
724724
}
725725

726+
// # Panics
727+
// All `dead_codes` must have the same lint level, otherwise we will intentionally ICE.
728+
// This is because we emit a multi-spanned lint using the lint level of the `dead_codes`'s
729+
// first local def id.
730+
// Prefer calling `Self.warn_dead_code` or `Self.warn_dead_code_grouped_by_lint_level`
731+
// since those methods group by lint level before calling this method.
726732
fn warn_multiple_dead_codes(
727733
&self,
728734
dead_codes: &[LocalDefId],
@@ -734,6 +740,15 @@ impl<'tcx> DeadVisitor<'tcx> {
734740
return;
735741
};
736742
let tcx = self.tcx;
743+
744+
let first_hir_id = tcx.hir().local_def_id_to_hir_id(first_id);
745+
let first_lint_level = tcx.lint_level_at_node(lint::builtin::DEAD_CODE, first_hir_id).0;
746+
assert!(dead_codes.iter().skip(1).all(|id| {
747+
let hir_id = tcx.hir().local_def_id_to_hir_id(*id);
748+
let level = tcx.lint_level_at_node(lint::builtin::DEAD_CODE, hir_id).0;
749+
level == first_lint_level
750+
}));
751+
737752
let names: Vec<_> =
738753
dead_codes.iter().map(|&def_id| tcx.item_name(def_id.to_def_id())).collect();
739754
let spans: Vec<_> = dead_codes
@@ -814,18 +829,9 @@ impl<'tcx> DeadVisitor<'tcx> {
814829
}
815830
};
816831

817-
// FIXME: Remove this before landing the PR.
818-
// Just keeping it around so that I remember how to get the expectation id.
819-
// for id in &dead_codes[1..] {
820-
// let hir = self.tcx.hir().local_def_id_to_hir_id(*id);
821-
// let lint_level = self.tcx.lint_level_at_node(lint::builtin::DEAD_CODE, hir).0;
822-
// if let Some(expectation_id) = lint_level.get_expectation_id() {
823-
// self.tcx.sess.diagnostic().insert_fulfilled_expectation(expectation_id);
824-
// }
825-
// }
826832
self.tcx.emit_spanned_lint(
827833
lint,
828-
tcx.hir().local_def_id_to_hir_id(first_id),
834+
first_hir_id,
829835
MultiSpan::from_spans(spans),
830836
diag,
831837
);
@@ -903,14 +909,14 @@ fn check_mod_deathness(tcx: TyCtxt<'_>, module: LocalDefId) {
903909
if let hir::ItemKind::Impl(impl_item) = tcx.hir().item(item).kind {
904910
let mut dead_items = Vec::new();
905911
for item in impl_item.items {
906-
let did = item.id.owner_id.def_id;
907-
if !visitor.is_live_code(did) {
912+
let def_id = item.id.owner_id.def_id;
913+
if !visitor.is_live_code(def_id) {
908914
let name = tcx.item_name(def_id.to_def_id());
909-
let hir = tcx.hir().local_def_id_to_hir_id(did);
915+
let hir = tcx.hir().local_def_id_to_hir_id(def_id);
910916
let level = tcx.lint_level_at_node(lint::builtin::DEAD_CODE, hir).0;
911917

912918
dead_items.push(DeadVariant {
913-
def_id: did,
919+
def_id,
914920
name,
915921
level,
916922
})
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// revisions: rpass1
2+
//
3+
// The corresponding ui test can be found in
4+
// `tests/ui/lint/rfc-2383-lint-reason/expect_unused_inside_impl_block.rs`
5+
6+
#![feature(lint_reasons)]
7+
#![warn(unused)]
8+
9+
struct OneUnused;
10+
struct TwoUnused;
11+
12+
impl OneUnused {
13+
#[expect(unused)]
14+
fn unused() {}
15+
}
16+
17+
impl TwoUnused {
18+
#[expect(unused)]
19+
fn unused1(){}
20+
21+
// This unused method has `#[expect(unused)]`, so the compiler should not emit a warning.
22+
// This ui test was added after a regression in the compiler where it did not recognize multiple
23+
// `#[expect(unused)]` annotations inside of impl blocks.
24+
// issue 114416
25+
#[expect(unused)]
26+
fn unused2(){}
27+
}
28+
29+
fn main() {
30+
let _ = OneUnused;
31+
let _ = TwoUnused;
32+
}

tests/ui/lint/rfc-2383-lint-reason/expect_unused_inside_impl_block.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
// check-pass
2+
//
3+
// The corresponding incremental compilation test can be found in
4+
// `tests/incremental/issue-114416-expect-unused-inside-impl-block.rs`
25

36
#![feature(lint_reasons)]
47
#![warn(unused)]
@@ -15,8 +18,9 @@ impl TwoUnused {
1518
#[expect(unused)]
1619
fn unused1(){}
1720

18-
// Tests a regression where the compiler erroneously determined that all `#[expect(unused)]`
19-
// after the first method in the impl block were unfulfilled.
21+
// This unused method has `#[expect(unused)]`, so the compiler should not emit a warning.
22+
// This ui test was added after a regression in the compiler where it did not recognize multiple
23+
// `#[expect(unused)]` annotations inside of impl blocks.
2024
// issue 114416
2125
#[expect(unused)]
2226
fn unused2(){}

0 commit comments

Comments
 (0)