Skip to content

Don't even parse an intrinsic unless the feature gate is enabled #123603

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 16, 2024
Merged
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
11 changes: 3 additions & 8 deletions compiler/rustc_hir_typeck/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ use rustc_span::source_map::Spanned;
use rustc_span::symbol::{kw, sym, Ident, Symbol};
use rustc_span::Span;
use rustc_target::abi::{FieldIdx, FIRST_VARIANT};
use rustc_target::spec::abi::Abi::RustIntrinsic;
use rustc_trait_selection::infer::InferCtxtExt;
use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt;
use rustc_trait_selection::traits::ObligationCtxt;
Expand Down Expand Up @@ -541,16 +540,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
if let ty::FnDef(did, _) = *ty.kind() {
let fn_sig = ty.fn_sig(tcx);

if tcx.fn_sig(did).skip_binder().abi() == RustIntrinsic
&& tcx.item_name(did) == sym::transmute
{
if tcx.is_intrinsic(did, sym::transmute) {
let Some(from) = fn_sig.inputs().skip_binder().get(0) else {
let e = self.dcx().span_delayed_bug(
span_bug!(
tcx.def_span(did),
"intrinsic fn `transmute` defined with no parameters",
"intrinsic fn `transmute` defined with no parameters"
);
self.set_tainted_by_errors(e);
return Ty::new_error(tcx, e);
};
let to = fn_sig.output().skip_binder();
// We defer the transmute to the end of typeck, once all inference vars have
Expand Down
11 changes: 8 additions & 3 deletions compiler/rustc_middle/src/ty/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1679,10 +1679,15 @@ pub fn is_doc_notable_trait(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
.any(|items| items.iter().any(|item| item.has_name(sym::notable_trait)))
}

/// Determines whether an item is an intrinsic (which may be via Abi or via the `rustc_intrinsic` attribute)
/// Determines whether an item is an intrinsic (which may be via Abi or via the `rustc_intrinsic` attribute).
///
/// We double check the feature gate here because whether a function may be defined as an intrinsic causes
/// the compiler to make some assumptions about its shape; if the user doesn't use a feature gate, they may
/// cause an ICE that we otherwise may want to prevent.
pub fn intrinsic_raw(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<ty::IntrinsicDef> {
if matches!(tcx.fn_sig(def_id).skip_binder().abi(), Abi::RustIntrinsic)
|| tcx.has_attr(def_id, sym::rustc_intrinsic)
if (matches!(tcx.fn_sig(def_id).skip_binder().abi(), Abi::RustIntrinsic)
&& tcx.features().intrinsics)
|| (tcx.has_attr(def_id, sym::rustc_intrinsic) && tcx.features().rustc_attrs)
{
Some(ty::IntrinsicDef {
name: tcx.item_name(def_id.into()),
Expand Down
2 changes: 0 additions & 2 deletions tests/ui/feature-gates/feature-gate-abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ trait Tuple { }
// Functions
extern "rust-intrinsic" fn f1() {} //~ ERROR intrinsics are subject to change
//~^ ERROR intrinsic must be in
//~| ERROR unrecognized intrinsic function: `f1`
extern "rust-intrinsic" fn f2() {} //~ ERROR intrinsics are subject to change
//~^ ERROR intrinsic must be in
//~| ERROR unrecognized intrinsic function: `f2`
extern "rust-call" fn f4(_: ()) {} //~ ERROR rust-call ABI is subject to change

// Methods in trait definition
Expand Down
71 changes: 27 additions & 44 deletions tests/ui/feature-gates/feature-gate-abi.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ LL | extern "rust-intrinsic" fn f1() {}
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error[E0658]: intrinsics are subject to change
--> $DIR/feature-gate-abi.rs:17:8
--> $DIR/feature-gate-abi.rs:16:8
|
LL | extern "rust-intrinsic" fn f2() {}
| ^^^^^^^^^^^^^^^^
Expand All @@ -17,7 +17,7 @@ LL | extern "rust-intrinsic" fn f2() {}
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error[E0658]: rust-call ABI is subject to change
--> $DIR/feature-gate-abi.rs:20:8
--> $DIR/feature-gate-abi.rs:18:8
|
LL | extern "rust-call" fn f4(_: ()) {}
| ^^^^^^^^^^^
Expand All @@ -27,7 +27,7 @@ LL | extern "rust-call" fn f4(_: ()) {}
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error[E0658]: intrinsics are subject to change
--> $DIR/feature-gate-abi.rs:24:12
--> $DIR/feature-gate-abi.rs:22:12
|
LL | extern "rust-intrinsic" fn m1();
| ^^^^^^^^^^^^^^^^
Expand All @@ -36,7 +36,7 @@ LL | extern "rust-intrinsic" fn m1();
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error[E0658]: intrinsics are subject to change
--> $DIR/feature-gate-abi.rs:26:12
--> $DIR/feature-gate-abi.rs:24:12
|
LL | extern "rust-intrinsic" fn m2();
| ^^^^^^^^^^^^^^^^
Expand All @@ -45,7 +45,7 @@ LL | extern "rust-intrinsic" fn m2();
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error[E0658]: rust-call ABI is subject to change
--> $DIR/feature-gate-abi.rs:28:12
--> $DIR/feature-gate-abi.rs:26:12
|
LL | extern "rust-call" fn m4(_: ());
| ^^^^^^^^^^^
Expand All @@ -55,7 +55,7 @@ LL | extern "rust-call" fn m4(_: ());
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error[E0658]: rust-call ABI is subject to change
--> $DIR/feature-gate-abi.rs:30:12
--> $DIR/feature-gate-abi.rs:28:12
|
LL | extern "rust-call" fn dm4(_: ()) {}
| ^^^^^^^^^^^
Expand All @@ -65,7 +65,7 @@ LL | extern "rust-call" fn dm4(_: ()) {}
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error[E0658]: intrinsics are subject to change
--> $DIR/feature-gate-abi.rs:37:12
--> $DIR/feature-gate-abi.rs:35:12
|
LL | extern "rust-intrinsic" fn m1() {}
| ^^^^^^^^^^^^^^^^
Expand All @@ -74,7 +74,7 @@ LL | extern "rust-intrinsic" fn m1() {}
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error[E0658]: intrinsics are subject to change
--> $DIR/feature-gate-abi.rs:39:12
--> $DIR/feature-gate-abi.rs:37:12
|
LL | extern "rust-intrinsic" fn m2() {}
| ^^^^^^^^^^^^^^^^
Expand All @@ -83,7 +83,7 @@ LL | extern "rust-intrinsic" fn m2() {}
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error[E0658]: rust-call ABI is subject to change
--> $DIR/feature-gate-abi.rs:41:12
--> $DIR/feature-gate-abi.rs:39:12
|
LL | extern "rust-call" fn m4(_: ()) {}
| ^^^^^^^^^^^
Expand All @@ -93,7 +93,7 @@ LL | extern "rust-call" fn m4(_: ()) {}
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error[E0658]: intrinsics are subject to change
--> $DIR/feature-gate-abi.rs:46:12
--> $DIR/feature-gate-abi.rs:44:12
|
LL | extern "rust-intrinsic" fn im1() {}
| ^^^^^^^^^^^^^^^^
Expand All @@ -102,7 +102,7 @@ LL | extern "rust-intrinsic" fn im1() {}
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error[E0658]: intrinsics are subject to change
--> $DIR/feature-gate-abi.rs:48:12
--> $DIR/feature-gate-abi.rs:46:12
|
LL | extern "rust-intrinsic" fn im2() {}
| ^^^^^^^^^^^^^^^^
Expand All @@ -111,7 +111,7 @@ LL | extern "rust-intrinsic" fn im2() {}
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error[E0658]: rust-call ABI is subject to change
--> $DIR/feature-gate-abi.rs:50:12
--> $DIR/feature-gate-abi.rs:48:12
|
LL | extern "rust-call" fn im4(_: ()) {}
| ^^^^^^^^^^^
Expand All @@ -121,7 +121,7 @@ LL | extern "rust-call" fn im4(_: ()) {}
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error[E0658]: intrinsics are subject to change
--> $DIR/feature-gate-abi.rs:54:18
--> $DIR/feature-gate-abi.rs:52:18
|
LL | type A1 = extern "rust-intrinsic" fn();
| ^^^^^^^^^^^^^^^^
Expand All @@ -130,7 +130,7 @@ LL | type A1 = extern "rust-intrinsic" fn();
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error[E0658]: intrinsics are subject to change
--> $DIR/feature-gate-abi.rs:55:18
--> $DIR/feature-gate-abi.rs:53:18
|
LL | type A2 = extern "rust-intrinsic" fn();
| ^^^^^^^^^^^^^^^^
Expand All @@ -139,7 +139,7 @@ LL | type A2 = extern "rust-intrinsic" fn();
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error[E0658]: rust-call ABI is subject to change
--> $DIR/feature-gate-abi.rs:56:18
--> $DIR/feature-gate-abi.rs:54:18
|
LL | type A4 = extern "rust-call" fn(_: ());
| ^^^^^^^^^^^
Expand All @@ -149,7 +149,7 @@ LL | type A4 = extern "rust-call" fn(_: ());
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error[E0658]: intrinsics are subject to change
--> $DIR/feature-gate-abi.rs:59:8
--> $DIR/feature-gate-abi.rs:57:8
|
LL | extern "rust-intrinsic" {}
| ^^^^^^^^^^^^^^^^
Expand All @@ -158,7 +158,7 @@ LL | extern "rust-intrinsic" {}
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error[E0658]: intrinsics are subject to change
--> $DIR/feature-gate-abi.rs:60:8
--> $DIR/feature-gate-abi.rs:58:8
|
LL | extern "rust-intrinsic" {}
| ^^^^^^^^^^^^^^^^
Expand All @@ -167,7 +167,7 @@ LL | extern "rust-intrinsic" {}
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error[E0658]: rust-call ABI is subject to change
--> $DIR/feature-gate-abi.rs:61:8
--> $DIR/feature-gate-abi.rs:59:8
|
LL | extern "rust-call" {}
| ^^^^^^^^^^^
Expand All @@ -176,30 +176,14 @@ LL | extern "rust-call" {}
= help: add `#![feature(unboxed_closures)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error[E0093]: unrecognized intrinsic function: `f1`
--> $DIR/feature-gate-abi.rs:14:28
|
LL | extern "rust-intrinsic" fn f1() {}
| ^^ unrecognized intrinsic
|
= help: if you're adding an intrinsic, be sure to update `check_intrinsic_type`

error[E0093]: unrecognized intrinsic function: `f2`
--> $DIR/feature-gate-abi.rs:17:28
|
LL | extern "rust-intrinsic" fn f2() {}
| ^^ unrecognized intrinsic
|
= help: if you're adding an intrinsic, be sure to update `check_intrinsic_type`

error: intrinsic must be in `extern "rust-intrinsic" { ... }` block
--> $DIR/feature-gate-abi.rs:24:32
--> $DIR/feature-gate-abi.rs:22:32
|
LL | extern "rust-intrinsic" fn m1();
| ^^

error: intrinsic must be in `extern "rust-intrinsic" { ... }` block
--> $DIR/feature-gate-abi.rs:26:32
--> $DIR/feature-gate-abi.rs:24:32
|
LL | extern "rust-intrinsic" fn m2();
| ^^
Expand All @@ -211,36 +195,35 @@ LL | extern "rust-intrinsic" fn f1() {}
| ^^

error: intrinsic must be in `extern "rust-intrinsic" { ... }` block
--> $DIR/feature-gate-abi.rs:17:33
--> $DIR/feature-gate-abi.rs:16:33
|
LL | extern "rust-intrinsic" fn f2() {}
| ^^

error: intrinsic must be in `extern "rust-intrinsic" { ... }` block
--> $DIR/feature-gate-abi.rs:37:37
--> $DIR/feature-gate-abi.rs:35:37
|
LL | extern "rust-intrinsic" fn m1() {}
| ^^

error: intrinsic must be in `extern "rust-intrinsic" { ... }` block
--> $DIR/feature-gate-abi.rs:39:37
--> $DIR/feature-gate-abi.rs:37:37
|
LL | extern "rust-intrinsic" fn m2() {}
| ^^

error: intrinsic must be in `extern "rust-intrinsic" { ... }` block
--> $DIR/feature-gate-abi.rs:46:38
--> $DIR/feature-gate-abi.rs:44:38
|
LL | extern "rust-intrinsic" fn im1() {}
| ^^

error: intrinsic must be in `extern "rust-intrinsic" { ... }` block
--> $DIR/feature-gate-abi.rs:48:38
--> $DIR/feature-gate-abi.rs:46:38
|
LL | extern "rust-intrinsic" fn im2() {}
| ^^

error: aborting due to 29 previous errors
error: aborting due to 27 previous errors

Some errors have detailed explanations: E0093, E0658.
For more information about an error, try `rustc --explain E0093`.
For more information about this error, try `rustc --explain E0658`.
1 change: 0 additions & 1 deletion tests/ui/feature-gates/feature-gate-intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ extern "rust-intrinsic" { //~ ERROR intrinsics are subject to change

extern "rust-intrinsic" fn baz() {} //~ ERROR intrinsics are subject to change
//~^ ERROR intrinsic must be in
//~| ERROR unrecognized intrinsic function: `baz`

fn main() {}
10 changes: 1 addition & 9 deletions tests/ui/feature-gates/feature-gate-intrinsics.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,13 @@ LL | fn bar();
|
= help: if you're adding an intrinsic, be sure to update `check_intrinsic_type`

error[E0093]: unrecognized intrinsic function: `baz`
--> $DIR/feature-gate-intrinsics.rs:5:28
|
LL | extern "rust-intrinsic" fn baz() {}
| ^^^ unrecognized intrinsic
|
= help: if you're adding an intrinsic, be sure to update `check_intrinsic_type`

error: intrinsic must be in `extern "rust-intrinsic" { ... }` block
--> $DIR/feature-gate-intrinsics.rs:5:34
|
LL | extern "rust-intrinsic" fn baz() {}
| ^^

error: aborting due to 5 previous errors
error: aborting due to 4 previous errors

Some errors have detailed explanations: E0093, E0658.
For more information about an error, try `rustc --explain E0093`.
7 changes: 7 additions & 0 deletions tests/ui/intrinsics/incorrect-read_via_copy-defn.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
fn main() {
read_via_copy();
}

extern "rust-intrinsic" fn read_via_copy() {}
//~^ ERROR intrinsics are subject to change
//~| ERROR intrinsic must be in `extern "rust-intrinsic" { ... }` block
18 changes: 18 additions & 0 deletions tests/ui/intrinsics/incorrect-read_via_copy-defn.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
error[E0658]: intrinsics are subject to change
--> $DIR/incorrect-read_via_copy-defn.rs:5:8
|
LL | extern "rust-intrinsic" fn read_via_copy() {}
| ^^^^^^^^^^^^^^^^
|
= help: add `#![feature(intrinsics)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error: intrinsic must be in `extern "rust-intrinsic" { ... }` block
--> $DIR/incorrect-read_via_copy-defn.rs:5:44
|
LL | extern "rust-intrinsic" fn read_via_copy() {}
| ^^

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0658`.
3 changes: 1 addition & 2 deletions tests/ui/intrinsics/incorrect-transmute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@ fn main() {
}

extern "rust-intrinsic" fn transmute() {}
//~^ ERROR intrinsic has wrong number of type parameters: found 0, expected 2
//~| ERROR intrinsics are subject to change
//~^ ERROR intrinsics are subject to change
//~| ERROR intrinsic must be in `extern "rust-intrinsic" { ... }` block
11 changes: 2 additions & 9 deletions tests/ui/intrinsics/incorrect-transmute.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,12 @@ LL | extern "rust-intrinsic" fn transmute() {}
= help: add `#![feature(intrinsics)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error[E0094]: intrinsic has wrong number of type parameters: found 0, expected 2
--> $DIR/incorrect-transmute.rs:5:37
|
LL | extern "rust-intrinsic" fn transmute() {}
| ^ expected 2 type parameters

error: intrinsic must be in `extern "rust-intrinsic" { ... }` block
--> $DIR/incorrect-transmute.rs:5:40
|
LL | extern "rust-intrinsic" fn transmute() {}
| ^^

error: aborting due to 3 previous errors
error: aborting due to 2 previous errors

Some errors have detailed explanations: E0094, E0658.
For more information about an error, try `rustc --explain E0094`.
For more information about this error, try `rustc --explain E0658`.
Loading