Giter VIP home page Giter VIP logo

Comments (2)

steffahn avatar steffahn commented on May 23, 2024 1

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.

steffahn avatar steffahn commented on May 23, 2024

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

  1. 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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.