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
2 changes: 2 additions & 0 deletions compiler/rustc_mir_transform/src/promote_consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,8 @@ impl<'tcx> Validator<'_, 'tcx> {
// can only promote static accesses inside statics.
&& let Some(hir::ConstContext::Static(..)) = self.const_kind
&& !self.tcx.is_thread_local_static(did)
// Extern statics can never be read by CTFE, even inside a static.
&& !self.tcx.is_foreign_item(did)
{
// Recurse.
} else {
Expand Down

This file was deleted.

44 changes: 0 additions & 44 deletions tests/mir-opt/const_promotion_extern_static.FOO.PromoteTemps.diff

This file was deleted.

7 changes: 0 additions & 7 deletions tests/mir-opt/const_promotion_extern_static.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
//@ skip-filecheck
//@ ignore-endian-big
extern "C" {
static X: i32;
}
static Y: i32 = 42;

// EMIT_MIR const_promotion_extern_static.BAR.PromoteTemps.diff
// EMIT_MIR const_promotion_extern_static.BAR-promoted[0].SimplifyCfg-pre-optimizations.after.mir
static mut BAR: *const &i32 = [&Y].as_ptr();

// EMIT_MIR const_promotion_extern_static.FOO.PromoteTemps.diff
// EMIT_MIR const_promotion_extern_static.FOO-promoted[0].SimplifyCfg-pre-optimizations.after.mir
static mut FOO: *const &i32 = [unsafe { &X }].as_ptr();

// EMIT_MIR const_promotion_extern_static.BOP.built.after.mir
static BOP: &i32 = &13;

Expand Down
15 changes: 15 additions & 0 deletions tests/ui/statics/extern-static-in-static-ice-143174.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Regression test for #143174.

#![crate_type = "lib"]

type Fun = unsafe extern "C" fn();

struct Foo(Fun);

static FOO: &Foo = &Foo(BAR);
//~^ ERROR cannot access extern static `BAR` [E0080]
//~| ERROR use of extern static is unsafe and requires unsafe function or block [E0133]

unsafe extern "C" {
static BAR: Fun;
}
18 changes: 18 additions & 0 deletions tests/ui/statics/extern-static-in-static-ice-143174.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
error[E0080]: cannot access extern static `BAR`
--> $DIR/extern-static-in-static-ice-143174.rs:9:25
|
LL | static FOO: &Foo = &Foo(BAR);
| ^^^ evaluation of `FOO` failed here

error[E0133]: use of extern static is unsafe and requires unsafe function or block
--> $DIR/extern-static-in-static-ice-143174.rs:9:25
|
LL | static FOO: &Foo = &Foo(BAR);
| ^^^ use of extern static
|
= note: extern statics are not controlled by the Rust type system: invalid data, aliasing violations or data races will cause undefined behavior

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0080, E0133.
For more information about an error, try `rustc --explain E0080`.
11 changes: 11 additions & 0 deletions tests/ui/statics/extern-static-promotion-rejected.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// previously part of tests/mir-opt/const_promotion_extern_static.rs
// promotion of extern statics is now rejected entirely, even if we're not trying to read its value

unsafe extern "C" {
static X: i32;
}

static mut FOO: *const &i32 = [unsafe { &X }].as_ptr();
//~^ ERROR dangling pointer

fn main() {}
8 changes: 8 additions & 0 deletions tests/ui/statics/extern-static-promotion-rejected.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: encountered dangling pointer in final value of mutable static
--> $DIR/extern-static-promotion-rejected.rs:8:1
|
LL | static mut FOO: *const &i32 = [unsafe { &X }].as_ptr();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 1 previous error

Loading