Skip to content

Commit fe93349

Browse files
committed
Minor adjustments and refactoring
1 parent 7b2befc commit fe93349

File tree

5 files changed

+30
-67
lines changed

5 files changed

+30
-67
lines changed

compiler/rustc_error_codes/src/error_codes/E0094.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
An invalid number of generic type, lifetime, or const parameters was
2-
given to an intrinsic function.
1+
An invalid number of generic parameters was passed to an intrinsic function.
32

43
Erroneous code example:
54

compiler/rustc_typeck/src/check/intrinsic.rs

+24-53
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
44
use crate::errors::{
55
SimdShuffleMissingLength, UnrecognizedAtomicOperation, UnrecognizedIntrinsicFunction,
6-
WrongNumberOfGenericArgumentsToInstrinsic,
6+
WrongNumberOfGenericArgumentsToIntrinsic,
77
};
88
use crate::require_same_types;
99

@@ -24,27 +24,10 @@ fn equate_intrinsic_type<'tcx>(
2424
n_lts: usize,
2525
sig: ty::PolyFnSig<'tcx>,
2626
) {
27-
let (gen_lts, gen_tys, gen_cns, span) = match &it.kind {
27+
let (own_counts, span) = match &it.kind {
2828
hir::ForeignItemKind::Fn(.., generics) => {
29-
let mut gen_lts = 0;
30-
let mut gen_tys = 0;
31-
let mut gen_cns = 0;
32-
33-
for param in generics.params {
34-
match param.kind {
35-
hir::GenericParamKind::Lifetime { .. } => {
36-
gen_lts += 1;
37-
}
38-
hir::GenericParamKind::Type { .. } => {
39-
gen_tys += 1;
40-
}
41-
hir::GenericParamKind::Const { .. } => {
42-
gen_cns += 1;
43-
}
44-
}
45-
}
46-
47-
(gen_lts, gen_tys, gen_cns, generics.span)
29+
let own_counts = tcx.generics_of(it.def_id.to_def_id()).own_counts();
30+
(own_counts, generics.span)
4831
}
4932
_ => {
5033
struct_span_err!(tcx.sess, it.span, E0622, "intrinsic must be a function")
@@ -54,31 +37,25 @@ fn equate_intrinsic_type<'tcx>(
5437
}
5538
};
5639

57-
if gen_lts != n_lts {
58-
tcx.sess.emit_err(WrongNumberOfGenericArgumentsToInstrinsic {
59-
span,
60-
found: gen_lts,
61-
expected: n_lts,
62-
expected_pluralize: pluralize!(n_lts),
63-
descr: "lifetime",
64-
});
65-
} else if gen_tys != n_tps {
66-
tcx.sess.emit_err(WrongNumberOfGenericArgumentsToInstrinsic {
67-
span,
68-
found: gen_tys,
69-
expected: n_tps,
70-
expected_pluralize: pluralize!(n_tps),
71-
descr: "type",
72-
});
73-
} else if gen_cns != 0 {
74-
tcx.sess.emit_err(WrongNumberOfGenericArgumentsToInstrinsic {
75-
span,
76-
found: gen_cns,
77-
expected: 0,
78-
expected_pluralize: pluralize!(0),
79-
descr: "const",
80-
});
81-
} else {
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+
};
54+
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+
{
8259
let fty = tcx.mk_fn_ptr(sig);
8360
let cause = ObligationCause::new(it.span, it.hir_id(), ObligationCauseCode::IntrinsicType);
8461
require_same_types(tcx, &cause, tcx.mk_fn_ptr(tcx.fn_sig(it.def_id)), fty);
@@ -404,13 +381,7 @@ pub fn check_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>) {
404381
return;
405382
}
406383
};
407-
(
408-
n_tps,
409-
if matches!(intrinsic_name, sym::va_copy) { 1 } else { 0 },
410-
inputs,
411-
output,
412-
unsafety,
413-
)
384+
(n_tps, 0, inputs, output, unsafety)
414385
};
415386
let sig = tcx.mk_fn_sig(inputs.into_iter(), output, false, unsafety, Abi::RustIntrinsic);
416387
let sig = ty::Binder::bind_with_vars(sig, bound_vars);

compiler/rustc_typeck/src/errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub struct UnrecognizedAtomicOperation<'a> {
2424

2525
#[derive(SessionDiagnostic)]
2626
#[error = "E0094"]
27-
pub struct WrongNumberOfGenericArgumentsToInstrinsic<'a> {
27+
pub struct WrongNumberOfGenericArgumentsToIntrinsic<'a> {
2828
#[message = "intrinsic has wrong number of {descr} \
2929
parameters: found {found}, expected {expected}"]
3030
#[label = "expected {expected} {descr} parameter{expected_pluralize}"]

src/test/ui/simd-intrinsic/issue-85855.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ extern "platform-intrinsic" {
99
fn simd_saturating_add<'a, T: 'a>(x: T, y: T);
1010
//~^ ERROR: intrinsic has wrong number of lifetime parameters
1111

12-
fn simd_add<'a, T>(x: T, y: T);
13-
//~^ ERROR: intrinsic has wrong number of lifetime parameters
12+
fn simd_add<'a, T>(x: T, y: T) -> T;
1413

1514
fn simd_sub<T, U>(x: T, y: U);
1615
//~^ ERROR: intrinsic has wrong number of type parameters

src/test/ui/simd-intrinsic/issue-85855.stderr

+3-9
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,18 @@ error[E0094]: intrinsic has wrong number of lifetime parameters: found 1, expect
44
LL | fn simd_saturating_add<'a, T: 'a>(x: T, y: T);
55
| ^^^^^^^^^^^ expected 0 lifetime parameters
66

7-
error[E0094]: intrinsic has wrong number of lifetime parameters: found 1, expected 0
8-
--> $DIR/issue-85855.rs:12:16
9-
|
10-
LL | fn simd_add<'a, T>(x: T, y: T);
11-
| ^^^^^^^ expected 0 lifetime parameters
12-
137
error[E0094]: intrinsic has wrong number of type parameters: found 2, expected 1
14-
--> $DIR/issue-85855.rs:15:16
8+
--> $DIR/issue-85855.rs:14:16
159
|
1610
LL | fn simd_sub<T, U>(x: T, y: U);
1711
| ^^^^^^ expected 1 type parameter
1812

1913
error[E0094]: intrinsic has wrong number of const parameters: found 1, expected 0
20-
--> $DIR/issue-85855.rs:18:16
14+
--> $DIR/issue-85855.rs:17:16
2115
|
2216
LL | fn simd_mul<T, const N: usize>(x: T, y: T);
2317
| ^^^^^^^^^^^^^^^^^^^ expected 0 const parameters
2418

25-
error: aborting due to 4 previous errors
19+
error: aborting due to 3 previous errors
2620

2721
For more information about this error, try `rustc --explain E0094`.

0 commit comments

Comments
 (0)