Skip to content

Commit cb07edd

Browse files
committed
Allow unsafe extern on all editions
1 parent f4b3f15 commit cb07edd

File tree

10 files changed

+79
-36
lines changed

10 files changed

+79
-36
lines changed

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,15 +1021,12 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
10211021
walk_list!(self, visit_attribute, &item.attrs);
10221022
return; // Avoid visiting again.
10231023
}
1024-
ItemKind::ForeignMod(ForeignMod { abi, unsafety, .. }) => {
1024+
ItemKind::ForeignMod(ForeignMod { abi, .. }) => {
10251025
let old_item = mem::replace(&mut self.extern_mod, Some(item));
10261026
self.visibility_not_permitted(
10271027
&item.vis,
10281028
errors::VisibilityNotPermittedNote::IndividualForeignItems,
10291029
);
1030-
if let &Unsafe::Yes(span) = unsafety {
1031-
self.dcx().emit_err(errors::UnsafeItem { span, kind: "extern block" });
1032-
}
10331030
if abi.is_none() {
10341031
self.maybe_lint_missing_abi(item.span, item.id);
10351032
}

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,15 +567,29 @@ pub enum ArchiveKind {
567567
}
568568

569569
// LLVMRustThinLTOData
570+
#[cfg(bootstrap)]
570571
extern "C" {
571572
pub type ThinLTOData;
572573
}
573574

574575
// LLVMRustThinLTOBuffer
576+
#[cfg(bootstrap)]
575577
extern "C" {
576578
pub type ThinLTOBuffer;
577579
}
578580

581+
// LLVMRustThinLTOData
582+
#[cfg(not(bootstrap))]
583+
unsafe extern "C" {
584+
pub type ThinLTOData;
585+
}
586+
587+
// LLVMRustThinLTOBuffer
588+
#[cfg(not(bootstrap))]
589+
unsafe extern "C" {
590+
pub type ThinLTOBuffer;
591+
}
592+
579593
/// LLVMRustThinLTOModule
580594
#[repr(C)]
581595
pub struct ThinLTOModule {
@@ -634,30 +648,71 @@ struct InvariantOpaque<'a> {
634648
}
635649

636650
// Opaque pointer types
651+
#[cfg(bootstrap)]
637652
extern "C" {
638653
pub type Module;
639654
}
655+
#[cfg(bootstrap)]
640656
extern "C" {
641657
pub type Context;
642658
}
659+
#[cfg(bootstrap)]
643660
extern "C" {
644661
pub type Type;
645662
}
663+
#[cfg(bootstrap)]
646664
extern "C" {
647665
pub type Value;
648666
}
667+
#[cfg(bootstrap)]
649668
extern "C" {
650669
pub type ConstantInt;
651670
}
671+
#[cfg(bootstrap)]
652672
extern "C" {
653673
pub type Attribute;
654674
}
675+
#[cfg(bootstrap)]
655676
extern "C" {
656677
pub type Metadata;
657678
}
679+
#[cfg(bootstrap)]
658680
extern "C" {
659681
pub type BasicBlock;
660682
}
683+
#[cfg(not(bootstrap))]
684+
unsafe extern "C" {
685+
pub type Module;
686+
}
687+
#[cfg(not(bootstrap))]
688+
unsafe extern "C" {
689+
pub type Context;
690+
}
691+
#[cfg(not(bootstrap))]
692+
unsafe extern "C" {
693+
pub type Type;
694+
}
695+
#[cfg(not(bootstrap))]
696+
unsafe extern "C" {
697+
pub type Value;
698+
}
699+
#[cfg(not(bootstrap))]
700+
unsafe extern "C" {
701+
pub type ConstantInt;
702+
}
703+
#[cfg(not(bootstrap))]
704+
unsafe extern "C" {
705+
pub type Attribute;
706+
}
707+
#[cfg(not(bootstrap))]
708+
unsafe extern "C" {
709+
pub type Metadata;
710+
}
711+
#[cfg(not(bootstrap))]
712+
unsafe extern "C" {
713+
pub type BasicBlock;
714+
}
715+
661716
#[repr(C)]
662717
pub struct Builder<'a>(InvariantOpaque<'a>);
663718
#[repr(C)]

compiler/rustc_llvm/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,14 @@ pub fn initialize_available_targets() {
4343
($cfg:meta, $($method:ident),*) => { {
4444
#[cfg($cfg)]
4545
fn init() {
46+
#[cfg(bootstrap)]
4647
extern "C" {
4748
$(fn $method();)*
4849
}
50+
#[cfg(not(bootstrap))]
51+
unsafe extern "C" {
52+
$(fn $method();)*
53+
}
4954
unsafe {
5055
$($method();)*
5156
}

compiler/rustc_middle/src/ty/list.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,20 @@ impl<T> Default for &List<T> {
6161
}
6262
}
6363

64+
#[cfg(bootstrap)]
6465
extern "C" {
6566
/// A dummy type used to force `List` to be unsized while not requiring
6667
/// references to it be wide pointers.
6768
type OpaqueListContents;
6869
}
6970

71+
#[cfg(not(bootstrap))]
72+
unsafe extern "C" {
73+
/// A dummy type used to force `List` to be unsized while not requiring
74+
/// references to it be wide pointers.
75+
type OpaqueListContents;
76+
}
77+
7078
impl<H, T> RawList<H, T> {
7179
#[inline(always)]
7280
pub fn len(&self) -> usize {
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
extern "C" unsafe {
2-
//~^ ERROR expected `{`, found keyword `unsafe`
3-
//~| ERROR extern block cannot be declared unsafe
2+
//~^ ERROR expected `{`, found keyword `unsafe`
43
unsafe fn foo();
5-
//~^ ERROR functions in `extern` blocks cannot have qualifiers
4+
//~^ ERROR functions in `extern` blocks cannot have qualifiers
65
}
76

87
fn main() {}

tests/ui/parser/unsafe-foreign-mod-2.stderr

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,14 @@ error: expected `{`, found keyword `unsafe`
44
LL | extern "C" unsafe {
55
| ^^^^^^ expected `{`
66

7-
error: extern block cannot be declared unsafe
8-
--> $DIR/unsafe-foreign-mod-2.rs:1:12
9-
|
10-
LL | extern "C" unsafe {
11-
| ^^^^^^
12-
137
error: functions in `extern` blocks cannot have qualifiers
14-
--> $DIR/unsafe-foreign-mod-2.rs:4:5
8+
--> $DIR/unsafe-foreign-mod-2.rs:3:5
159
|
1610
LL | extern "C" unsafe {
1711
| ----------------- in this `extern` block
18-
...
12+
LL |
1913
LL | unsafe fn foo();
2014
| ^^^^^^ help: remove this qualifier
2115

22-
error: aborting due to 3 previous errors
16+
error: aborting due to 2 previous errors
2317

tests/ui/parser/unsafe-foreign-mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
unsafe extern "C" {
2-
//~^ ERROR extern block cannot be declared unsafe
3-
}
1+
//@ build-pass
2+
3+
unsafe extern "C" {}
44

55
fn main() {}

tests/ui/parser/unsafe-foreign-mod.stderr

Lines changed: 0 additions & 8 deletions
This file was deleted.

tests/ui/rust-2024/unsafe-extern-blocks/extern-items.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
//@ build-pass
2+
13
extern "C" {
24
static TEST1: i32;
35
fn test1(i: i32);
46
}
57

68
unsafe extern "C" {
7-
//~^ ERROR: extern block cannot be declared unsafe
89
static TEST2: i32;
910
fn test2(i: i32);
1011
}

tests/ui/rust-2024/unsafe-extern-blocks/extern-items.stderr

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)