Repository navigation
Conversation
This comment has been minimized.
This comment has been minimized.
08d707c to
9959a0a
Compare
|
r? mejrs |
|
Some changes occurred in compiler/rustc_passes/src/check_attr.rs cc @jdonszelmann, @JonathanBrouwer
Some changes occurred in compiler/rustc_attr_parsing cc @jdonszelmann, @JonathanBrouwer
cc @rust-lang/clippy
cc @bjorn3 Some changes occurred in compiler/rustc_attr_ir |
There was a problem hiding this comment.
rustc_force_variance is unsound, with it you can write code such as:
#![feature(rustc_attrs)]
#![allow(internal_features)]
struct CovariantMutRef<'a, #[rustc_force_variance(covariant)] T> {
data: &'a mut T,
}
fn main() {
let mut x: &'static str = "ferris";
let long: CovariantMutRef<'_, &'static str> = CovariantMutRef { data: &mut x };
{
let y = String::from("corro");
let short: CovariantMutRef<'_, &str> = long;
*short.data = &y;
}
println!("{}", x);
}produces:
error: Undefined Behavior: constructing invalid value of type &str: encountered a dangling reference (use-after-free)
--> RUSTLIB/core/src/fmt/mod.rs:LL:CC
|
LL | fn fmt(&self, f: &mut Formatter<'_>) -> Result { $tr::fmt(&**self, f) }
| ^^^^^^^ Undefined Behavior occurred here
...
LL | fmt_refs! { Debug, Display, Octal, Binary, LowerHex, UpperHex, LowerExp, UpperExp }
| ----------------------------------------------------------------------------------- in this macro invocation
|
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
While it's not a Big Deal since it's an internal attribute, I think it ought to be addressed:
- someone might at some point end up using it in a std data structure and accidentally expose such an unsoundness
- I'm not entirely sure what
CovariantUnsafeCellneeds to do to be sound. I think it is currently sound because of the absence of a&mut T -> &mut CovariantUnsafeCell<T>method? It would be nice to capture this in a safety comment somewhere.
I suspect the easiest fix is to just make the attribute unsafe.
Can you also add a feature gate test for both attributes?
| if let ItemKind::Struct(_, _, data) = &item.kind | ||
| && !matches!(data, VariantData::Unit(_)) | ||
| { | ||
| cx.emit_err(crate::diagnostics::InvalidPhantomMarkerTarget { attr_span }); |
|
I'll make the attribute unsafe and add the test. These attributes shouldn't get a feature gate: they are internal and never going to be stabilized. There is no point in giving them a separate tracking issue. Improving the docs of
|
No, I mean a test checking that they can't be used without |
This comment has been minimized.
This comment has been minimized.
9959a0a to
684476f
Compare
|
This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
|
☔ The latest upstream changes (presumably #165056) made this pull request unmergeable. Please resolve the merge conflicts by rebasing. |
Tracking issue: #135806
This PR changes the
Phantom{Covariant,Contravariant,Invariant}(Lifetime)marker types so that they mirror thePhantomDatatype: they can now be constructed as unit structs, no longer need the awkwardvariance()constructor, and are treated the same way asPhantomData.The compiler side adds 2 internal attributes:
#[rustc_force_variance(covariant|contravariant|invariant|bivariant)]on a generic parameter sets the variance of that parameter and overrides the one inferred from fields. This also removes the requirement that this parameter is used within a struct, allowing for generics on a unit struct. This is used forUnsafeCell,CovariantUnsafeCellandPhantomData, removing the current list of special cases in the variance solver.#[rustc_phantom_marker]on a unit struct indicates that this type should be ignored by some lints and structural checks, listed below.These checks were previously explicitly ignoring
PhantomDatafields, and have now been extended to ignore all#[rustc_phantom_marker]fields:DispatchFromDynCoerceUnsizedReborrowdead_code)used_underscore_bindingin clippyThese checks continue to be specific to
PhantomDatabecause it implies ownership of aT:Tfor auto-traits.Tin the drop-check constraints.Finally, the trait impls have been changed to be consistent with
PhantomData: these types now have aconst Defaultimpl and aStructualPartialEqimpl.