Type covariance absent when used in struct

Aperta
#1,610 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
48/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Tranquilla
Stack tecnologico
rust
Ambito
data

Direzione di ricerca

Start with the reported Test struct and the covariant and invariant function examples in this issue. Compare variance behavior for Array2 fields with Box, tuples, and Rust arrays; done means identifying and correcting the discrepancy and adding a regression test for the lifetime conversion.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Arrays are covariant, but structs containing them don't seem to be. If I create a struct

struct Test<T> (ndarray::Array2<T>);

then, even though Array2 is covariant over T, my struct isn't:

// works
pub fn covariant<'long: 'short, 'short>(input: Box<ndarray::Array2<&'long u8>>) -> Box<ndarray::Array2<&'short u8>> {
    input
}

// compiler error
pub fn invariant<'long: 'short, 'short>(input: Box<Test<&'long u8>>) -> Box<Test<&'short u8>> {
    input
}

This doesn't make sense to me. From the rust reference, I would expect Test's variance to depend only on the types of its field, but it doesn't. Moreover, only structs that internally contain Array2 are affected: structs that take the array as a parameter (like the Box above) are fine, as are tuples and rust arrays. This is also unaffected by the complexity of the type inside the array, as long as it references a lifetime.

Lingua principale
Rust
Stelle
4.3k
Fork
391
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di rust-ndarray/ndarray

Tutte le issue di rust-ndarray/ndarray

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.