Skip to content

Commit 10723c2

Browse files
committed
remove support for extern-block const intrinsics
1 parent 7934f26 commit 10723c2

16 files changed

+118
-224
lines changed

compiler/rustc_attr/src/builtin.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,7 @@ pub fn find_stability(
273273
/// Collects stability info from `rustc_const_stable`/`rustc_const_unstable`/`rustc_promotable`
274274
/// attributes in `attrs`. Returns `None` if no stability attributes are found.
275275
///
276-
/// `is_const_fn` indicates whether this is a function marked as `const`. It will always
277-
/// be false for intrinsics in an `extern` block!
276+
/// `is_const_fn` indicates whether this is a function marked as `const`.
278277
pub fn find_const_stability(
279278
sess: &Session,
280279
attrs: &[Attribute],
@@ -330,7 +329,7 @@ pub fn find_const_stability(
330329
}
331330
}
332331

333-
// Merge promotable and not_exposed_on_stable into stability info
332+
// Merge promotable and const_stable_indirect into stability info
334333
if promotable {
335334
match &mut const_stab {
336335
Some((stab, _)) => stab.promotable = promotable,
@@ -352,10 +351,7 @@ pub fn find_const_stability(
352351
})
353352
}
354353
}
355-
_ => {
356-
// We ignore the `#[rustc_const_stable_indirect]` here, it should be picked up by
357-
// the `default_const_unstable` logic.
358-
}
354+
_ => {}
359355
}
360356
}
361357
// Make sure if `const_stable_indirect` is present, that is recorded. Also make sure all `const

compiler/rustc_const_eval/src/const_eval/fn_queries.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,9 @@ fn constness(tcx: TyCtxt<'_>, def_id: LocalDefId) -> hir::Constness {
2525
hir::Constness::Const
2626
}
2727
hir::Node::Item(hir::Item { kind: hir::ItemKind::Impl(impl_), .. }) => impl_.constness,
28-
hir::Node::ForeignItem(hir::ForeignItem { kind: hir::ForeignItemKind::Fn(..), .. }) => {
29-
// Intrinsics use `rustc_const_{un,}stable` attributes to indicate constness. All other
30-
// foreign items cannot be evaluated at compile-time.
31-
let is_const = if tcx.intrinsic(def_id).is_some() {
32-
tcx.lookup_const_stability(def_id).is_some()
33-
} else {
34-
false
35-
};
36-
if is_const { hir::Constness::Const } else { hir::Constness::NotConst }
28+
hir::Node::ForeignItem(_) => {
29+
// Foreign items cannot be evaluated at compile-time.
30+
hir::Constness::NotConst
3731
}
3832
hir::Node::Expr(e) if let hir::ExprKind::Closure(c) = e.kind => c.constness,
3933
_ => {

compiler/rustc_passes/src/stability.rs

-15
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
106106
def_id: LocalDefId,
107107
item_sp: Span,
108108
fn_sig: Option<&'tcx hir::FnSig<'tcx>>,
109-
is_foreign_item: bool,
110109
kind: AnnotationKind,
111110
inherit_deprecation: InheritDeprecation,
112111
inherit_const_stability: InheritConstStability,
@@ -175,11 +174,7 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
175174
// implied), check if the function/method is const or the parent impl block is const.
176175
if let Some(fn_sig) = fn_sig
177176
&& !fn_sig.header.is_const()
178-
// We have to exclude foreign items as they might be intrinsics. Sadly we can't check
179-
// their ABI; `fn_sig.abi` is *not* correct for foreign functions.
180-
&& !is_foreign_item
181177
&& const_stab.is_some()
182-
&& (!self.in_trait_impl || !self.tcx.is_const_fn(def_id.to_def_id()))
183178
{
184179
self.tcx.dcx().emit_err(errors::MissingConstErr { fn_sig_span: fn_sig.span });
185180
}
@@ -398,7 +393,6 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
398393
ctor_def_id,
399394
i.span,
400395
None,
401-
/* is_foreign_item */ false,
402396
AnnotationKind::Required,
403397
InheritDeprecation::Yes,
404398
InheritConstStability::No,
@@ -417,7 +411,6 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
417411
i.owner_id.def_id,
418412
i.span,
419413
fn_sig,
420-
/* is_foreign_item */ false,
421414
kind,
422415
InheritDeprecation::Yes,
423416
const_stab_inherit,
@@ -437,7 +430,6 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
437430
ti.owner_id.def_id,
438431
ti.span,
439432
fn_sig,
440-
/* is_foreign_item */ false,
441433
AnnotationKind::Required,
442434
InheritDeprecation::Yes,
443435
InheritConstStability::No,
@@ -461,7 +453,6 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
461453
ii.owner_id.def_id,
462454
ii.span,
463455
fn_sig,
464-
/* is_foreign_item */ false,
465456
kind,
466457
InheritDeprecation::Yes,
467458
InheritConstStability::No,
@@ -477,7 +468,6 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
477468
var.def_id,
478469
var.span,
479470
None,
480-
/* is_foreign_item */ false,
481471
AnnotationKind::Required,
482472
InheritDeprecation::Yes,
483473
InheritConstStability::No,
@@ -488,7 +478,6 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
488478
ctor_def_id,
489479
var.span,
490480
None,
491-
/* is_foreign_item */ false,
492481
AnnotationKind::Required,
493482
InheritDeprecation::Yes,
494483
InheritConstStability::No,
@@ -507,7 +496,6 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
507496
s.def_id,
508497
s.span,
509498
None,
510-
/* is_foreign_item */ false,
511499
AnnotationKind::Required,
512500
InheritDeprecation::Yes,
513501
InheritConstStability::No,
@@ -527,7 +515,6 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
527515
i.owner_id.def_id,
528516
i.span,
529517
fn_sig,
530-
/* is_foreign_item */ true,
531518
AnnotationKind::Required,
532519
InheritDeprecation::Yes,
533520
InheritConstStability::No,
@@ -550,7 +537,6 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
550537
p.def_id,
551538
p.span,
552539
None,
553-
/* is_foreign_item */ false,
554540
kind,
555541
InheritDeprecation::No,
556542
InheritConstStability::No,
@@ -712,7 +698,6 @@ fn stability_index(tcx: TyCtxt<'_>, (): ()) -> Index {
712698
CRATE_DEF_ID,
713699
tcx.hir().span(CRATE_HIR_ID),
714700
None,
715-
/* is_foreign_item */ false,
716701
AnnotationKind::Required,
717702
InheritDeprecation::Yes,
718703
InheritConstStability::No,

tests/rustdoc/const-intrinsic.rs

+17-11
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
1-
#![feature(intrinsics)]
1+
#![feature(intrinsics, rustc_attrs)]
22
#![feature(staged_api)]
33

44
#![crate_name = "foo"]
55
#![stable(since="1.0.0", feature="rust1")]
66

7-
extern "rust-intrinsic" {
8-
//@ has 'foo/fn.transmute.html'
9-
//@ has - '//pre[@class="rust item-decl"]' 'pub const unsafe extern "rust-intrinsic" fn transmute<T, U>(_: T) -> U'
10-
#[stable(since="1.0.0", feature="rust1")]
11-
#[rustc_const_stable(feature = "const_transmute", since = "1.56.0")]
12-
pub fn transmute<T, U>(_: T) -> U;
7+
//@ has 'foo/fn.transmute.html'
8+
//@ has - '//pre[@class="rust item-decl"]' 'pub const unsafe fn transmute<T, U>(_: T) -> U'
9+
#[stable(since="1.0.0", feature="rust1")]
10+
#[rustc_const_stable(feature = "const_transmute", since = "1.56.0")]
11+
#[rustc_intrinsic]
12+
#[rustc_intrinsic_must_be_overridden]
13+
pub const unsafe fn transmute<T, U>(_: T) -> U {
14+
loop {}
15+
}
1316

14-
//@ has 'foo/fn.unreachable.html'
15-
//@ has - '//pre[@class="rust item-decl"]' 'pub unsafe extern "rust-intrinsic" fn unreachable() -> !'
16-
#[stable(since="1.0.0", feature="rust1")]
17-
pub fn unreachable() -> !;
17+
//@ has 'foo/fn.unreachable.html'
18+
//@ has - '//pre[@class="rust item-decl"]' 'pub unsafe fn unreachable() -> !'
19+
#[stable(since="1.0.0", feature="rust1")]
20+
#[rustc_intrinsic]
21+
#[rustc_intrinsic_must_be_overridden]
22+
pub unsafe fn unreachable() -> ! {
23+
loop {}
1824
}
1925

2026
extern "C" {

tests/ui/closures/coerce-unsafe-to-closure.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
error[E0277]: expected a `FnOnce(&str)` closure, found `unsafe extern "rust-intrinsic" fn(_) -> _ {std::intrinsics::transmute::<_, _>}`
1+
error[E0277]: expected a `FnOnce(&str)` closure, found `unsafe fn(_) -> _ {std::intrinsics::transmute::<_, _>}`
22
--> $DIR/coerce-unsafe-to-closure.rs:2:44
33
|
44
LL | let x: Option<&[u8]> = Some("foo").map(std::mem::transmute);
55
| --- ^^^^^^^^^^^^^^^^^^^ call the function in a closure: `|| unsafe { /* code */ }`
66
| |
77
| required by a bound introduced by this call
88
|
9-
= help: the trait `FnOnce(&str)` is not implemented for fn item `unsafe extern "rust-intrinsic" fn(_) -> _ {std::intrinsics::transmute::<_, _>}`
9+
= help: the trait `FnOnce(&str)` is not implemented for fn item `unsafe fn(_) -> _ {std::intrinsics::transmute::<_, _>}`
1010
= note: unsafe function cannot be called generically without an unsafe block
1111
note: required by a bound in `Option::<T>::map`
1212
--> $SRC_DIR/core/src/option.rs:LL:COL
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,11 @@
11
#![feature(staged_api, rustc_attrs, intrinsics)]
22
#![stable(since="1.0.0", feature = "stable")]
33

4-
#[stable(since="1.0.0", feature = "stable")]
5-
pub mod old_way {
6-
extern "rust-intrinsic" {
7-
#[unstable(feature = "unstable", issue = "42")]
8-
pub fn size_of_val<T>(x: *const T) -> usize;
9-
10-
#[unstable(feature = "unstable", issue = "42")]
11-
#[rustc_const_unstable(feature = "unstable", issue = "42")]
12-
pub fn min_align_of_val<T>(x: *const T) -> usize;
13-
}
14-
}
15-
16-
#[stable(since="1.0.0", feature = "stable")]
17-
pub mod new_way {
18-
#[unstable(feature = "unstable", issue = "42")]
19-
#[rustc_intrinsic]
20-
pub const unsafe fn size_of_val<T>(x: *const T) -> usize { 42 }
21-
22-
#[unstable(feature = "unstable", issue = "42")]
23-
#[rustc_const_unstable(feature = "unstable", issue = "42")]
24-
#[rustc_intrinsic]
25-
pub const unsafe fn min_align_of_val<T>(x: *const T) -> usize { 42 }
26-
}
4+
#[unstable(feature = "unstable", issue = "42")]
5+
#[rustc_intrinsic]
6+
pub const unsafe fn size_of_val<T>(x: *const T) -> usize { 42 }
7+
8+
#[unstable(feature = "unstable", issue = "42")]
9+
#[rustc_const_unstable(feature = "unstable", issue = "42")]
10+
#[rustc_intrinsic]
11+
pub const unsafe fn min_align_of_val<T>(x: *const T) -> usize { 42 }

tests/ui/consts/const-eval/simd/insert_extract.rs

+12-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//@ run-pass
22
#![feature(repr_simd)]
3-
#![feature(intrinsics)]
3+
#![feature(intrinsics, rustc_attrs)]
44
#![feature(staged_api)]
55
#![stable(feature = "foo", since = "1.3.37")]
66
#![allow(non_camel_case_types)]
@@ -10,14 +10,18 @@
1010
#[repr(simd)] struct u16x2([u16; 2]);
1111
#[repr(simd)] struct f32x4([f32; 4]);
1212

13-
extern "rust-intrinsic" {
14-
#[stable(feature = "foo", since = "1.3.37")]
15-
#[rustc_const_stable(feature = "foo", since = "1.3.37")]
16-
fn simd_insert<T, U>(x: T, idx: u32, val: U) -> T;
13+
#[stable(feature = "foo", since = "1.3.37")]
14+
#[rustc_const_stable(feature = "foo", since = "1.3.37")]
15+
#[rustc_intrinsic]
16+
const unsafe fn simd_insert<T, U>(_x: T, _idx: u32, _val: U) -> T {
17+
unimplemented!()
18+
}
1719

18-
#[stable(feature = "foo", since = "1.3.37")]
19-
#[rustc_const_stable(feature = "foo", since = "1.3.37")]
20-
fn simd_extract<T, U>(x: T, idx: u32) -> U;
20+
#[stable(feature = "foo", since = "1.3.37")]
21+
#[rustc_const_stable(feature = "foo", since = "1.3.37")]
22+
#[rustc_intrinsic]
23+
const unsafe fn simd_extract<T, U>(_x: T, _idx: u32) -> U {
24+
unimplemented!()
2125
}
2226

2327
fn main() {

tests/ui/consts/const-unstable-intrinsic.rs

+15-39
Original file line numberDiff line numberDiff line change
@@ -14,63 +14,39 @@ fn main() {
1414
const fn const_main() {
1515
let x = 42;
1616
unsafe {
17-
unstable_intrinsic::old_way::size_of_val(&x);
18-
//~^ERROR: unstable library feature `unstable`
19-
//~|ERROR: cannot call non-const intrinsic
20-
unstable_intrinsic::old_way::min_align_of_val(&x);
21-
//~^ERROR: unstable library feature `unstable`
22-
//~|ERROR: not yet stable as a const intrinsic
23-
unstable_intrinsic::new_way::size_of_val(&x);
17+
unstable_intrinsic::size_of_val(&x);
2418
//~^ERROR: unstable library feature `unstable`
2519
//~|ERROR: cannot be (indirectly) exposed to stable
26-
unstable_intrinsic::new_way::min_align_of_val(&x);
20+
unstable_intrinsic::min_align_of_val(&x);
2721
//~^ERROR: unstable library feature `unstable`
2822
//~|ERROR: not yet stable as a const intrinsic
2923

30-
old_way::size_of_val(&x);
31-
//~^ERROR: cannot call non-const intrinsic
32-
old_way::min_align_of_val(&x);
33-
//~^ERROR: cannot use `#[feature(local)]`
34-
new_way::size_of_val(&x);
24+
size_of_val(&x);
3525
//~^ERROR: cannot be (indirectly) exposed to stable
36-
new_way::min_align_of_val(&x);
26+
min_align_of_val(&x);
3727
//~^ERROR: cannot use `#[feature(local)]`
3828
}
3929
}
4030

41-
#[stable(since="1.0.0", feature = "stable")]
42-
pub mod old_way {
43-
extern "rust-intrinsic" {
44-
#[unstable(feature = "local", issue = "42")]
45-
pub fn size_of_val<T>(x: *const T) -> usize;
46-
47-
#[unstable(feature = "local", issue = "42")]
48-
#[rustc_const_unstable(feature = "local", issue = "42")]
49-
pub fn min_align_of_val<T>(x: *const T) -> usize;
50-
}
51-
}
52-
53-
#[stable(since="1.0.0", feature = "stable")]
54-
pub mod new_way {
55-
#[unstable(feature = "local", issue = "42")]
56-
#[rustc_intrinsic]
57-
pub const unsafe fn size_of_val<T>(x: *const T) -> usize { 42 }
31+
#[unstable(feature = "local", issue = "42")]
32+
#[rustc_intrinsic]
33+
pub const unsafe fn size_of_val<T>(x: *const T) -> usize { 42 }
5834

59-
#[unstable(feature = "local", issue = "42")]
60-
#[rustc_const_unstable(feature = "local", issue = "42")]
61-
#[rustc_intrinsic]
62-
pub const unsafe fn min_align_of_val<T>(x: *const T) -> usize { 42 }
63-
}
35+
#[unstable(feature = "local", issue = "42")]
36+
#[rustc_const_unstable(feature = "local", issue = "42")]
37+
#[rustc_intrinsic]
38+
pub const unsafe fn min_align_of_val<T>(x: *const T) -> usize { 42 }
6439

6540
#[stable(feature = "rust1", since = "1.0.0")]
6641
#[rustc_const_stable(feature = "const_intrinsic_copy", since = "1.63.0")]
6742
#[inline]
6843
pub const unsafe fn copy<T>(src: *const T, dst: *mut T, count: usize) {
6944
// Const stability attributes are not inherited from parent items.
70-
extern "rust-intrinsic" {
71-
fn copy<T>(src: *const T, dst: *mut T, count: usize);
45+
#[rustc_intrinsic]
46+
const unsafe fn copy<T>(src: *const T, dst: *mut T, count: usize) {
47+
unimplemented!()
7248
}
7349

7450
unsafe { copy(src, dst, count) }
75-
//~^ ERROR cannot call non-const intrinsic
51+
//~^ ERROR cannot be (indirectly) exposed to stable
7652
}

0 commit comments

Comments
 (0)