Comments (2)
But… funky behavior with function pointers in arrays isn’t new, apparently.
type One = for<'a> fn(&'a ());
type Two = fn(&'static ());
// One is subtype of Two
fn same<T>(_: T, _: T) {}
// consistent behavior, same on stable and nightly
fn f(x: One) {
let y: Two = x;
[y, x]; // fails (also on stable, no regression)
same(y, x); // works
[(y,), (x,)]; // works
["", &String::new()]; // works
[&String::new(), ""]; // works, wait… how‽…
[(x,), (y,)]; // fails… as expected, if it takes the
// type of the first element as “correct” (also on stable, no regression)
same(x, y); // fails as expected (also on stable, no regression)
}
I wonder if there’s an issue tracking this inconsistent behavior already.
Similarly, this mirrors the original issue repro, but with analogous behavior that also fails on stable
pub fn foo(_: &i32) {}
pub const _: [fn(&'static i32); 2] = [foo, foo]; // fails (also on stable, no regression)
error[E0308]: mismatched types
--> src/lib.rs:2:44
|
2 | pub const _: [fn(&'static i32); 2] = [foo, foo];
| ^^^ expected fn pointer, found fn item
|
= note: expected fn pointer `fn(&'static _)`
found fn item `for<'a> fn(&'a _) {foo}`
from rust.
Interesting. The PR in question (#118247) does come with deliberate breaking changes that were found not to be an issue in crater runs.
However I find this particular code being broken surprising.1 Here’s a few more test cases (the ones that // fails
all pass on stable).
#![allow(unused)]
type One = for<'a> fn(&'a (), &'a ());
type Two = for<'a, 'b> fn(&'a (), &'b ());
fn f(x: One) {
let y: Two = x; // okay
let arr = [x, x]; // okay
let _: [Two; 2] = arr; // okay
let _: [Two; 2] = [x, x]; // fails (regression)
[y, x]; // fails (regression)
[x, y]; // fails (regression)
same(x, y); // okay
[x, y as _]; // fails (regression)
}
fn same<T>(_: T, _: T) {}
So it almost seems to me like this is some special logic around arrays that can’t handle the new situation after #118247. Perhaps either it’s some handling specifically of functions/function pointers in array expressions; or perhaps generally handling of subtyping there, as we have two distinct types now that are subtypes of each other. Ah wait, here’s another test for that:
[(x,), (y,)]; // okay
let x_: (One,) = (x,);
let y_: (Two,) = (y,);
[x_, y_]; // okay
Okay, so it’s more like specifically functions in arrays that are handled weirdly.
Maybe that special handling is related to the kind of logic that powers
fn f() {}
fn g() {}
fn main() {
let x = [f, g];
}
to compile successfully, deducing the type fn()
as common target (even though nothing was there to indicate it)...
didn’t that also work with match
?
fn same<T>(_: T, _: T) {}
fn f() {}
fn g() {}
fn main() {
same(f, g); // fails (expected, no regression here!)
match () {
_ => f,
_ => g, // works
};
}
yes it did! So let’s test if it’s the same
type One = for<'a> fn(&'a (), &'a ());
type Two = for<'a, 'b> fn(&'a (), &'b ());
fn f(x: One) {
let y: Two = x;
match 0 {
_ => x,
_ => y, // fails (regression)
};
}
yes, indeed!
So the issue is not in array-specific code, but specifically in the code that finds a good “common type” for function items / pointers in array expressions, match
expressions, and probably more.
Footnotes
-
The type of things that are expected to be broken are for example coercions from
*mut One
to*mut Two
↩
from rust.
Related Issues (20)
- Panic at std/thread/mod.rs:1517, called Option::unwrap() on a `None` value
- A warning about redundant imports should be separate from `unused_imports` HOT 1
- Improve MSVC debug info for f16 and f128 HOT 2
- Internal compiler error during build
- format args should suggest using the formatter for const values when it makes sense
- move `EarlyBinder` into `ImplTraitHeader` HOT 1
- ICE: `invalid asymmetric binary op` HOT 1
- `./x check library/std --target=aarch64-kmc-solid_asp3` spuriously requires binaries to be in `PATH` HOT 7
- Accessing large static global behaves incorrectly HOT 10
- Feature: A separate type for measuring thread time HOT 2
- Tracking Issue for porting `run-make` tests to use Rust HOT 4
- Machineary behind an unreachable `panic!` ends-up in final optimized binary HOT 6
- ICE: query 'hir_crate' not cached due to poisoning query 'hir_attrs' not cached due to poisoning HOT 2
- std::env::args should work in wasm32-unknown-emscripten staticlibs HOT 5
- Miri (possibly everything that depends on rustc private crates) no longer builds: cannot find -lLLVM-18-rust-1.78.0-nightly HOT 28
- Tracking Issue for `acceptfilter`
- ICE: `type parameter B/#1 (B/#1/1) out of range when instantiating` HOT 1
- Incorrect suggestion to add turbofish when `>` is missing HOT 1
- `unused_imports` redundant import `is already defined here` points to self HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rust.