Rollup merge of #91272 - FabianWolff:issue-90870-const-fn-eq, r=wesleywiser

Print a suggestion when comparing references to primitive types in `const fn`

Fixes #90870.
This commit is contained in:
Matthias Krüger 2021-12-08 11:08:57 +01:00 committed by GitHub
commit 871cf2bc9e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 170 additions and 10 deletions

View file

@ -0,0 +1,34 @@
// Regression test for issue #90870.
// run-rustfix
#![allow(dead_code)]
const fn f(a: &u8, b: &u8) -> bool {
*a == *b
//~^ ERROR: calls in constant functions are limited to constant functions, tuple structs and tuple variants [E0015]
//~| HELP: consider dereferencing here
}
const fn g(a: &&&&i64, b: &&&&i64) -> bool {
****a == ****b
//~^ ERROR: calls in constant functions are limited to constant functions, tuple structs and tuple variants [E0015]
//~| HELP: consider dereferencing here
}
const fn h(mut a: &[u8], mut b: &[u8]) -> bool {
while let ([l, at @ ..], [r, bt @ ..]) = (a, b) {
if *l == *r {
//~^ ERROR: calls in constant functions are limited to constant functions, tuple structs and tuple variants [E0015]
//~| HELP: consider dereferencing here
a = at;
b = bt;
} else {
return false;
}
}
a.is_empty() && b.is_empty()
}
fn main() {}

View file

@ -0,0 +1,34 @@
// Regression test for issue #90870.
// run-rustfix
#![allow(dead_code)]
const fn f(a: &u8, b: &u8) -> bool {
a == b
//~^ ERROR: calls in constant functions are limited to constant functions, tuple structs and tuple variants [E0015]
//~| HELP: consider dereferencing here
}
const fn g(a: &&&&i64, b: &&&&i64) -> bool {
a == b
//~^ ERROR: calls in constant functions are limited to constant functions, tuple structs and tuple variants [E0015]
//~| HELP: consider dereferencing here
}
const fn h(mut a: &[u8], mut b: &[u8]) -> bool {
while let ([l, at @ ..], [r, bt @ ..]) = (a, b) {
if l == r {
//~^ ERROR: calls in constant functions are limited to constant functions, tuple structs and tuple variants [E0015]
//~| HELP: consider dereferencing here
a = at;
b = bt;
} else {
return false;
}
}
a.is_empty() && b.is_empty()
}
fn main() {}

View file

@ -0,0 +1,36 @@
error[E0015]: calls in constant functions are limited to constant functions, tuple structs and tuple variants
--> $DIR/issue-90870.rs:8:5
|
LL | a == b
| ^^^^^^
|
help: consider dereferencing here
|
LL | *a == *b
| + +
error[E0015]: calls in constant functions are limited to constant functions, tuple structs and tuple variants
--> $DIR/issue-90870.rs:14:5
|
LL | a == b
| ^^^^^^
|
help: consider dereferencing here
|
LL | ****a == ****b
| ++++ ++++
error[E0015]: calls in constant functions are limited to constant functions, tuple structs and tuple variants
--> $DIR/issue-90870.rs:21:12
|
LL | if l == r {
| ^^^^^^
|
help: consider dereferencing here
|
LL | if *l == *r {
| + +
error: aborting due to 3 previous errors
For more information about this error, try `rustc --explain E0015`.