Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions compiler/rustc_hir_analysis/src/check/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ use rustc_middle::ty::{
AdtDef, BottomUpFolder, FnSig, GenericArgKind, RegionKind, TypeFoldable, TypeSuperVisitable,
TypeVisitable, TypeVisitableExt, fold_regions,
};
use rustc_session::lint::builtin::UNINHABITED_STATIC;
use rustc_target::spec::{AbiMap, AbiMapping};
use rustc_trait_selection::error_reporting::InferCtxtErrorExt;
use rustc_trait_selection::error_reporting::traits::on_unimplemented::OnUnimplementedDirective;
Expand Down Expand Up @@ -197,16 +196,13 @@ fn check_static_inhabited(tcx: TyCtxt<'_>, def_id: LocalDefId) {
}
};
if layout.is_uninhabited() {
Copy link
Member

@fmease fmease Dec 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This obviously doesn't account for visibility meaning if upstream makes a public type with private fields uninhabited it could break downstream.

Is that already considered to be a breaking change due to other features? I see a bunch of uses of is_uninhabited in the compiler, so maybe?

Counterexamples: Exhaustiveness checking accounts for visibility at module granularity even, #[repr(transparent)] no longer permits external types with private fields (well, it's still a future-incompat deny-by-default lint, repr_transparent_non_zst_fields).

Edit: This concern probably doesn't really matter actually, it's super theoretical. It's more important that we reject the bad statics.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about it a bit and I think for this lint specifically this is fine.

a static implies the existence of the value, so there is either

  • Some library that provides a value of such type (extern static)
  • Some (accessible) function that returns a value of such type (normal static)

In both cases making the value uncostructable would already be a breaking change.


Another way to look at this: having a static of an uninhabited type is unsound and leads to UB on basically any use of such static. We shouldn't compile such programs, period.

Copy link
Member

@fmease fmease Dec 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't compile such programs, period.

I do agree.

having a static of an uninhabited type is unsound and leads to UB on basically any use of such static

Re. supposed unsoundness tho, is that really the case? Merely defining such a static isn't insta-UB or is it? And since extern statics are unsafe to reference, no language invariant is broken in safe code, so it's not unsound; sure it is UB but that would be the user's fault.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough, it slipped from my mind that those accesses are unsafe.

tcx.node_span_lint(
UNINHABITED_STATIC,
tcx.local_def_id_to_hir_id(def_id),
span,
|lint| {
lint.primary_message("static of uninhabited type");
lint
.note("uninhabited statics cannot be initialized, and any access would be an immediate error");
},
);
tcx.dcx()
.struct_span_err(span, "static of uninhabited type")
.with_note(
"uninhabited statics cannot be initialized, \
and any access would be an immediate error",
)
.emit();
}
}

Expand Down
5 changes: 5 additions & 0 deletions compiler/rustc_lint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,11 @@ fn register_builtins(store: &mut LintStore) {
see <https://github.com/rust-lang/rust/issues/40107> for more information",
);
store.register_removed("wasm_c_abi", "the wasm C ABI has been fixed");
store.register_removed(
"uninhabited_statics",
"converted into a hard error, \
see <https://github.com/rust-lang/rust/issues/74840> for more information",
)
}

fn register_internals(store: &mut LintStore) {
Expand Down
30 changes: 0 additions & 30 deletions compiler/rustc_lint_defs/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ declare_lint_pass! {
UNCOVERED_PARAM_IN_PROJECTION,
UNEXPECTED_CFGS,
UNFULFILLED_LINT_EXPECTATIONS,
UNINHABITED_STATIC,
UNKNOWN_CRATE_TYPES,
UNKNOWN_DIAGNOSTIC_ATTRIBUTES,
UNKNOWN_LINTS,
Expand Down Expand Up @@ -2675,35 +2674,6 @@ declare_lint! {
"suggest casting to a function pointer when attempting to take references to function items",
}

declare_lint! {
/// The `uninhabited_static` lint detects uninhabited statics.
///
/// ### Example
///
/// ```rust
/// enum Void {}
/// unsafe extern {
/// static EXTERN: Void;
/// }
/// ```
///
/// {{produces}}
///
/// ### Explanation
///
/// Statics with an uninhabited type can never be initialized, so they are impossible to define.
/// However, this can be side-stepped with an `extern static`, leading to problems later in the
/// compiler which assumes that there are no initialized uninhabited places (such as locals or
/// statics). This was accidentally allowed, but is being phased out.
pub UNINHABITED_STATIC,
Warn,
"uninhabited static",
@future_incompatible = FutureIncompatibleInfo {
reason: FutureIncompatibilityReason::FutureReleaseError,
reference: "issue #74840 <https://github.com/rust-lang/rust/issues/74840>",
};
}

declare_lint! {
/// The `unnameable_test_items` lint detects [`#[test]`][test] functions
/// that are not able to be run by the test harness because they are in a
Expand Down
11 changes: 4 additions & 7 deletions tests/ui/statics/uninhabited-static.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
#![feature(never_type)]
#![deny(uninhabited_static)]

enum Void {}
extern "C" {
static VOID: Void; //~ ERROR static of uninhabited type
//~| WARN: previously accepted
static NEVER: !; //~ ERROR static of uninhabited type
//~| WARN: previously accepted
}

static VOID2: Void = unsafe { std::mem::transmute(()) }; //~ ERROR static of uninhabited type
//~| WARN: previously accepted
static VOID2: Void = unsafe { std::mem::transmute(()) };
//~^ ERROR static of uninhabited type
//~| ERROR value of uninhabited type `Void`
static NEVER2: Void = unsafe { std::mem::transmute(()) }; //~ ERROR static of uninhabited type
//~| WARN: previously accepted
static NEVER2: Void = unsafe { std::mem::transmute(()) };
//~^ ERROR static of uninhabited type
//~| ERROR value of uninhabited type `Void`

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

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

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

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

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

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