Skip to content

Commit 9b8307b

Browse files
Auto merge of #149518 - WaffleLapkin:say-no-to-uninhabited-statics, r=<try>
Promote `uninhabited_static` lint to a hard error
2 parents 568b117 + 0a6b3e8 commit 9b8307b

File tree

5 files changed

+22
-67
lines changed

5 files changed

+22
-67
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
@@ -638,6 +638,11 @@ fn register_builtins(store: &mut LintStore) {
638638
see <https://github.com/rust-lang/rust/issues/40107> for more information",
639639
);
640640
store.register_removed("wasm_c_abi", "the wasm C ABI has been fixed");
641+
store.register_removed(
642+
"uninhabited_statics",
643+
"converted into a hard error, \
644+
see <https://github.com/rust-lang/rust/issues/74840> for more information",
645+
)
641646
}
642647

643648
fn register_internals(store: &mut LintStore) {

compiler/rustc_lint_defs/src/builtin.rs

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ declare_lint_pass! {
115115
UNCOVERED_PARAM_IN_PROJECTION,
116116
UNEXPECTED_CFGS,
117117
UNFULFILLED_LINT_EXPECTATIONS,
118-
UNINHABITED_STATIC,
119118
UNKNOWN_CRATE_TYPES,
120119
UNKNOWN_DIAGNOSTIC_ATTRIBUTES,
121120
UNKNOWN_LINTS,
@@ -2675,35 +2674,6 @@ declare_lint! {
26752674
"suggest casting to a function pointer when attempting to take references to function items",
26762675
}
26772676

2678-
declare_lint! {
2679-
/// The `uninhabited_static` lint detects uninhabited statics.
2680-
///
2681-
/// ### Example
2682-
///
2683-
/// ```rust
2684-
/// enum Void {}
2685-
/// unsafe extern {
2686-
/// static EXTERN: Void;
2687-
/// }
2688-
/// ```
2689-
///
2690-
/// {{produces}}
2691-
///
2692-
/// ### Explanation
2693-
///
2694-
/// Statics with an uninhabited type can never be initialized, so they are impossible to define.
2695-
/// However, this can be side-stepped with an `extern static`, leading to problems later in the
2696-
/// compiler which assumes that there are no initialized uninhabited places (such as locals or
2697-
/// statics). This was accidentally allowed, but is being phased out.
2698-
pub UNINHABITED_STATIC,
2699-
Warn,
2700-
"uninhabited static",
2701-
@future_incompatible = FutureIncompatibleInfo {
2702-
reason: FutureIncompatibilityReason::FutureReleaseError,
2703-
reference: "issue #74840 <https://github.com/rust-lang/rust/issues/74840>",
2704-
};
2705-
}
2706-
27072677
declare_lint! {
27082678
/// The `unnameable_test_items` lint detects [`#[test]`][test] functions
27092679
/// 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)