Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
13a5067
Unignore test
Oct 13, 2020
0558e6e
bootstrap: fall back to auto-detected CXX
jonas-schievink Oct 14, 2020
77a7ccf
bootstrap: configure native toolchain for run-make
jonas-schievink Oct 14, 2020
e36de6b
Move issue-36710 test to run-make
jonas-schievink Oct 14, 2020
a671604
Ignore test on WASM
jonas-schievink Oct 14, 2020
d7c7649
ignore-thumb
jonas-schievink Oct 16, 2020
60594b1
Ignore on 32-bit targets
Oct 19, 2020
d80f127
Avoid panic_bounds_check in fmt::write.
m-ou-se Oct 19, 2020
ea24395
Add debug_asserts for the unsafe indexing in fmt::write.
m-ou-se Oct 20, 2020
356d5b5
Add test to check for fmt::write bloat.
m-ou-se Oct 20, 2020
52640f2
[mir-opt] Allow debuginfo to be generated for a constant or a Place
wesleywiser May 30, 2020
2202653
Miri engine validity check: simplify code with 'matches!'
RalfJung Oct 21, 2020
fcaf233
Miri engine interning: improve comments, and entirely skip ZST
RalfJung Oct 21, 2020
484d9eb
Move fmt-write-bloat test to run-make-fulldeps.
m-ou-se Oct 21, 2020
d0d0e78
Capture output from threads spawned in tests
tmandry Aug 5, 2020
db15596
Only load LOCAL_STREAMS if they are being used
SergioBenitez Oct 22, 2020
86df903
Tweak "use `.await`" suggestion
estebank Oct 21, 2020
a4ee3ca
Suggest semicolon removal on prior match arm
estebank Oct 22, 2020
671d7c4
Account for possible boxable `impl Future` in semicolon removal sugge…
estebank Oct 22, 2020
62ba365
Review comments: use newtype instead of `bool`
estebank Oct 22, 2020
3a0227b
Silence unnecessary `await foo?` knock-down error
estebank Oct 22, 2020
1829b4a
Add test case for different `impl Future`s
estebank Oct 22, 2020
c548511
Add more `.await` suggestions on E0308
estebank Oct 23, 2020
f5d7443
Suggest semicolon removal and boxing when appropriate
estebank Oct 23, 2020
1333206
ensure that statics are inhabited
RalfJung Oct 24, 2020
5d62492
fix typo
RalfJung Oct 24, 2020
7b4c397
Do not try to report on closures to avoid ICE
JohnTitor Oct 23, 2020
4ec396e
Test with NLL explicitly
JohnTitor Oct 25, 2020
e218380
Make some functions private that don't have to be public
jyn514 Oct 25, 2020
3bd5cc9
also test non-extern uninhabited statics
RalfJung Oct 25, 2020
6de07f8
Rollup merge of #73210 - wesleywiser:consts_in_debuginfo, r=oli-obk
Dylan-DPC Oct 25, 2020
66fed47
Rollup merge of #77901 - jonas-schievink:unignore-test-36710, r=Mark-…
Dylan-DPC Oct 25, 2020
5d07254
Rollup merge of #78122 - fusion-engineering-forks:fmt-write-bounds-ch…
Dylan-DPC Oct 25, 2020
423d164
Rollup merge of #78179 - RalfJung:miri-comments, r=oli-obk
Dylan-DPC Oct 25, 2020
a3bd184
Rollup merge of #78214 - estebank:match-semicolon, r=oli-obk
Dylan-DPC Oct 25, 2020
0c44b23
Rollup merge of #78227 - SergioBenitez:test-stdout-threading, r=m-ou-se
Dylan-DPC Oct 25, 2020
73c9339
Rollup merge of #78268 - JohnTitor:issue-78262, r=estebank
Dylan-DPC Oct 25, 2020
21cd03b
Rollup merge of #78324 - RalfJung:uninhabited-statics, r=oli-obk
Dylan-DPC Oct 25, 2020
9b485eb
Rollup merge of #78348 - jyn514:pub-crate, r=GuillaumeGomez
Dylan-DPC Oct 25, 2020
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
Prev Previous commit
Next Next commit
ensure that statics are inhabited
  • Loading branch information
RalfJung committed Oct 24, 2020
commit 1333206eb32a78ef6ffe0defd4acd164e47146b7
30 changes: 30 additions & 0 deletions compiler/rustc_session/src/lint/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2647,6 +2647,35 @@ declare_lint! {
};
}

declare_lint! {
/// The `uninhabited_static` lint detects uninhbaited statics.
///
/// ### Example
///
/// ```rust
/// enum Void {}
/// 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 accientally allowed, but is being phased out.
pub UNINHABITED_STATIC,
Warn,
"uninhabited static",
@future_incompatible = FutureIncompatibleInfo {
reference: "issue #74840 <https://github.com/rust-lang/rust/issues/74840>",
edition: None,
};
}

declare_tool_lint! {
pub rustc::INEFFECTIVE_UNSTABLE_TRAIT_IMPL,
Deny,
Expand Down Expand Up @@ -2732,6 +2761,7 @@ declare_lint_pass! {
CENUM_IMPL_DROP_CAST,
CONST_EVALUATABLE_UNCHECKED,
INEFFECTIVE_UNSTABLE_TRAIT_IMPL,
UNINHABITED_STATIC,
]
}

Expand Down
51 changes: 45 additions & 6 deletions compiler/rustc_typeck/src/check/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ use rustc_infer::infer::{RegionVariableOrigin, TyCtxtInferExt};
use rustc_middle::ty::fold::TypeFoldable;
use rustc_middle::ty::subst::GenericArgKind;
use rustc_middle::ty::util::{Discr, IntTypeExt, Representability};
use rustc_middle::ty::{self, RegionKind, ToPredicate, Ty, TyCtxt};
use rustc_middle::ty::{self, ParamEnv, RegionKind, ToPredicate, Ty, TyCtxt};
use rustc_session::config::EntryFnType;
use rustc_session::lint::builtin::UNINHABITED_STATIC;
use rustc_span::symbol::sym;
use rustc_span::{self, MultiSpan, Span};
use rustc_target::spec::abi::Abi;
Expand Down Expand Up @@ -338,7 +339,7 @@ pub(super) fn check_struct(tcx: TyCtxt<'_>, id: hir::HirId, span: Span) {
check_packed(tcx, span, def);
}

pub(super) fn check_union(tcx: TyCtxt<'_>, id: hir::HirId, span: Span) {
fn check_union(tcx: TyCtxt<'_>, id: hir::HirId, span: Span) {
let def_id = tcx.hir().local_def_id(id);
let def = tcx.adt_def(def_id);
def.destructor(tcx); // force the destructor to be evaluated
Expand All @@ -349,7 +350,7 @@ pub(super) fn check_union(tcx: TyCtxt<'_>, id: hir::HirId, span: Span) {
}

/// Check that the fields of the `union` do not need dropping.
pub(super) fn check_union_fields(tcx: TyCtxt<'_>, span: Span, item_def_id: LocalDefId) -> bool {
fn check_union_fields(tcx: TyCtxt<'_>, span: Span, item_def_id: LocalDefId) -> bool {
let item_type = tcx.type_of(item_def_id);
if let ty::Adt(def, substs) = item_type.kind() {
assert!(def.is_union());
Expand Down Expand Up @@ -377,6 +378,36 @@ pub(super) fn check_union_fields(tcx: TyCtxt<'_>, span: Span, item_def_id: Local
true
}

/// Check that a `static` is inhabited.
fn check_static_inhabited<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId, span: Span) {
// Make sure statics are inhabited.
// Other parts of the compiler assume that there are no uninhabited places. In principle it
// would be enugh to check this for `extern` statics, as statics with an initializer will
// have UB during initialization if they are uninhabited, but there also seems to be no good
// reason to allow any statics to be uninhabited.
let ty = tcx.type_of(def_id);
let layout = match tcx.layout_of(ParamEnv::reveal_all().and(ty)) {
Ok(l) => l,
Err(_) => {
// Generic statics are rejected, but we still reach this case.
tcx.sess.delay_span_bug(span, "generic static must be rejected");
return;
}
};
if layout.abi.is_uninhabited() {
tcx.struct_span_lint_hir(
UNINHABITED_STATIC,
tcx.hir().local_def_id_to_hir_id(def_id),
span,
|lint| {
lint.build("static of uninhabited type")
.note("uninhabited statics cannot be initialized, and any access would be an immediate error")
.emit();
},
);
}
}

/// Checks that an opaque type does not contain cycles and does not use `Self` or `T::Foo`
/// projections that would result in "inheriting lifetimes".
pub(super) fn check_opaque<'tcx>(
Expand Down Expand Up @@ -609,6 +640,7 @@ pub fn check_item_type<'tcx>(tcx: TyCtxt<'tcx>, it: &'tcx hir::Item<'tcx>) {
let def_id = tcx.hir().local_def_id(it.hir_id);
tcx.ensure().typeck(def_id);
maybe_check_static_with_link_section(tcx, def_id, it.span);
check_static_inhabited(tcx, def_id, it.span);
}
hir::ItemKind::Const(..) => {
tcx.ensure().typeck(tcx.hir().local_def_id(it.hir_id));
Expand Down Expand Up @@ -691,7 +723,8 @@ pub fn check_item_type<'tcx>(tcx: TyCtxt<'tcx>, it: &'tcx hir::Item<'tcx>) {
}
} else {
for item in m.items {
let generics = tcx.generics_of(tcx.hir().local_def_id(item.hir_id));
let def_id = tcx.hir().local_def_id(item.hir_id);
let generics = tcx.generics_of(def_id);
let own_counts = generics.own_counts();
if generics.params.len() - own_counts.lifetimes != 0 {
let (kinds, kinds_pl, egs) = match (own_counts.types, own_counts.consts) {
Expand Down Expand Up @@ -722,8 +755,14 @@ pub fn check_item_type<'tcx>(tcx: TyCtxt<'tcx>, it: &'tcx hir::Item<'tcx>) {
.emit();
}

if let hir::ForeignItemKind::Fn(ref fn_decl, _, _) = item.kind {
require_c_abi_if_c_variadic(tcx, fn_decl, m.abi, item.span);
match item.kind {
hir::ForeignItemKind::Fn(ref fn_decl, _, _) => {
require_c_abi_if_c_variadic(tcx, fn_decl, m.abi, item.span);
}
hir::ForeignItemKind::Static(..) => {
check_static_inhabited(tcx, def_id, item.span);
}
_ => {}
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/test/ui/statics/uninhabited-static.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#![feature(never_type)]
#![deny(uninhabited_static)]

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

fn main() {}
27 changes: 27 additions & 0 deletions src/test/ui/statics/uninhabited-static.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
error: static of uninhabited type
--> $DIR/uninhabited-static.rs:6:5
|
LL | static VOID: Void;
| ^^^^^^^^^^^^^^^^^^
|
note: the lint level is defined here
--> $DIR/uninhabited-static.rs:2:9
|
LL | #![deny(uninhabited_static)]
| ^^^^^^^^^^^^^^^^^^
= 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
|
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: aborting due to 2 previous errors