Skip to content

Commit

Permalink
Minor adjustments and refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianWolff committed Jul 1, 2021
1 parent 7b2befc commit fe93349
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 67 deletions.
3 changes: 1 addition & 2 deletions compiler/rustc_error_codes/src/error_codes/E0094.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
An invalid number of generic type, lifetime, or const parameters was
given to an intrinsic function.
An invalid number of generic parameters was passed to an intrinsic function.

Erroneous code example:

Expand Down
77 changes: 24 additions & 53 deletions compiler/rustc_typeck/src/check/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate::errors::{
SimdShuffleMissingLength, UnrecognizedAtomicOperation, UnrecognizedIntrinsicFunction,
WrongNumberOfGenericArgumentsToInstrinsic,
WrongNumberOfGenericArgumentsToIntrinsic,
};
use crate::require_same_types;

Expand All @@ -24,27 +24,10 @@ fn equate_intrinsic_type<'tcx>(
n_lts: usize,
sig: ty::PolyFnSig<'tcx>,
) {
let (gen_lts, gen_tys, gen_cns, span) = match &it.kind {
let (own_counts, span) = match &it.kind {
hir::ForeignItemKind::Fn(.., generics) => {
let mut gen_lts = 0;
let mut gen_tys = 0;
let mut gen_cns = 0;

for param in generics.params {
match param.kind {
hir::GenericParamKind::Lifetime { .. } => {
gen_lts += 1;
}
hir::GenericParamKind::Type { .. } => {
gen_tys += 1;
}
hir::GenericParamKind::Const { .. } => {
gen_cns += 1;
}
}
}

(gen_lts, gen_tys, gen_cns, generics.span)
let own_counts = tcx.generics_of(it.def_id.to_def_id()).own_counts();
(own_counts, generics.span)
}
_ => {
struct_span_err!(tcx.sess, it.span, E0622, "intrinsic must be a function")
Expand All @@ -54,31 +37,25 @@ fn equate_intrinsic_type<'tcx>(
}
};

if gen_lts != n_lts {
tcx.sess.emit_err(WrongNumberOfGenericArgumentsToInstrinsic {
span,
found: gen_lts,
expected: n_lts,
expected_pluralize: pluralize!(n_lts),
descr: "lifetime",
});
} else if gen_tys != n_tps {
tcx.sess.emit_err(WrongNumberOfGenericArgumentsToInstrinsic {
span,
found: gen_tys,
expected: n_tps,
expected_pluralize: pluralize!(n_tps),
descr: "type",
});
} else if gen_cns != 0 {
tcx.sess.emit_err(WrongNumberOfGenericArgumentsToInstrinsic {
span,
found: gen_cns,
expected: 0,
expected_pluralize: pluralize!(0),
descr: "const",
});
} else {
let gen_count_ok = |found: usize, expected: usize, descr: &str| -> bool {
if found != expected {
tcx.sess.emit_err(WrongNumberOfGenericArgumentsToIntrinsic {
span,
found,
expected,
expected_pluralize: pluralize!(expected),
descr,
});
false
} else {
true
}
};

if gen_count_ok(own_counts.lifetimes, n_lts, "lifetime")
&& gen_count_ok(own_counts.types, n_tps, "type")
&& gen_count_ok(own_counts.consts, 0, "const")
{
let fty = tcx.mk_fn_ptr(sig);
let cause = ObligationCause::new(it.span, it.hir_id(), ObligationCauseCode::IntrinsicType);
require_same_types(tcx, &cause, tcx.mk_fn_ptr(tcx.fn_sig(it.def_id)), fty);
Expand Down Expand Up @@ -404,13 +381,7 @@ pub fn check_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>) {
return;
}
};
(
n_tps,
if matches!(intrinsic_name, sym::va_copy) { 1 } else { 0 },
inputs,
output,
unsafety,
)
(n_tps, 0, inputs, output, unsafety)
};
let sig = tcx.mk_fn_sig(inputs.into_iter(), output, false, unsafety, Abi::RustIntrinsic);
let sig = ty::Binder::bind_with_vars(sig, bound_vars);
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_typeck/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub struct UnrecognizedAtomicOperation<'a> {

#[derive(SessionDiagnostic)]
#[error = "E0094"]
pub struct WrongNumberOfGenericArgumentsToInstrinsic<'a> {
pub struct WrongNumberOfGenericArgumentsToIntrinsic<'a> {
#[message = "intrinsic has wrong number of {descr} \
parameters: found {found}, expected {expected}"]
#[label = "expected {expected} {descr} parameter{expected_pluralize}"]
Expand Down
3 changes: 1 addition & 2 deletions src/test/ui/simd-intrinsic/issue-85855.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ extern "platform-intrinsic" {
fn simd_saturating_add<'a, T: 'a>(x: T, y: T);
//~^ ERROR: intrinsic has wrong number of lifetime parameters

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

fn simd_sub<T, U>(x: T, y: U);
//~^ ERROR: intrinsic has wrong number of type parameters
Expand Down
12 changes: 3 additions & 9 deletions src/test/ui/simd-intrinsic/issue-85855.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,18 @@ error[E0094]: intrinsic has wrong number of lifetime parameters: found 1, expect
LL | fn simd_saturating_add<'a, T: 'a>(x: T, y: T);
| ^^^^^^^^^^^ expected 0 lifetime parameters

error[E0094]: intrinsic has wrong number of lifetime parameters: found 1, expected 0
--> $DIR/issue-85855.rs:12:16
|
LL | fn simd_add<'a, T>(x: T, y: T);
| ^^^^^^^ expected 0 lifetime parameters

error[E0094]: intrinsic has wrong number of type parameters: found 2, expected 1
--> $DIR/issue-85855.rs:15:16
--> $DIR/issue-85855.rs:14:16
|
LL | fn simd_sub<T, U>(x: T, y: U);
| ^^^^^^ expected 1 type parameter

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

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

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

0 comments on commit fe93349

Please sign in to comment.