Skip to content

Commit 3b9128d

Browse files
committed
safe_extern_static -> error
1 parent 1d08c8e commit 3b9128d

File tree

13 files changed

+55
-94
lines changed

13 files changed

+55
-94
lines changed

src/doc/rustc/src/lints/listing/deny-by-default.md

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,6 @@ To fix it, remove the `()`s.
118118

119119
This lint detects a specific situation of re-exporting a private `extern crate`;
120120

121-
## safe-extern-statics
122-
123-
In older versions of Rust, there was a soundness issue where `extern static`s were allowed
124-
to be accessed in safe code. This lint now catches and denies this kind of code.
125-
126121
## unknown-crate-types
127122

128123
This lint detects an unknown crate type found in a `#[crate_type]` directive. Some

src/librustc/lint/builtin.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,6 @@ declare_lint! {
145145
"lints that have been renamed or removed"
146146
}
147147

148-
declare_lint! {
149-
pub SAFE_EXTERN_STATICS,
150-
Deny,
151-
"safe access to extern statics was erroneously allowed"
152-
}
153-
154148
declare_lint! {
155149
pub SAFE_PACKED_BORROWS,
156150
Warn,
@@ -397,7 +391,6 @@ declare_lint_pass! {
397391
PUB_USE_OF_PRIVATE_EXTERN_CRATE,
398392
CONST_ERR,
399393
RENAMED_AND_REMOVED_LINTS,
400-
SAFE_EXTERN_STATICS,
401394
SAFE_PACKED_BORROWS,
402395
PATTERNS_IN_FNS_WITHOUT_BODY,
403396
PARENTHESIZED_PARAMS_IN_TYPES_AND_MODULES,

src/librustc/mir/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2803,7 +2803,6 @@ pub enum UnsafetyViolationKind {
28032803
General,
28042804
/// Permitted in const fn and regular fns.
28052805
GeneralAndConstFn,
2806-
ExternStatic(hir::HirId),
28072806
BorrowPacked(hir::HirId),
28082807
}
28092808

src/librustc_lint/lib.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -321,11 +321,6 @@ pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) {
321321
reference: "issue #49716 <https://github.com/rust-lang/rust/issues/49716>",
322322
edition: Some(Edition::Edition2018),
323323
},
324-
FutureIncompatibleInfo {
325-
id: LintId::of(SAFE_EXTERN_STATICS),
326-
reference: "issue #36247 <https://github.com/rust-lang/rust/issues/36247>",
327-
edition: None,
328-
},
329324
FutureIncompatibleInfo {
330325
id: LintId::of(ILLEGAL_FLOATING_POINT_LITERAL_PATTERN),
331326
reference: "issue #41620 <https://github.com/rust-lang/rust/issues/41620>",
@@ -476,6 +471,8 @@ pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) {
476471
"converted into hard error, see https://github.com/rust-lang/rust/issues/37872");
477472
store.register_removed("invalid_type_param_default",
478473
"converted into hard error, see https://github.com/rust-lang/rust/issues/36887");
474+
store.register_removed("safe_extern_statics",
475+
"converted into hard error, see https://github.com/rust-lang/rust/issues/36247");
479476
}
480477

481478
pub fn register_internals(store: &mut lint::LintStore, sess: Option<&Session>) {

src/librustc_mir/transform/check_unsafety.rs

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc::ty::cast::CastTy;
88
use rustc::hir;
99
use rustc::hir::Node;
1010
use rustc::hir::def_id::DefId;
11-
use rustc::lint::builtin::{SAFE_EXTERN_STATICS, SAFE_PACKED_BORROWS, UNUSED_UNSAFE};
11+
use rustc::lint::builtin::{SAFE_PACKED_BORROWS, UNUSED_UNSAFE};
1212
use rustc::mir::*;
1313
use rustc::mir::visit::{PlaceContext, Visitor, MutatingUseContext};
1414

@@ -210,23 +210,19 @@ impl<'a, 'tcx> Visitor<'tcx> for UnsafetyChecker<'a, 'tcx> {
210210
}
211211
PlaceBase::Static(box Static { kind: StaticKind::Static(def_id), .. }) => {
212212
if self.tcx.is_mutable_static(*def_id) {
213-
self.require_unsafe("use of mutable static",
213+
self.require_unsafe(
214+
"use of mutable static",
214215
"mutable statics can be mutated by multiple threads: aliasing \
215216
violations or data races will cause undefined behavior",
216-
UnsafetyViolationKind::General);
217+
UnsafetyViolationKind::General,
218+
);
217219
} else if self.tcx.is_foreign_item(*def_id) {
218-
let source_info = self.source_info;
219-
let lint_root =
220-
self.source_scope_local_data[source_info.scope].lint_root;
221-
self.register_violations(&[UnsafetyViolation {
222-
source_info,
223-
description: InternedString::intern("use of extern static"),
224-
details: InternedString::intern(
225-
"extern statics are not controlled by the Rust type system: \
226-
invalid data, aliasing violations or data races will cause \
227-
undefined behavior"),
228-
kind: UnsafetyViolationKind::ExternStatic(lint_root)
229-
}], &[]);
220+
self.require_unsafe(
221+
"use of extern static",
222+
"extern statics are not controlled by the Rust type system: invalid \
223+
data, aliasing violations or data races will cause undefined behavior",
224+
UnsafetyViolationKind::General
225+
);
230226
}
231227
}
232228
}
@@ -352,8 +348,7 @@ impl<'a, 'tcx> UnsafetyChecker<'a, 'tcx> {
352348
match violation.kind {
353349
UnsafetyViolationKind::GeneralAndConstFn |
354350
UnsafetyViolationKind::General => {},
355-
UnsafetyViolationKind::BorrowPacked(_) |
356-
UnsafetyViolationKind::ExternStatic(_) => if self.min_const_fn {
351+
UnsafetyViolationKind::BorrowPacked(_) => if self.min_const_fn {
357352
// const fns don't need to be backwards compatible and can
358353
// emit these violations as a hard error instead of a backwards
359354
// compat lint
@@ -381,8 +376,7 @@ impl<'a, 'tcx> UnsafetyChecker<'a, 'tcx> {
381376
UnsafetyViolationKind::GeneralAndConstFn => {},
382377
// these things are forbidden in const fns
383378
UnsafetyViolationKind::General |
384-
UnsafetyViolationKind::BorrowPacked(_) |
385-
UnsafetyViolationKind::ExternStatic(_) => {
379+
UnsafetyViolationKind::BorrowPacked(_) => {
386380
let mut violation = violation.clone();
387381
// const fns don't need to be backwards compatible and can
388382
// emit these violations as a hard error instead of a backwards
@@ -646,14 +640,6 @@ pub fn check_unsafety(tcx: TyCtxt<'_>, def_id: DefId) {
646640
.note(&details.as_str()[..])
647641
.emit();
648642
}
649-
UnsafetyViolationKind::ExternStatic(lint_hir_id) => {
650-
tcx.lint_node_note(SAFE_EXTERN_STATICS,
651-
lint_hir_id,
652-
source_info.span,
653-
&format!("{} is unsafe and requires unsafe function or block \
654-
(error E0133)", &description.as_str()[..]),
655-
&details.as_str()[..]);
656-
}
657643
UnsafetyViolationKind::BorrowPacked(lint_hir_id) => {
658644
if let Some(impl_def_id) = builtin_derive_def_id(tcx, def_id) {
659645
tcx.unsafe_derive_on_repr_packed(impl_def_id);

src/test/ui/issues/issue-14227.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
#![allow(safe_extern_statics, warnings)]
2-
31
extern {
42
pub static symbol: u32;
53
}
64
static CRASH: u32 = symbol;
7-
//~^ ERROR could not evaluate static initializer
8-
//~| tried to read from foreign (extern) static
5+
//~^ ERROR use of extern static is unsafe and requires
96

107
fn main() {}

src/test/ui/issues/issue-14227.stderr

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
error[E0080]: could not evaluate static initializer
2-
--> $DIR/issue-14227.rs:6:21
1+
error[E0133]: use of extern static is unsafe and requires unsafe function or block
2+
--> $DIR/issue-14227.rs:4:21
33
|
44
LL | static CRASH: u32 = symbol;
5-
| ^^^^^^ tried to read from foreign (extern) static
5+
| ^^^^^^ use of extern static
6+
|
7+
= note: extern statics are not controlled by the Rust type system: invalid data, aliasing violations or data races will cause undefined behavior
68

79
error: aborting due to previous error
810

9-
For more information about this error, try `rustc --explain E0080`.
11+
For more information about this error, try `rustc --explain E0133`.

src/test/ui/issues/issue-16538.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#![allow(safe_extern_statics)]
2-
31
mod Y {
42
pub type X = usize;
53
extern {
@@ -13,5 +11,6 @@ mod Y {
1311
static foo: *const Y::X = Y::foo(Y::x as *const Y::X);
1412
//~^ ERROR `*const usize` cannot be shared between threads safely [E0277]
1513
//~| ERROR E0015
14+
//~| ERROR use of extern static is unsafe and requires
1615

1716
fn main() {}

src/test/ui/issues/issue-16538.stderr

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
error[E0015]: calls in statics are limited to constant functions, tuple structs and tuple variants
2-
--> $DIR/issue-16538.rs:13:27
2+
--> $DIR/issue-16538.rs:11:27
33
|
44
LL | static foo: *const Y::X = Y::foo(Y::x as *const Y::X);
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
66

77
error[E0277]: `*const usize` cannot be shared between threads safely
8-
--> $DIR/issue-16538.rs:13:1
8+
--> $DIR/issue-16538.rs:11:1
99
|
1010
LL | static foo: *const Y::X = Y::foo(Y::x as *const Y::X);
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `*const usize` cannot be shared between threads safely
1212
|
1313
= help: the trait `std::marker::Sync` is not implemented for `*const usize`
1414
= note: shared static variables must have a type that implements `Sync`
1515

16-
error: aborting due to 2 previous errors
16+
error[E0133]: use of extern static is unsafe and requires unsafe function or block
17+
--> $DIR/issue-16538.rs:11:34
18+
|
19+
LL | static foo: *const Y::X = Y::foo(Y::x as *const Y::X);
20+
| ^^^^ use of extern static
21+
|
22+
= note: extern statics are not controlled by the Rust type system: invalid data, aliasing violations or data races will cause undefined behavior
23+
24+
error: aborting due to 3 previous errors
1725

18-
Some errors have detailed explanations: E0015, E0277.
26+
Some errors have detailed explanations: E0015, E0133, E0277.
1927
For more information about an error, try `rustc --explain E0015`.

src/test/ui/issues/issue-28324.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
#![allow(safe_extern_statics)]
2-
31
extern {
42
static error_message_count: u32;
53
}
64

75
pub static BAZ: u32 = *&error_message_count;
8-
//~^ ERROR could not evaluate static initializer
9-
//~| tried to read from foreign (extern) static
6+
//~^ ERROR use of extern static is unsafe and requires
107

118
fn main() {}

src/test/ui/issues/issue-28324.stderr

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
error[E0080]: could not evaluate static initializer
2-
--> $DIR/issue-28324.rs:7:23
1+
error[E0133]: use of extern static is unsafe and requires unsafe function or block
2+
--> $DIR/issue-28324.rs:5:24
33
|
44
LL | pub static BAZ: u32 = *&error_message_count;
5-
| ^^^^^^^^^^^^^^^^^^^^^ tried to read from foreign (extern) static
5+
| ^^^^^^^^^^^^^^^^^^^^ use of extern static
6+
|
7+
= note: extern statics are not controlled by the Rust type system: invalid data, aliasing violations or data races will cause undefined behavior
68

79
error: aborting due to previous error
810

9-
For more information about this error, try `rustc --explain E0080`.
11+
For more information about this error, try `rustc --explain E0133`.

src/test/ui/safe-extern-statics.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
// aux-build:extern-statics.rs
22

3-
#![allow(unused)]
4-
53
extern crate extern_statics;
64
use extern_statics::*;
75

@@ -11,11 +9,7 @@ extern {
119

1210
fn main() {
1311
let a = A; //~ ERROR use of extern static is unsafe
14-
//~^ WARN this was previously accepted by the compiler
1512
let ra = &A; //~ ERROR use of extern static is unsafe
16-
//~^ WARN this was previously accepted by the compiler
1713
let xa = XA; //~ ERROR use of extern static is unsafe
18-
//~^ WARN this was previously accepted by the compiler
1914
let xra = &XA; //~ ERROR use of extern static is unsafe
20-
//~^ WARN this was previously accepted by the compiler
2115
}
Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,35 @@
1-
error: use of extern static is unsafe and requires unsafe function or block (error E0133)
2-
--> $DIR/safe-extern-statics.rs:13:13
1+
error[E0133]: use of extern static is unsafe and requires unsafe function or block
2+
--> $DIR/safe-extern-statics.rs:11:13
33
|
44
LL | let a = A;
5-
| ^
5+
| ^ use of extern static
66
|
7-
= note: `#[deny(safe_extern_statics)]` on by default
8-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
9-
= note: for more information, see issue #36247 <https://github.com/rust-lang/rust/issues/36247>
107
= note: extern statics are not controlled by the Rust type system: invalid data, aliasing violations or data races will cause undefined behavior
118

12-
error: use of extern static is unsafe and requires unsafe function or block (error E0133)
13-
--> $DIR/safe-extern-statics.rs:15:14
9+
error[E0133]: use of extern static is unsafe and requires unsafe function or block
10+
--> $DIR/safe-extern-statics.rs:12:14
1411
|
1512
LL | let ra = &A;
16-
| ^^
13+
| ^^ use of extern static
1714
|
18-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
19-
= note: for more information, see issue #36247 <https://github.com/rust-lang/rust/issues/36247>
2015
= note: extern statics are not controlled by the Rust type system: invalid data, aliasing violations or data races will cause undefined behavior
2116

22-
error: use of extern static is unsafe and requires unsafe function or block (error E0133)
23-
--> $DIR/safe-extern-statics.rs:17:14
17+
error[E0133]: use of extern static is unsafe and requires unsafe function or block
18+
--> $DIR/safe-extern-statics.rs:13:14
2419
|
2520
LL | let xa = XA;
26-
| ^^
21+
| ^^ use of extern static
2722
|
28-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
29-
= note: for more information, see issue #36247 <https://github.com/rust-lang/rust/issues/36247>
3023
= note: extern statics are not controlled by the Rust type system: invalid data, aliasing violations or data races will cause undefined behavior
3124

32-
error: use of extern static is unsafe and requires unsafe function or block (error E0133)
33-
--> $DIR/safe-extern-statics.rs:19:15
25+
error[E0133]: use of extern static is unsafe and requires unsafe function or block
26+
--> $DIR/safe-extern-statics.rs:14:15
3427
|
3528
LL | let xra = &XA;
36-
| ^^^
29+
| ^^^ use of extern static
3730
|
38-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
39-
= note: for more information, see issue #36247 <https://github.com/rust-lang/rust/issues/36247>
4031
= note: extern statics are not controlled by the Rust type system: invalid data, aliasing violations or data races will cause undefined behavior
4132

4233
error: aborting due to 4 previous errors
4334

35+
For more information about this error, try `rustc --explain E0133`.

0 commit comments

Comments
 (0)