diff options
| author | Frank King <frankking1729@gmail.com> | 2025-02-05 18:58:29 +0800 |
|---|---|---|
| committer | Frank King <frankking1729@gmail.com> | 2025-03-01 22:02:46 +0800 |
| commit | 42f51d4fd42c95e0c51c3f8742d63db0548cd5a0 (patch) | |
| tree | 754318d4e5ccbdb944dc38741039989a48d124e4 /compiler/rustc_passes/src | |
| parent | 30508faeb3248d399079513b6e0107af30a43948 (diff) | |
| download | rust-42f51d4fd42c95e0c51c3f8742d63db0548cd5a0.tar.gz rust-42f51d4fd42c95e0c51c3f8742d63db0548cd5a0.zip | |
Implment `#[cfg]` and `#[cfg_attr]` in `where` clauses
Diffstat (limited to 'compiler/rustc_passes/src')
| -rw-r--r-- | compiler/rustc_passes/src/check_attr.rs | 29 | ||||
| -rw-r--r-- | compiler/rustc_passes/src/errors.rs | 8 |
2 files changed, 36 insertions, 1 deletions
diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs index 5ada289cc20..d6c0edf5ae7 100644 --- a/compiler/rustc_passes/src/check_attr.rs +++ b/compiler/rustc_passes/src/check_attr.rs @@ -919,7 +919,8 @@ impl<'tcx> CheckAttrVisitor<'tcx> { | Target::Arm | Target::ForeignMod | Target::Closure - | Target::Impl => Some(target.name()), + | Target::Impl + | Target::WherePredicate => Some(target.name()), Target::ExternCrate | Target::Use | Target::Static @@ -2614,6 +2615,32 @@ impl<'tcx> Visitor<'tcx> for CheckAttrVisitor<'tcx> { intravisit::walk_item(self, item) } + fn visit_where_predicate(&mut self, where_predicate: &'tcx hir::WherePredicate<'tcx>) { + // FIXME(where_clause_attrs): Currently, as the following check shows, + // only `#[cfg]` and `#[cfg_attr]` are allowed, but it should be removed + // if we allow more attributes (e.g., tool attributes and `allow/deny/warn`) + // in where clauses. After that, only `self.check_attributes` should be enough. + const ATTRS_ALLOWED: &[Symbol] = &[sym::cfg, sym::cfg_attr]; + let spans = self + .tcx + .hir() + .attrs(where_predicate.hir_id) + .iter() + .filter(|attr| !ATTRS_ALLOWED.iter().any(|&sym| attr.has_name(sym))) + .map(|attr| attr.span()) + .collect::<Vec<_>>(); + if !spans.is_empty() { + self.tcx.dcx().emit_err(errors::UnsupportedAttributesInWhere { span: spans.into() }); + } + self.check_attributes( + where_predicate.hir_id, + where_predicate.span, + Target::WherePredicate, + None, + ); + intravisit::walk_where_predicate(self, where_predicate) + } + fn visit_generic_param(&mut self, generic_param: &'tcx hir::GenericParam<'tcx>) { let target = Target::from_generic_param(generic_param); self.check_attributes(generic_param.hir_id, generic_param.span, target, None); diff --git a/compiler/rustc_passes/src/errors.rs b/compiler/rustc_passes/src/errors.rs index 9bb9b2353dc..b8359c27e53 100644 --- a/compiler/rustc_passes/src/errors.rs +++ b/compiler/rustc_passes/src/errors.rs @@ -1909,3 +1909,11 @@ pub(crate) struct RustcConstStableIndirectPairing { #[primary_span] pub span: Span, } + +#[derive(Diagnostic)] +#[diag(passes_unsupported_attributes_in_where)] +#[help] +pub(crate) struct UnsupportedAttributesInWhere { + #[primary_span] + pub span: MultiSpan, +} |
