Skip to content

Commit 6257168

Browse files
committed
check if a static is extern before trying to promote
1 parent f3ef3bd commit 6257168

3 files changed

Lines changed: 34 additions & 0 deletions

File tree

compiler/rustc_mir_transform/src/promote_consts.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ impl<'tcx> Validator<'_, 'tcx> {
317317
// can only promote static accesses inside statics.
318318
&& let Some(hir::ConstContext::Static(..)) = self.const_kind
319319
&& !self.tcx.is_thread_local_static(did)
320+
&& !self.tcx.is_foreign_item(did)
320321
{
321322
// Recurse.
322323
} else {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Regression test for #143174.
2+
3+
#![crate_type = "lib"]
4+
5+
type Fun = unsafe extern "C" fn();
6+
7+
struct Foo(Fun);
8+
9+
static FOO: &Foo = &Foo(BAR);
10+
//~^ ERROR cannot access extern static `BAR` [E0080]
11+
//~| ERROR use of extern static is unsafe and requires unsafe function or block [E0133]
12+
13+
unsafe extern "C" {
14+
static BAR: Fun;
15+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error[E0080]: cannot access extern static `BAR`
2+
--> $DIR/extern-static-in-static-ice-143174.rs:9:25
3+
|
4+
LL | static FOO: &Foo = &Foo(BAR);
5+
| ^^^ evaluation of `FOO` failed here
6+
7+
error[E0133]: use of extern static is unsafe and requires unsafe function or block
8+
--> $DIR/extern-static-in-static-ice-143174.rs:9:25
9+
|
10+
LL | static FOO: &Foo = &Foo(BAR);
11+
| ^^^ use of extern static
12+
|
13+
= note: extern statics are not controlled by the Rust type system: invalid data, aliasing violations or data races will cause undefined behavior
14+
15+
error: aborting due to 2 previous errors
16+
17+
Some errors have detailed explanations: E0080, E0133.
18+
For more information about an error, try `rustc --explain E0080`.

0 commit comments

Comments
 (0)