Skip to content

Commit 531505f

Browse files
committed
Check signature of intrinsics with fallback bodies
1 parent 0dac617 commit 531505f

File tree

9 files changed

+92
-38
lines changed

9 files changed

+92
-38
lines changed

compiler/rustc_hir_analysis/src/check/check.rs

+19-2
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,18 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) {
472472
DefKind::Enum => {
473473
check_enum(tcx, def_id);
474474
}
475-
DefKind::Fn => {} // entirely within check_item_body
475+
DefKind::Fn => {
476+
if let Some(name) = tcx.intrinsic(def_id) {
477+
intrinsic::check_intrinsic_type(
478+
tcx,
479+
def_id,
480+
tcx.def_ident_span(def_id).unwrap(),
481+
name,
482+
Abi::Rust,
483+
)
484+
}
485+
// Everything else is checked entirely within check_item_body
486+
}
476487
DefKind::Impl { of_trait } => {
477488
if of_trait && let Some(impl_trait_ref) = tcx.impl_trait_ref(def_id) {
478489
check_impl_items_against_trait(tcx, def_id, impl_trait_ref.instantiate_identity());
@@ -533,7 +544,13 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) {
533544
match abi {
534545
Abi::RustIntrinsic => {
535546
for item in items {
536-
intrinsic::check_intrinsic_type(tcx, item.id.owner_id.def_id, item.span);
547+
intrinsic::check_intrinsic_type(
548+
tcx,
549+
item.id.owner_id.def_id,
550+
item.span,
551+
item.ident.name,
552+
abi,
553+
);
537554
}
538555
}
539556

compiler/rustc_hir_analysis/src/check/intrinsic.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ fn equate_intrinsic_type<'tcx>(
2626
sig: ty::PolyFnSig<'tcx>,
2727
) {
2828
let (own_counts, span) = match tcx.hir_node_by_def_id(def_id) {
29-
hir::Node::ForeignItem(hir::ForeignItem {
29+
hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn(_, generics, _), .. })
30+
| hir::Node::ForeignItem(hir::ForeignItem {
3031
kind: hir::ForeignItemKind::Fn(.., generics),
3132
..
3233
}) => {
@@ -136,7 +137,13 @@ pub fn intrinsic_operation_unsafety(tcx: TyCtxt<'_>, intrinsic_id: LocalDefId) -
136137

137138
/// Remember to add all intrinsics here, in `compiler/rustc_codegen_llvm/src/intrinsic.rs`,
138139
/// and in `library/core/src/intrinsics.rs`.
139-
pub fn check_intrinsic_type(tcx: TyCtxt<'_>, intrinsic_id: LocalDefId, span: Span) {
140+
pub fn check_intrinsic_type(
141+
tcx: TyCtxt<'_>,
142+
intrinsic_id: LocalDefId,
143+
span: Span,
144+
intrinsic_name: Symbol,
145+
abi: Abi,
146+
) {
140147
let generics = tcx.generics_of(intrinsic_id);
141148
let param = |n| {
142149
if let Some(&ty::GenericParamDef {
@@ -148,7 +155,6 @@ pub fn check_intrinsic_type(tcx: TyCtxt<'_>, intrinsic_id: LocalDefId, span: Spa
148155
Ty::new_error_with_message(tcx, span, "expected param")
149156
}
150157
};
151-
let intrinsic_name = tcx.item_name(intrinsic_id.into());
152158
let name_str = intrinsic_name.as_str();
153159

154160
let bound_vars = tcx.mk_bound_variable_kinds(&[
@@ -479,15 +485,15 @@ pub fn check_intrinsic_type(tcx: TyCtxt<'_>, intrinsic_id: LocalDefId, span: Spa
479485

480486
sym::black_box => (1, 0, vec![param(0)], param(0)),
481487

482-
sym::is_val_statically_known => (1, 0, vec![param(0)], tcx.types.bool),
488+
sym::is_val_statically_known => (1, 1, vec![param(0)], tcx.types.bool),
483489

484490
sym::const_eval_select => (4, 0, vec![param(0), param(1), param(2)], param(3)),
485491

486492
sym::vtable_size | sym::vtable_align => {
487493
(0, 0, vec![Ty::new_imm_ptr(tcx, Ty::new_unit(tcx))], tcx.types.usize)
488494
}
489495

490-
sym::debug_assertions => (0, 0, Vec::new(), tcx.types.bool),
496+
sym::debug_assertions => (0, 1, Vec::new(), tcx.types.bool),
491497

492498
other => {
493499
tcx.dcx().emit_err(UnrecognizedIntrinsicFunction { span, name: other });
@@ -496,7 +502,7 @@ pub fn check_intrinsic_type(tcx: TyCtxt<'_>, intrinsic_id: LocalDefId, span: Spa
496502
};
497503
(n_tps, 0, n_cts, inputs, output, unsafety)
498504
};
499-
let sig = tcx.mk_fn_sig(inputs, output, false, unsafety, Abi::RustIntrinsic);
505+
let sig = tcx.mk_fn_sig(inputs, output, false, unsafety, abi);
500506
let sig = ty::Binder::bind_with_vars(sig, bound_vars);
501507
equate_intrinsic_type(tcx, span, intrinsic_id, n_tps, n_lts, n_cts, sig)
502508
}

library/core/src/intrinsics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2590,7 +2590,7 @@ pub const unsafe fn is_val_statically_known<T: Copy>(_arg: T) -> bool {
25902590
#[rustc_const_unstable(feature = "delayed_debug_assertions", issue = "none")]
25912591
#[unstable(feature = "core_intrinsics", issue = "none")]
25922592
#[cfg_attr(not(bootstrap), rustc_intrinsic)]
2593-
pub(crate) const fn debug_assertions() -> bool {
2593+
pub(crate) const unsafe fn debug_assertions() -> bool {
25942594
cfg!(debug_assertions)
25952595
}
25962596

tests/ui/feature-gates/feature-gate-abi.rs

+2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ trait Tuple { }
1414
// Functions
1515
extern "rust-intrinsic" fn f1() {} //~ ERROR intrinsics are subject to change
1616
//~^ ERROR intrinsic must be in
17+
//~| ERROR unrecognized intrinsic function: `f1`
1718
extern "platform-intrinsic" fn f2() {} //~ ERROR platform intrinsics are experimental
1819
//~^ ERROR intrinsic must be in
20+
//~| ERROR unrecognized intrinsic function: `f2`
1921
extern "rust-call" fn f4(_: ()) {} //~ ERROR rust-call ABI is subject to change
2022

2123
// Methods in trait definition

tests/ui/feature-gates/feature-gate-abi.stderr

+40-27
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LL | extern "rust-intrinsic" fn f1() {}
88
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
99

1010
error[E0658]: platform intrinsics are experimental and possibly buggy
11-
--> $DIR/feature-gate-abi.rs:17:8
11+
--> $DIR/feature-gate-abi.rs:18:8
1212
|
1313
LL | extern "platform-intrinsic" fn f2() {}
1414
| ^^^^^^^^^^^^^^^^^^^^
@@ -18,7 +18,7 @@ LL | extern "platform-intrinsic" fn f2() {}
1818
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
1919

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

3030
error[E0658]: intrinsics are subject to change
31-
--> $DIR/feature-gate-abi.rs:23:12
31+
--> $DIR/feature-gate-abi.rs:25:12
3232
|
3333
LL | extern "rust-intrinsic" fn m1();
3434
| ^^^^^^^^^^^^^^^^
@@ -37,7 +37,7 @@ LL | extern "rust-intrinsic" fn m1();
3737
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
3838

3939
error[E0658]: platform intrinsics are experimental and possibly buggy
40-
--> $DIR/feature-gate-abi.rs:25:12
40+
--> $DIR/feature-gate-abi.rs:27:12
4141
|
4242
LL | extern "platform-intrinsic" fn m2();
4343
| ^^^^^^^^^^^^^^^^^^^^
@@ -47,7 +47,7 @@ LL | extern "platform-intrinsic" fn m2();
4747
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
4848

4949
error[E0658]: rust-call ABI is subject to change
50-
--> $DIR/feature-gate-abi.rs:27:12
50+
--> $DIR/feature-gate-abi.rs:29:12
5151
|
5252
LL | extern "rust-call" fn m4(_: ());
5353
| ^^^^^^^^^^^
@@ -57,7 +57,7 @@ LL | extern "rust-call" fn m4(_: ());
5757
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
5858

5959
error[E0658]: rust-call ABI is subject to change
60-
--> $DIR/feature-gate-abi.rs:29:12
60+
--> $DIR/feature-gate-abi.rs:31:12
6161
|
6262
LL | extern "rust-call" fn dm4(_: ()) {}
6363
| ^^^^^^^^^^^
@@ -67,7 +67,7 @@ LL | extern "rust-call" fn dm4(_: ()) {}
6767
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
6868

6969
error[E0658]: intrinsics are subject to change
70-
--> $DIR/feature-gate-abi.rs:36:12
70+
--> $DIR/feature-gate-abi.rs:38:12
7171
|
7272
LL | extern "rust-intrinsic" fn m1() {}
7373
| ^^^^^^^^^^^^^^^^
@@ -76,7 +76,7 @@ LL | extern "rust-intrinsic" fn m1() {}
7676
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
7777

7878
error[E0658]: platform intrinsics are experimental and possibly buggy
79-
--> $DIR/feature-gate-abi.rs:38:12
79+
--> $DIR/feature-gate-abi.rs:40:12
8080
|
8181
LL | extern "platform-intrinsic" fn m2() {}
8282
| ^^^^^^^^^^^^^^^^^^^^
@@ -86,7 +86,7 @@ LL | extern "platform-intrinsic" fn m2() {}
8686
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
8787

8888
error[E0658]: rust-call ABI is subject to change
89-
--> $DIR/feature-gate-abi.rs:40:12
89+
--> $DIR/feature-gate-abi.rs:42:12
9090
|
9191
LL | extern "rust-call" fn m4(_: ()) {}
9292
| ^^^^^^^^^^^
@@ -96,7 +96,7 @@ LL | extern "rust-call" fn m4(_: ()) {}
9696
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
9797

9898
error[E0658]: intrinsics are subject to change
99-
--> $DIR/feature-gate-abi.rs:45:12
99+
--> $DIR/feature-gate-abi.rs:47:12
100100
|
101101
LL | extern "rust-intrinsic" fn im1() {}
102102
| ^^^^^^^^^^^^^^^^
@@ -105,7 +105,7 @@ LL | extern "rust-intrinsic" fn im1() {}
105105
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
106106

107107
error[E0658]: platform intrinsics are experimental and possibly buggy
108-
--> $DIR/feature-gate-abi.rs:47:12
108+
--> $DIR/feature-gate-abi.rs:49:12
109109
|
110110
LL | extern "platform-intrinsic" fn im2() {}
111111
| ^^^^^^^^^^^^^^^^^^^^
@@ -115,7 +115,7 @@ LL | extern "platform-intrinsic" fn im2() {}
115115
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
116116

117117
error[E0658]: rust-call ABI is subject to change
118-
--> $DIR/feature-gate-abi.rs:49:12
118+
--> $DIR/feature-gate-abi.rs:51:12
119119
|
120120
LL | extern "rust-call" fn im4(_: ()) {}
121121
| ^^^^^^^^^^^
@@ -125,7 +125,7 @@ LL | extern "rust-call" fn im4(_: ()) {}
125125
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
126126

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

136136
error[E0658]: platform intrinsics are experimental and possibly buggy
137-
--> $DIR/feature-gate-abi.rs:54:18
137+
--> $DIR/feature-gate-abi.rs:56:18
138138
|
139139
LL | type A2 = extern "platform-intrinsic" fn();
140140
| ^^^^^^^^^^^^^^^^^^^^
@@ -144,7 +144,7 @@ LL | type A2 = extern "platform-intrinsic" fn();
144144
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
145145

146146
error[E0658]: rust-call ABI is subject to change
147-
--> $DIR/feature-gate-abi.rs:55:18
147+
--> $DIR/feature-gate-abi.rs:57:18
148148
|
149149
LL | type A4 = extern "rust-call" fn(_: ());
150150
| ^^^^^^^^^^^
@@ -154,7 +154,7 @@ LL | type A4 = extern "rust-call" fn(_: ());
154154
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
155155

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

165165
error[E0658]: platform intrinsics are experimental and possibly buggy
166-
--> $DIR/feature-gate-abi.rs:59:8
166+
--> $DIR/feature-gate-abi.rs:61:8
167167
|
168168
LL | extern "platform-intrinsic" {}
169169
| ^^^^^^^^^^^^^^^^^^^^
@@ -173,7 +173,7 @@ LL | extern "platform-intrinsic" {}
173173
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
174174

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

185+
error[E0093]: unrecognized intrinsic function: `f1`
186+
--> $DIR/feature-gate-abi.rs:15:28
187+
|
188+
LL | extern "rust-intrinsic" fn f1() {}
189+
| ^^ unrecognized intrinsic
190+
191+
error[E0093]: unrecognized intrinsic function: `f2`
192+
--> $DIR/feature-gate-abi.rs:18:32
193+
|
194+
LL | extern "platform-intrinsic" fn f2() {}
195+
| ^^ unrecognized intrinsic
196+
185197
error: intrinsic must be in `extern "rust-intrinsic" { ... }` block
186-
--> $DIR/feature-gate-abi.rs:23:32
198+
--> $DIR/feature-gate-abi.rs:25:32
187199
|
188200
LL | extern "rust-intrinsic" fn m1();
189201
| ^^
190202

191203
error: intrinsic must be in `extern "rust-intrinsic" { ... }` block
192-
--> $DIR/feature-gate-abi.rs:25:36
204+
--> $DIR/feature-gate-abi.rs:27:36
193205
|
194206
LL | extern "platform-intrinsic" fn m2();
195207
| ^^
@@ -201,35 +213,36 @@ LL | extern "rust-intrinsic" fn f1() {}
201213
| ^^
202214

203215
error: intrinsic must be in `extern "rust-intrinsic" { ... }` block
204-
--> $DIR/feature-gate-abi.rs:17:37
216+
--> $DIR/feature-gate-abi.rs:18:37
205217
|
206218
LL | extern "platform-intrinsic" fn f2() {}
207219
| ^^
208220

209221
error: intrinsic must be in `extern "rust-intrinsic" { ... }` block
210-
--> $DIR/feature-gate-abi.rs:36:37
222+
--> $DIR/feature-gate-abi.rs:38:37
211223
|
212224
LL | extern "rust-intrinsic" fn m1() {}
213225
| ^^
214226

215227
error: intrinsic must be in `extern "rust-intrinsic" { ... }` block
216-
--> $DIR/feature-gate-abi.rs:38:41
228+
--> $DIR/feature-gate-abi.rs:40:41
217229
|
218230
LL | extern "platform-intrinsic" fn m2() {}
219231
| ^^
220232

221233
error: intrinsic must be in `extern "rust-intrinsic" { ... }` block
222-
--> $DIR/feature-gate-abi.rs:45:38
234+
--> $DIR/feature-gate-abi.rs:47:38
223235
|
224236
LL | extern "rust-intrinsic" fn im1() {}
225237
| ^^
226238

227239
error: intrinsic must be in `extern "rust-intrinsic" { ... }` block
228-
--> $DIR/feature-gate-abi.rs:47:42
240+
--> $DIR/feature-gate-abi.rs:49:42
229241
|
230242
LL | extern "platform-intrinsic" fn im2() {}
231243
| ^^
232244

233-
error: aborting due to 27 previous errors
245+
error: aborting due to 29 previous errors
234246

235-
For more information about this error, try `rustc --explain E0658`.
247+
Some errors have detailed explanations: E0093, E0658.
248+
For more information about an error, try `rustc --explain E0093`.

tests/ui/feature-gates/feature-gate-intrinsics.rs

+1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ extern "rust-intrinsic" { //~ ERROR intrinsics are subject to change
44

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

89
fn main() {}

tests/ui/feature-gates/feature-gate-intrinsics.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,19 @@ error[E0093]: unrecognized intrinsic function: `bar`
2222
LL | fn bar();
2323
| ^^^^^^^^^ unrecognized intrinsic
2424

25+
error[E0093]: unrecognized intrinsic function: `baz`
26+
--> $DIR/feature-gate-intrinsics.rs:5:28
27+
|
28+
LL | extern "rust-intrinsic" fn baz() {}
29+
| ^^^ unrecognized intrinsic
30+
2531
error: intrinsic must be in `extern "rust-intrinsic" { ... }` block
2632
--> $DIR/feature-gate-intrinsics.rs:5:34
2733
|
2834
LL | extern "rust-intrinsic" fn baz() {}
2935
| ^^
3036

31-
error: aborting due to 4 previous errors
37+
error: aborting due to 5 previous errors
3238

3339
Some errors have detailed explanations: E0093, E0658.
3440
For more information about an error, try `rustc --explain E0093`.

tests/ui/intrinsics-always-extern.rs

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ impl Foo for () {
1010
}
1111

1212
extern "rust-intrinsic" fn hello() {//~ ERROR intrinsic must
13+
//~^ ERROR unrecognized intrinsic function: `hello`
1314
}
1415

1516
fn main() {

0 commit comments

Comments
 (0)