Open
Description
rust-analyzer version: rust-analyzer version: 0.3.2353-standalone (37acea8052 2025-03-23)
rustc version: rustc 1.84.1 (e71f9a9a9 2025-01-27)
editor or extension: VSCode with rust-analyzer version 0.3.2353
code snippet to reproduce:
use std::cell::RefCell;
/// This type is covariant:
/// ```
/// use rust_playground::lifetimes::Recursive;
/// fn is_covariant<'long, 'short>(value: Recursive<'long>)
/// where
/// 'long: 'short,
/// {
/// let _: Recursive<'short> = value;
/// }
/// ```
/// and not contravariant:
/// ```compile_fail
/// use rust_playground::lifetimes::Recursive;
/// fn is_contravariant<'long, 'short>(value: Recursive<'short>)
/// where
/// 'long: 'short,
/// {
/// let _: Recursive<'long> = value;
/// }
/// ```
pub struct Recursive<'a> {
v: &'a i32,
parent: Option<Box<Recursive<'a>>>,
}
/// This type is not covariant:
/// ```compile_fail
/// use rust_playground::lifetimes::Recursive1;
/// fn is_covariant<'long, 'short>(value: Recursive1<'long>)
/// where
/// 'long: 'short,
/// {
/// let _: Recursive1<'short> = value;
/// }
/// ```
/// and not contravariant
/// ```compile_fail
/// use rust_playground::lifetimes::Recursive1;
/// fn is_contravariant<'long, 'short>(value: Recursive1<'short>)
/// where
/// 'long: 'short,
/// {
/// let _: Recursive1<'long> = value;
/// }
/// ```
/// i.e. it is invariant
pub struct Recursive1<'a>(&'a i32, Option<Box<Recursive2<'a>>>);
pub struct Recursive2<'a>(RefCell<Recursive1<'a>>);
For all three defined structs if I hover over the 'a
parameter in VSCode it shows me bivariant
although Recursive
is covariant and Recursive1
and Recursive2
are invariant over 'a
.
This gets worse if for example the invariant structs are used together with other covariant types for example:
pub struct Mixed<'a>(&'a i32, Recursive1<'a>);
In this case when I hover over it, it shows covariant
although in fact it is invariant.