Skip to content

Commit 9e5372c

Browse files
committed
promote uninhabited_static lint to a hard error
1 parent ce63e5d commit 9e5372c

File tree

5 files changed

+22
-66
lines changed

5 files changed

+22
-66
lines changed

compiler/rustc_hir_analysis/src/check/check.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ use rustc_middle::ty::{
2323
AdtDef, BottomUpFolder, FnSig, GenericArgKind, RegionKind, TypeFoldable, TypeSuperVisitable,
2424
TypeVisitable, TypeVisitableExt, fold_regions,
2525
};
26-
use rustc_session::lint::builtin::UNINHABITED_STATIC;
2726
use rustc_target::spec::{AbiMap, AbiMapping};
2827
use rustc_trait_selection::error_reporting::InferCtxtErrorExt;
2928
use rustc_trait_selection::error_reporting::traits::on_unimplemented::OnUnimplementedDirective;
@@ -197,16 +196,13 @@ fn check_static_inhabited(tcx: TyCtxt<'_>, def_id: LocalDefId) {
197196
}
198197
};
199198
if layout.is_uninhabited() {
200-
tcx.node_span_lint(
201-
UNINHABITED_STATIC,
202-
tcx.local_def_id_to_hir_id(def_id),
203-
span,
204-
|lint| {
205-
lint.primary_message("static of uninhabited type");
206-
lint
207-
.note("uninhabited statics cannot be initialized, and any access would be an immediate error");
208-
},
209-
);
199+
tcx.dcx()
200+
.struct_span_err(span, "static of uninhabited type")
201+
.with_note(
202+
"uninhabited statics cannot be initialized, \
203+
and any access would be an immediate error",
204+
)
205+
.emit();
210206
}
211207
}
212208

compiler/rustc_lint/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,11 @@ fn register_builtins(store: &mut LintStore) {
640640
see <https://github.com/rust-lang/rust/issues/40107> for more information",
641641
);
642642
store.register_removed("wasm_c_abi", "the wasm C ABI has been fixed");
643+
store.register_removed(
644+
"uninhabited_statics",
645+
"converted into a hard error, \
646+
see <https://github.com/rust-lang/rust/issues/74840> for more information",
647+
)
643648
}
644649

645650
fn register_internals(store: &mut LintStore) {

compiler/rustc_lint_defs/src/builtin.rs

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ declare_lint_pass! {
114114
UNCOVERED_PARAM_IN_PROJECTION,
115115
UNEXPECTED_CFGS,
116116
UNFULFILLED_LINT_EXPECTATIONS,
117-
UNINHABITED_STATIC,
118117
UNKNOWN_CRATE_TYPES,
119118
UNKNOWN_DIAGNOSTIC_ATTRIBUTES,
120119
UNKNOWN_LINTS,
@@ -2677,34 +2676,6 @@ declare_lint! {
26772676
"suggest casting to a function pointer when attempting to take references to function items",
26782677
}
26792678

2680-
declare_lint! {
2681-
/// The `uninhabited_static` lint detects uninhabited statics.
2682-
///
2683-
/// ### Example
2684-
///
2685-
/// ```rust
2686-
/// enum Void {}
2687-
/// unsafe extern {
2688-
/// static EXTERN: Void;
2689-
/// }
2690-
/// ```
2691-
///
2692-
/// {{produces}}
2693-
///
2694-
/// ### Explanation
2695-
///
2696-
/// Statics with an uninhabited type can never be initialized, so they are impossible to define.
2697-
/// However, this can be side-stepped with an `extern static`, leading to problems later in the
2698-
/// compiler which assumes that there are no initialized uninhabited places (such as locals or
2699-
/// statics). This was accidentally allowed, but is being phased out.
2700-
pub UNINHABITED_STATIC,
2701-
Warn,
2702-
"uninhabited static",
2703-
@future_incompatible = FutureIncompatibleInfo {
2704-
reason: fcw!(FutureReleaseError #74840),
2705-
};
2706-
}
2707-
27082679
declare_lint! {
27092680
/// The `unnameable_test_items` lint detects [`#[test]`][test] functions
27102681
/// that are not able to be run by the test harness because they are in a
Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
#![feature(never_type)]
2-
#![deny(uninhabited_static)]
32

43
enum Void {}
54
extern "C" {
65
static VOID: Void; //~ ERROR static of uninhabited type
7-
//~| WARN: previously accepted
86
static NEVER: !; //~ ERROR static of uninhabited type
9-
//~| WARN: previously accepted
107
}
118

12-
static VOID2: Void = unsafe { std::mem::transmute(()) }; //~ ERROR static of uninhabited type
13-
//~| WARN: previously accepted
9+
static VOID2: Void = unsafe { std::mem::transmute(()) };
10+
//~^ ERROR static of uninhabited type
1411
//~| ERROR value of uninhabited type `Void`
15-
static NEVER2: Void = unsafe { std::mem::transmute(()) }; //~ ERROR static of uninhabited type
16-
//~| WARN: previously accepted
12+
static NEVER2: Void = unsafe { std::mem::transmute(()) };
13+
//~^ ERROR static of uninhabited type
1714
//~| ERROR value of uninhabited type `Void`
1815

1916
fn main() {}

tests/ui/statics/uninhabited-static.stderr

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,43 @@
11
error: static of uninhabited type
2-
--> $DIR/uninhabited-static.rs:12:1
2+
--> $DIR/uninhabited-static.rs:9:1
33
|
44
LL | static VOID2: Void = unsafe { std::mem::transmute(()) };
55
| ^^^^^^^^^^^^^^^^^^
66
|
7-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
8-
= note: for more information, see issue #74840 <https://github.com/rust-lang/rust/issues/74840>
97
= note: uninhabited statics cannot be initialized, and any access would be an immediate error
10-
note: the lint level is defined here
11-
--> $DIR/uninhabited-static.rs:2:9
12-
|
13-
LL | #![deny(uninhabited_static)]
14-
| ^^^^^^^^^^^^^^^^^^
158

169
error: static of uninhabited type
17-
--> $DIR/uninhabited-static.rs:15:1
10+
--> $DIR/uninhabited-static.rs:12:1
1811
|
1912
LL | static NEVER2: Void = unsafe { std::mem::transmute(()) };
2013
| ^^^^^^^^^^^^^^^^^^^
2114
|
22-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
23-
= note: for more information, see issue #74840 <https://github.com/rust-lang/rust/issues/74840>
2415
= note: uninhabited statics cannot be initialized, and any access would be an immediate error
2516

2617
error: static of uninhabited type
27-
--> $DIR/uninhabited-static.rs:6:5
18+
--> $DIR/uninhabited-static.rs:5:5
2819
|
2920
LL | static VOID: Void;
3021
| ^^^^^^^^^^^^^^^^^
3122
|
32-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
33-
= note: for more information, see issue #74840 <https://github.com/rust-lang/rust/issues/74840>
3423
= note: uninhabited statics cannot be initialized, and any access would be an immediate error
3524

3625
error: static of uninhabited type
37-
--> $DIR/uninhabited-static.rs:8:5
26+
--> $DIR/uninhabited-static.rs:6:5
3827
|
3928
LL | static NEVER: !;
4029
| ^^^^^^^^^^^^^^^
4130
|
42-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
43-
= note: for more information, see issue #74840 <https://github.com/rust-lang/rust/issues/74840>
4431
= note: uninhabited statics cannot be initialized, and any access would be an immediate error
4532

4633
error[E0080]: constructing invalid value: encountered a value of uninhabited type `Void`
47-
--> $DIR/uninhabited-static.rs:12:31
34+
--> $DIR/uninhabited-static.rs:9:31
4835
|
4936
LL | static VOID2: Void = unsafe { std::mem::transmute(()) };
5037
| ^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `VOID2` failed here
5138

5239
error[E0080]: constructing invalid value: encountered a value of uninhabited type `Void`
53-
--> $DIR/uninhabited-static.rs:15:32
40+
--> $DIR/uninhabited-static.rs:12:32
5441
|
5542
LL | static NEVER2: Void = unsafe { std::mem::transmute(()) };
5643
| ^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `NEVER2` failed here

0 commit comments

Comments
 (0)