Repository navigation
Add internal #[rustc_assert_variance] attribute for use in std #163578
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
6314d17
5f5e9ab
c56b6ee
6abfa1c
576a9e5
6d5354f
07c2c7c
b138c7b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,8 +3,8 @@ use std::path::PathBuf; | |
| use rustc_ast::{GenericParamKind, ItemKind, LitIntType, LitKind, MetaItemLit}; | ||
| use rustc_attr_ir::lang_items::LangItem; | ||
| use rustc_attr_ir::{ | ||
| BorrowckGraphvizFormatKind, CguFields, CguKind, RustcCleanAttribute, RustcCleanQueries, | ||
| RustcMirKind, | ||
| BorrowckGraphvizFormatKind, CguFields, CguKind, RustcAssertVarianceKind, RustcCleanAttribute, | ||
| RustcCleanQueries, RustcMirKind, | ||
| }; | ||
| use rustc_data_structures::fx::FxHashMap; | ||
| use rustc_feature::AttributeStability; | ||
|
|
@@ -1168,3 +1168,54 @@ impl NoArgsAttributeParser for RustcCanonicalSymbolParser { | |
| ); | ||
| const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcCanonicalSymbol; | ||
| } | ||
|
|
||
| pub(crate) struct RustcAssertVarianceParser; | ||
|
|
||
| impl SingleAttributeParser for RustcAssertVarianceParser { | ||
| const PATH: &[Symbol] = &[sym::rustc_assert_variance]; | ||
|
|
||
| const STABILITY: AttributeStability = unstable!(rustc_attrs); | ||
|
|
||
| const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[ | ||
| Allow(Target::TypeParam), | ||
| Allow(Target::LifetimeParam), | ||
| Allow(Target::ConstParam), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can const parameters be anything other than invariant? 😨
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not to my knowledge. It's more there for syntactic completeness so that the attribute can attach to any generic parameter. |
||
| ]); | ||
|
|
||
| const TEMPLATE: AttributeTemplate = | ||
| template!(OneOf: &[sym::covariant, sym::invariant, sym::contravariant, sym::bivariant]); | ||
|
|
||
| fn convert(cx: &mut AcceptContext<'_, '_>, args: &ArgParser) -> Option<AttributeKind> { | ||
| let arg = cx.expect_single_element_list(args, cx.attr_span)?; | ||
|
|
||
| let mut fail_arg = |span| { | ||
| cx.adcx().expected_specific_argument( | ||
| span, | ||
| &[sym::covariant, sym::invariant, sym::contravariant, sym::bivariant], | ||
| ) | ||
| }; | ||
|
|
||
| let Some(arg) = arg.meta_item_no_args() else { | ||
| fail_arg(arg.span()); | ||
| return None; | ||
| }; | ||
|
|
||
| let Some(arg) = arg.ident() else { | ||
| fail_arg(arg.span()); | ||
| return None; | ||
| }; | ||
|
|
||
| let kind = match arg.name { | ||
| sym::covariant => RustcAssertVarianceKind::Covariant, | ||
| sym::invariant => RustcAssertVarianceKind::Invariant, | ||
| sym::contravariant => RustcAssertVarianceKind::Contravariant, | ||
| sym::bivariant => RustcAssertVarianceKind::Bivariant, | ||
| _ => { | ||
| fail_arg(arg.span); | ||
| return None; | ||
| } | ||
| }; | ||
|
|
||
| Some(AttributeKind::RustcAssertVariance { span: cx.attr_span, kind }) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,7 +14,7 @@ use rustc_attr_ir::lang_items::LangItem; | |
| use rustc_attr_ir::target::{AssocCtxt, MethodKind, Target}; | ||
| use rustc_attr_ir::{ | ||
| Attribute, AttributeKind, DocAttribute, EiiDecl, EiiImpl, EiiImplResolution, InlineAttr, | ||
| OptimizeAttr, ReprAttr, find_attr, | ||
| OptimizeAttr, ReprAttr, RustcAssertVarianceKind, find_attr, | ||
| }; | ||
| use rustc_attr_parsing::AttributeParser; | ||
| use rustc_data_structures::thin_vec::ThinVec; | ||
|
|
@@ -37,7 +37,7 @@ use rustc_middle::hir::nested_filter; | |
| use rustc_middle::query::Providers; | ||
| use rustc_middle::traits::ObligationCause; | ||
| use rustc_middle::ty::error::{ExpectedFound, TypeError}; | ||
| use rustc_middle::ty::{self, TyCtxt, TypingMode, Unnormalized}; | ||
| use rustc_middle::ty::{self, TyCtxt, TypingMode, Unnormalized, Variance}; | ||
| use rustc_session::diagnostics::feature_err; | ||
| use rustc_span::edition::Edition; | ||
| use rustc_span::{DUMMY_SP, Ident, Span, Symbol, bug, kw, span_bug, sym}; | ||
|
|
@@ -223,6 +223,9 @@ impl<'tcx> CheckAttrVisitor<'tcx> { | |
| AttributeKind::Linkage(_linkage, span) => { | ||
| self.check_linkage(*span, hir_id, target, item) | ||
| } | ||
| AttributeKind::RustcAssertVariance { span: attr_span, kind } => { | ||
| self.check_rustc_assert_variance(span, *attr_span, hir_id, target, *kind); | ||
| } | ||
|
|
||
| // All of the following attributes have no specific checks. | ||
| // tidy-alphabetical-start | ||
|
|
@@ -1417,6 +1420,69 @@ impl<'tcx> CheckAttrVisitor<'tcx> { | |
| _ => {} | ||
| } | ||
| } | ||
|
|
||
| fn check_rustc_assert_variance( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In PR #161351 I moved a I guess
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see no harm in it being in check_attr, stuff like "is #[may_dangle] on a Drop impl" is also in check_attr. Happy to cede to wherever you think this fits best though.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, I just thought y'all wanted to yeet
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's a decent amount of "need to run queries etc for this check" code in check_attr, so getting rid of check_attr entirely isn't going to happen sadly. |
||
| &self, | ||
| span: Span, | ||
| attr_span: Span, | ||
| hir_id: HirId, | ||
| target: Target, | ||
| kind: RustcAssertVarianceKind, | ||
| ) { | ||
| // If the target is invalid the parser has already emitted an error | ||
| if !matches!(target, Target::TypeParam | Target::LifetimeParam | Target::ConstParam) { | ||
| return; | ||
| } | ||
|
|
||
| let Node::GenericParam(param) = self.tcx.hir_node(hir_id) else { return }; | ||
| let owner = hir_id.owner.to_def_id(); | ||
| let variances = self.tcx.variances_of(owner); | ||
| let generics = self.tcx.generics_of(owner); | ||
|
|
||
| let variance = generics | ||
| .own_params | ||
| .iter() | ||
| .zip(variances.iter()) | ||
| .filter(|(other_param, _)| other_param.def_id == param.def_id.to_def_id()) | ||
| .map(|(_, variance)| variance) | ||
| .nth(0) | ||
| .expect("generic parameter should have variance"); | ||
|
|
||
| match (kind, variance) { | ||
| (RustcAssertVarianceKind::Covariant, Variance::Covariant) => {} | ||
| (RustcAssertVarianceKind::Invariant, Variance::Invariant) => {} | ||
| (RustcAssertVarianceKind::Contravariant, Variance::Contravariant) => {} | ||
| (RustcAssertVarianceKind::Bivariant, Variance::Bivariant) => {} | ||
| (asserted, actual) => { | ||
| let expected_variance = match asserted { | ||
| RustcAssertVarianceKind::Covariant => sym::covariant, | ||
| RustcAssertVarianceKind::Invariant => sym::invariant, | ||
| RustcAssertVarianceKind::Contravariant => sym::contravariant, | ||
| RustcAssertVarianceKind::Bivariant => sym::bivariant, | ||
| }; | ||
|
|
||
| let actual_variance = match actual { | ||
| Variance::Covariant => sym::covariant, | ||
| Variance::Invariant => sym::invariant, | ||
| Variance::Contravariant => sym::contravariant, | ||
| Variance::Bivariant => sym::bivariant, | ||
| }; | ||
|
|
||
| let item_ident = self.tcx.item_ident(owner); | ||
|
|
||
| let note = diagnostics::RustcAssertVarianceFailedNote { span: attr_span }; | ||
| self.tcx.dcx().emit_err(diagnostics::RustcAssertVarianceFailed { | ||
| item_span: self.tcx.def_span(owner), | ||
| note, | ||
| param_ident: param.name.ident(), | ||
| param_span: span, | ||
| item_ident, | ||
| expected_variance, | ||
| actual_variance, | ||
| }); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<'tcx> Visitor<'tcx> for CheckAttrVisitor<'tcx> { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know it technically doesn't matter for the implementation, but I do kinda wish these were:
so that they internally represent the variance a bit better, even if we don't rely on that.
View changes since the review
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this idea, but it seems more relevant for rustc_type_ir::Variance since that's what's actually used by the compiler to represent variance.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(also worth asking whether these two types should be different at all)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would rather not that rustc_attr_ir depends on rustc_type_ir, or the other way around.
It could live earlier in the crate graph, in rustc_structures, but moving it would also mean moving a lot of
Variancerelated methods out of rustc_type_ir. So merging these two comes with downsides.