Skip to content

Commit ab4d16f

Browse files
authored
Rollup merge of #86148 - FabianWolff:issue-85855, r=varkor
Check the number of generic lifetime and const parameters of intrinsics This pull request fixes #85855. The current code for type checking intrinsics only checks the number of generic _type_ parameters, but does not check for an incorrect number of lifetime or const parameters, which can cause problems later on, such as the ICE in #85855, where the code thought that it was looking at a type parameter but found a lifetime parameter: ``` error: internal compiler error: compiler/rustc_middle/src/ty/generics.rs:188:18: expected type parameter, but found another generic parameter ``` The changes in this PR add checks for the number of lifetime and const parameters, expand the scope of `E0094` to also apply to these cases, and improve the error message by properly pluralizing the number of expected generic parameters.
2 parents ecef52a + fe93349 commit ab4d16f

File tree

5 files changed

+81
-30
lines changed

5 files changed

+81
-30
lines changed

compiler/rustc_error_codes/src/error_codes/E0094.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
An invalid number of type parameters was given to an intrinsic function.
1+
An invalid number of generic parameters was passed to an intrinsic function.
22

33
Erroneous code example:
44

compiler/rustc_typeck/src/check/intrinsic.rs

+35-26
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
44
use crate::errors::{
55
SimdShuffleMissingLength, UnrecognizedAtomicOperation, UnrecognizedIntrinsicFunction,
6-
WrongNumberOfTypeArgumentsToInstrinsic,
6+
WrongNumberOfGenericArgumentsToIntrinsic,
77
};
88
use crate::require_same_types;
99

10-
use rustc_errors::struct_span_err;
10+
use rustc_errors::{pluralize, struct_span_err};
1111
use rustc_hir as hir;
1212
use rustc_middle::traits::{ObligationCause, ObligationCauseCode};
1313
use rustc_middle::ty::subst::Subst;
@@ -21,36 +21,45 @@ fn equate_intrinsic_type<'tcx>(
2121
tcx: TyCtxt<'tcx>,
2222
it: &hir::ForeignItem<'_>,
2323
n_tps: usize,
24+
n_lts: usize,
2425
sig: ty::PolyFnSig<'tcx>,
2526
) {
26-
match it.kind {
27-
hir::ForeignItemKind::Fn(..) => {}
27+
let (own_counts, span) = match &it.kind {
28+
hir::ForeignItemKind::Fn(.., generics) => {
29+
let own_counts = tcx.generics_of(it.def_id.to_def_id()).own_counts();
30+
(own_counts, generics.span)
31+
}
2832
_ => {
2933
struct_span_err!(tcx.sess, it.span, E0622, "intrinsic must be a function")
3034
.span_label(it.span, "expected a function")
3135
.emit();
3236
return;
3337
}
34-
}
38+
};
3539

36-
let i_n_tps = tcx.generics_of(it.def_id).own_counts().types;
37-
if i_n_tps != n_tps {
38-
let span = match it.kind {
39-
hir::ForeignItemKind::Fn(_, _, ref generics) => generics.span,
40-
_ => bug!(),
41-
};
40+
let gen_count_ok = |found: usize, expected: usize, descr: &str| -> bool {
41+
if found != expected {
42+
tcx.sess.emit_err(WrongNumberOfGenericArgumentsToIntrinsic {
43+
span,
44+
found,
45+
expected,
46+
expected_pluralize: pluralize!(expected),
47+
descr,
48+
});
49+
false
50+
} else {
51+
true
52+
}
53+
};
4254

43-
tcx.sess.emit_err(WrongNumberOfTypeArgumentsToInstrinsic {
44-
span,
45-
found: i_n_tps,
46-
expected: n_tps,
47-
});
48-
return;
55+
if gen_count_ok(own_counts.lifetimes, n_lts, "lifetime")
56+
&& gen_count_ok(own_counts.types, n_tps, "type")
57+
&& gen_count_ok(own_counts.consts, 0, "const")
58+
{
59+
let fty = tcx.mk_fn_ptr(sig);
60+
let cause = ObligationCause::new(it.span, it.hir_id(), ObligationCauseCode::IntrinsicType);
61+
require_same_types(tcx, &cause, tcx.mk_fn_ptr(tcx.fn_sig(it.def_id)), fty);
4962
}
50-
51-
let fty = tcx.mk_fn_ptr(sig);
52-
let cause = ObligationCause::new(it.span, it.hir_id(), ObligationCauseCode::IntrinsicType);
53-
require_same_types(tcx, &cause, tcx.mk_fn_ptr(tcx.fn_sig(it.def_id)), fty);
5463
}
5564

5665
/// Returns the unsafety of the given intrinsic.
@@ -121,7 +130,7 @@ pub fn check_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>) {
121130
})
122131
};
123132

124-
let (n_tps, inputs, output, unsafety) = if name_str.starts_with("atomic_") {
133+
let (n_tps, n_lts, inputs, output, unsafety) = if name_str.starts_with("atomic_") {
125134
let split: Vec<&str> = name_str.split('_').collect();
126135
assert!(split.len() >= 2, "Atomic intrinsic in an incorrect format");
127136

@@ -143,7 +152,7 @@ pub fn check_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>) {
143152
return;
144153
}
145154
};
146-
(n_tps, inputs, output, hir::Unsafety::Unsafe)
155+
(n_tps, 0, inputs, output, hir::Unsafety::Unsafe)
147156
} else {
148157
let unsafety = intrinsic_operation_unsafety(intrinsic_name);
149158
let (n_tps, inputs, output) = match intrinsic_name {
@@ -372,11 +381,11 @@ pub fn check_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>) {
372381
return;
373382
}
374383
};
375-
(n_tps, inputs, output, unsafety)
384+
(n_tps, 0, inputs, output, unsafety)
376385
};
377386
let sig = tcx.mk_fn_sig(inputs.into_iter(), output, false, unsafety, Abi::RustIntrinsic);
378387
let sig = ty::Binder::bind_with_vars(sig, bound_vars);
379-
equate_intrinsic_type(tcx, it, n_tps, sig)
388+
equate_intrinsic_type(tcx, it, n_tps, n_lts, sig)
380389
}
381390

382391
/// Type-check `extern "platform-intrinsic" { ... }` functions.
@@ -472,5 +481,5 @@ pub fn check_platform_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>)
472481
Abi::PlatformIntrinsic,
473482
);
474483
let sig = ty::Binder::dummy(sig);
475-
equate_intrinsic_type(tcx, it, n_tps, sig)
484+
equate_intrinsic_type(tcx, it, n_tps, 0, sig)
476485
}

compiler/rustc_typeck/src/errors.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@ pub struct UnrecognizedAtomicOperation<'a> {
2424

2525
#[derive(SessionDiagnostic)]
2626
#[error = "E0094"]
27-
pub struct WrongNumberOfTypeArgumentsToInstrinsic {
28-
#[message = "intrinsic has wrong number of type \
27+
pub struct WrongNumberOfGenericArgumentsToIntrinsic<'a> {
28+
#[message = "intrinsic has wrong number of {descr} \
2929
parameters: found {found}, expected {expected}"]
30-
#[label = "expected {expected} type parameter"]
30+
#[label = "expected {expected} {descr} parameter{expected_pluralize}"]
3131
pub span: Span,
3232
pub found: usize,
3333
pub expected: usize,
34+
pub expected_pluralize: &'a str,
35+
pub descr: &'a str,
3436
}
3537

3638
#[derive(SessionDiagnostic)]
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Check that appropriate errors are reported if an intrinsic is defined
2+
// with the wrong number of generic lifetime/type/const parameters, and
3+
// that no ICE occurs in these cases.
4+
5+
#![feature(platform_intrinsics)]
6+
#![crate_type="lib"]
7+
8+
extern "platform-intrinsic" {
9+
fn simd_saturating_add<'a, T: 'a>(x: T, y: T);
10+
//~^ ERROR: intrinsic has wrong number of lifetime parameters
11+
12+
fn simd_add<'a, T>(x: T, y: T) -> T;
13+
14+
fn simd_sub<T, U>(x: T, y: U);
15+
//~^ ERROR: intrinsic has wrong number of type parameters
16+
17+
fn simd_mul<T, const N: usize>(x: T, y: T);
18+
//~^ ERROR: intrinsic has wrong number of const parameters
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error[E0094]: intrinsic has wrong number of lifetime parameters: found 1, expected 0
2+
--> $DIR/issue-85855.rs:9:27
3+
|
4+
LL | fn simd_saturating_add<'a, T: 'a>(x: T, y: T);
5+
| ^^^^^^^^^^^ expected 0 lifetime parameters
6+
7+
error[E0094]: intrinsic has wrong number of type parameters: found 2, expected 1
8+
--> $DIR/issue-85855.rs:14:16
9+
|
10+
LL | fn simd_sub<T, U>(x: T, y: U);
11+
| ^^^^^^ expected 1 type parameter
12+
13+
error[E0094]: intrinsic has wrong number of const parameters: found 1, expected 0
14+
--> $DIR/issue-85855.rs:17:16
15+
|
16+
LL | fn simd_mul<T, const N: usize>(x: T, y: T);
17+
| ^^^^^^^^^^^^^^^^^^^ expected 0 const parameters
18+
19+
error: aborting due to 3 previous errors
20+
21+
For more information about this error, try `rustc --explain E0094`.

0 commit comments

Comments
 (0)