Skip to content

Commit 9b764c3

Browse files
committed
crates that provide a panic_handler are exempt from unused_extern_crates
fixes the *first* false positive reported in #53964
1 parent 35a5541 commit 9b764c3

File tree

13 files changed

+56
-0
lines changed

13 files changed

+56
-0
lines changed

src/librustc/dep_graph/dep_node.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,7 @@ define_dep_nodes!( <'tcx>
574574
[] IsPanicRuntime(CrateNum),
575575
[] IsCompilerBuiltins(CrateNum),
576576
[] HasGlobalAllocator(CrateNum),
577+
[] HasPanicHandler(CrateNum),
577578
[input] ExternCrate(DefId),
578579
[eval_always] LintLevels,
579580
[] Specializes { impl1: DefId, impl2: DefId },

src/librustc/session/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,9 @@ pub struct Session {
159159
/// Metadata about the allocators for the current crate being compiled
160160
pub has_global_allocator: Once<bool>,
161161

162+
/// Metadata about the panic handlers for the current crate being compiled
163+
pub has_panic_handler: Once<bool>,
164+
162165
/// Cap lint level specified by a driver specifically.
163166
pub driver_lint_caps: FxHashMap<lint::LintId, lint::Level>,
164167
}
@@ -1160,6 +1163,7 @@ pub fn build_session_(
11601163
(*GLOBAL_JOBSERVER).clone()
11611164
},
11621165
has_global_allocator: Once::new(),
1166+
has_panic_handler: Once::new(),
11631167
driver_lint_caps: FxHashMap(),
11641168
};
11651169

src/librustc/ty/query/config.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,12 @@ impl<'tcx> QueryDescription<'tcx> for queries::has_global_allocator<'tcx> {
464464
}
465465
}
466466

467+
impl<'tcx> QueryDescription<'tcx> for queries::has_panic_handler<'tcx> {
468+
fn describe(_: TyCtxt, _: CrateNum) -> String {
469+
"checking if the crate has_panic_handler".to_string()
470+
}
471+
}
472+
467473
impl<'tcx> QueryDescription<'tcx> for queries::extern_crate<'tcx> {
468474
fn describe(_: TyCtxt, _: DefId) -> String {
469475
"getting crate's ExternCrateData".to_string()

src/librustc/ty/query/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,7 @@ define_queries! { <'tcx>
381381
[fatal_cycle] fn is_panic_runtime: IsPanicRuntime(CrateNum) -> bool,
382382
[fatal_cycle] fn is_compiler_builtins: IsCompilerBuiltins(CrateNum) -> bool,
383383
[fatal_cycle] fn has_global_allocator: HasGlobalAllocator(CrateNum) -> bool,
384+
[fatal_cycle] fn has_panic_handler: HasPanicHandler(CrateNum) -> bool,
384385
[fatal_cycle] fn is_sanitizer_runtime: IsSanitizerRuntime(CrateNum) -> bool,
385386
[fatal_cycle] fn is_profiler_runtime: IsProfilerRuntime(CrateNum) -> bool,
386387
[fatal_cycle] fn panic_strategy: GetPanicStrategy(CrateNum) -> PanicStrategy,

src/librustc/ty/query/plumbing.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,6 +1168,7 @@ pub fn force_from_dep_node<'a, 'gcx, 'lcx>(tcx: TyCtxt<'a, 'gcx, 'lcx>,
11681168
DepKind::IsPanicRuntime => { force!(is_panic_runtime, krate!()); }
11691169
DepKind::IsCompilerBuiltins => { force!(is_compiler_builtins, krate!()); }
11701170
DepKind::HasGlobalAllocator => { force!(has_global_allocator, krate!()); }
1171+
DepKind::HasPanicHandler => { force!(has_panic_handler, krate!()); }
11711172
DepKind::ExternCrate => { force!(extern_crate, def_id!()); }
11721173
DepKind::LintLevels => { force!(lint_levels, LOCAL_CRATE); }
11731174
DepKind::InScopeTraits => { force!(in_scope_traits_map, def_id!().index); }

src/librustc_metadata/cstore_impl.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ provide! { <'tcx> tcx, def_id, other, cdata,
173173
is_panic_runtime => { cdata.root.panic_runtime }
174174
is_compiler_builtins => { cdata.root.compiler_builtins }
175175
has_global_allocator => { cdata.root.has_global_allocator }
176+
has_panic_handler => { cdata.root.has_panic_handler }
176177
is_sanitizer_runtime => { cdata.root.sanitizer_runtime }
177178
is_profiler_runtime => { cdata.root.profiler_runtime }
178179
panic_strategy => { cdata.root.panic_strategy }

src/librustc_metadata/encoder.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
484484
let is_proc_macro = tcx.sess.crate_types.borrow().contains(&CrateType::ProcMacro);
485485
let has_default_lib_allocator = attr::contains_name(&attrs, "default_lib_allocator");
486486
let has_global_allocator = *tcx.sess.has_global_allocator.get();
487+
let has_panic_handler = *tcx.sess.has_panic_handler.try_get().unwrap_or(&false);
487488

488489
let root = self.lazy(&CrateRoot {
489490
name: tcx.crate_name(LOCAL_CRATE),
@@ -494,6 +495,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
494495
panic_strategy: tcx.sess.panic_strategy(),
495496
edition: hygiene::default_edition(),
496497
has_global_allocator: has_global_allocator,
498+
has_panic_handler: has_panic_handler,
497499
has_default_lib_allocator: has_default_lib_allocator,
498500
plugin_registrar_fn: tcx.sess
499501
.plugin_registrar_fn

src/librustc_metadata/schema.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ pub struct CrateRoot {
193193
pub panic_strategy: PanicStrategy,
194194
pub edition: Edition,
195195
pub has_global_allocator: bool,
196+
pub has_panic_handler: bool,
196197
pub has_default_lib_allocator: bool,
197198
pub plugin_registrar_fn: Option<DefIndex>,
198199
pub macro_derive_registrar: Option<DefIndex>,

src/librustc_typeck/check/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,6 +1138,11 @@ fn check_fn<'a, 'gcx, 'tcx>(inherited: &'a Inherited<'a, 'gcx, 'tcx>,
11381138
if let Some(panic_impl_did) = fcx.tcx.lang_items().panic_impl() {
11391139
if panic_impl_did == fcx.tcx.hir.local_def_id(fn_id) {
11401140
if let Some(panic_info_did) = fcx.tcx.lang_items().panic_info() {
1141+
// at this point we don't care if there are duplicate handlers or if the handler has
1142+
// the wrong signature as this value we'll be used when writing metadata and that
1143+
// only happens if compilation succeeded
1144+
fcx.tcx.sess.has_panic_handler.try_set_same(true);
1145+
11411146
if declared_ret_ty.sty != ty::Never {
11421147
fcx.tcx.sess.span_err(
11431148
decl.output.span(),

src/librustc_typeck/check_unused.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ fn unused_crates_lint<'tcx>(tcx: TyCtxt<'_, 'tcx, 'tcx>) {
117117
!tcx.is_compiler_builtins(cnum)
118118
&& !tcx.is_panic_runtime(cnum)
119119
&& !tcx.has_global_allocator(cnum)
120+
&& !tcx.has_panic_handler(cnum)
120121
})
121122
.cloned()
122123
.collect();
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-include ../tools.mk
2+
3+
all:
4+
$(RUSTC) panic.rs
5+
$(RUSTC) -C panic=abort --emit=obj app.rs -L $(TMPDIR)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#![crate_type = "bin"]
2+
#![no_main]
3+
#![no_std]
4+
5+
#![deny(unused_extern_crates)]
6+
7+
// `panic` provides a `panic_handler` so it shouldn't trip the `unused_extern_crates` lint
8+
extern crate panic;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![crate_type = "lib"]
12+
#![feature(panic_handler)]
13+
#![no_std]
14+
15+
use core::panic::PanicInfo;
16+
17+
#[panic_handler]
18+
fn panic(_: &PanicInfo) -> ! {
19+
loop {}
20+
}

0 commit comments

Comments
 (0)