Skip to content

Commit 1a86a93

Browse files
committed
Rollup merge of rust-lang#54007 - japaric:gh53964, r=cramertj
crates that provide a `panic_handler` are exempt from the `unused_extern_crates` lint fixes the *first* false positive reported in rust-lang#53964
2 parents 407da0a + 6c4f3f5 commit 1a86a93

File tree

15 files changed

+65
-9
lines changed

15 files changed

+65
-9
lines changed

src/librustc/dep_graph/dep_node.rs

+1
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

+4
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

+6
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

+1
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

+1
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

+1
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

+2
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

+1
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

+5
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

+1
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();
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)
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;
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+
}

src/test/ui/removing-extern-crate.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
#![warn(rust_2018_idioms)]
1717
#![allow(unused_imports)]
1818

19-
extern crate std as foo;
19+
extern crate removing_extern_crate as foo;
2020
extern crate core;
2121

2222
mod another {
23-
extern crate std as foo;
24-
extern crate std;
23+
extern crate removing_extern_crate as foo;
24+
extern crate core;
2525
}
2626

2727
fn main() {}

src/test/ui/removing-extern-crate.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
warning: unused extern crate
22
--> $DIR/removing-extern-crate.rs:19:1
33
|
4-
LL | extern crate std as foo;
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: remove it
4+
LL | extern crate removing_extern_crate as foo;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove it
66
|
77
note: lint level defined here
88
--> $DIR/removing-extern-crate.rs:16:9
@@ -20,12 +20,12 @@ LL | extern crate core;
2020
warning: unused extern crate
2121
--> $DIR/removing-extern-crate.rs:23:5
2222
|
23-
LL | extern crate std as foo;
24-
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: remove it
23+
LL | extern crate removing_extern_crate as foo;
24+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove it
2525

2626
warning: unused extern crate
2727
--> $DIR/removing-extern-crate.rs:24:5
2828
|
29-
LL | extern crate std;
30-
| ^^^^^^^^^^^^^^^^^ help: remove it
29+
LL | extern crate core;
30+
| ^^^^^^^^^^^^^^^^^^ help: remove it
3131

0 commit comments

Comments
 (0)