Skip to content

Commit 19c9725

Browse files
committed
check earlier for attributes that are placed in where predicate
1 parent c5dc1c1 commit 19c9725

7 files changed

Lines changed: 188 additions & 175 deletions

File tree

compiler/rustc_attr_parsing/src/errors.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use rustc_errors::MultiSpan;
12
use rustc_macros::{Diagnostic, Subdiagnostic};
23
use rustc_span::{Span, Symbol};
34

@@ -24,3 +25,11 @@ pub(crate) struct ItemFollowingInnerAttr {
2425
#[primary_span]
2526
pub span: Span,
2627
}
28+
29+
#[derive(Diagnostic)]
30+
#[diag("most attributes are not supported in `where` clauses")]
31+
#[help("only `#[cfg]` and `#[cfg_attr]` are supported")]
32+
pub(crate) struct UnsupportedAttributesInWhere {
33+
#[primary_span]
34+
pub span: MultiSpan,
35+
}

compiler/rustc_attr_parsing/src/interface.rs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::convert::identity;
33
use rustc_ast as ast;
44
use rustc_ast::token::DocFragmentKind;
55
use rustc_ast::{AttrItemKind, AttrStyle, NodeId, Safety};
6-
use rustc_errors::{DiagCtxtHandle, StashKey};
6+
use rustc_errors::{DiagCtxtHandle, MultiSpan, StashKey};
77
use rustc_feature::{AttributeTemplate, Features};
88
use rustc_hir::attrs::AttributeKind;
99
use rustc_hir::lints::AttributeLintKind;
@@ -17,7 +17,7 @@ use crate::early_parsed::{EARLY_PARSED_ATTRIBUTES, EarlyParsedState};
1717
use crate::errors::{InvalidAttrAtCrateLevel, ItemFollowingInnerAttr};
1818
use crate::parser::{ArgParser, PathParser, RefPathParser};
1919
use crate::session_diagnostics::ParsedDescription;
20-
use crate::{Early, Late, OmitDoc, ShouldEmit};
20+
use crate::{Early, Late, OmitDoc, ShouldEmit, errors};
2121

2222
/// Context created once, for example as part of the ast lowering
2323
/// context, through which all attributes can be lowered.
@@ -440,6 +440,12 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
440440
}
441441
}
442442

443+
if !matches!(self.stage.should_emit(), ShouldEmit::Nothing)
444+
&& target == Target::WherePredicate
445+
{
446+
self.check_invalid_where_predicate_attrs(attributes.iter());
447+
}
448+
443449
attributes
444450
}
445451

@@ -581,4 +587,31 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
581587
.ok()
582588
.flatten()
583589
}
590+
591+
fn check_invalid_where_predicate_attrs<'attr>(
592+
&self,
593+
attrs: impl IntoIterator<Item = &'attr Attribute>,
594+
) {
595+
// FIXME(where_clause_attrs): Currently, as the following check shows,
596+
// only `#[cfg]` and `#[cfg_attr]` are allowed, but it should be removed
597+
// if we allow more attributes (e.g., tool attributes and `allow/deny/warn`)
598+
// in where clauses. After that, this function would become useless.
599+
let spans = attrs
600+
.into_iter()
601+
// FIXME: We shouldn't need to special-case `doc`!
602+
.filter(|attr| {
603+
matches!(
604+
attr,
605+
Attribute::Parsed(AttributeKind::DocComment { .. } | AttributeKind::Doc(_))
606+
| Attribute::Unparsed(_)
607+
)
608+
})
609+
.map(|attr| attr.span())
610+
.collect::<Vec<_>>();
611+
if !spans.is_empty() {
612+
self.dcx().emit_err(errors::UnsupportedAttributesInWhere {
613+
span: MultiSpan::from_spans(spans),
614+
});
615+
}
616+
}
584617
}

compiler/rustc_passes/src/check_attr.rs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1924,27 +1924,6 @@ impl<'tcx> Visitor<'tcx> for CheckAttrVisitor<'tcx> {
19241924
}
19251925

19261926
fn visit_where_predicate(&mut self, where_predicate: &'tcx hir::WherePredicate<'tcx>) {
1927-
// FIXME(where_clause_attrs): Currently, as the following check shows,
1928-
// only `#[cfg]` and `#[cfg_attr]` are allowed, but it should be removed
1929-
// if we allow more attributes (e.g., tool attributes and `allow/deny/warn`)
1930-
// in where clauses. After that, only `self.check_attributes` should be enough.
1931-
let spans = self
1932-
.tcx
1933-
.hir_attrs(where_predicate.hir_id)
1934-
.iter()
1935-
// FIXME: We shouldn't need to special-case `doc`!
1936-
.filter(|attr| {
1937-
matches!(
1938-
attr,
1939-
Attribute::Parsed(AttributeKind::DocComment { .. } | AttributeKind::Doc(_))
1940-
| Attribute::Unparsed(_)
1941-
)
1942-
})
1943-
.map(|attr| attr.span())
1944-
.collect::<Vec<_>>();
1945-
if !spans.is_empty() {
1946-
self.tcx.dcx().emit_err(errors::UnsupportedAttributesInWhere { span: spans.into() });
1947-
}
19481927
self.check_attributes(
19491928
where_predicate.hir_id,
19501929
where_predicate.span,

compiler/rustc_passes/src/errors.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,14 +1164,6 @@ pub(crate) struct RustcConstStableIndirectPairing {
11641164
pub span: Span,
11651165
}
11661166

1167-
#[derive(Diagnostic)]
1168-
#[diag("most attributes are not supported in `where` clauses")]
1169-
#[help("only `#[cfg]` and `#[cfg_attr]` are supported")]
1170-
pub(crate) struct UnsupportedAttributesInWhere {
1171-
#[primary_span]
1172-
pub span: MultiSpan,
1173-
}
1174-
11751167
#[derive(Diagnostic)]
11761168
pub(crate) enum UnexportableItem<'a> {
11771169
#[diag("{$descr}'s are not exportable")]

tests/ui/where-clauses/cfg_attribute.a.stderr

Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -93,194 +93,194 @@ LL | #[rustfmt::skip] ():,
9393
= help: only `#[cfg]` and `#[cfg_attr]` are supported
9494

9595
error: most attributes are not supported in `where` clauses
96-
--> $DIR/cfg_attribute.rs:66:5
96+
--> $DIR/cfg_attribute.rs:43:9
9797
|
98-
LL | #[derive(Clone)] ():,
99-
| ^^^^^^^^^^^^^^^^
98+
LL | #[derive(Clone)] ():,
99+
| ^^^^^^^^^^^^^^^^
100100
|
101101
= help: only `#[cfg]` and `#[cfg_attr]` are supported
102102

103103
error: most attributes are not supported in `where` clauses
104-
--> $DIR/cfg_attribute.rs:69:5
104+
--> $DIR/cfg_attribute.rs:46:9
105105
|
106-
LL | #[rustfmt::skip] ():,
107-
| ^^^^^^^^^^^^^^^^
106+
LL | #[rustfmt::skip] ():;
107+
| ^^^^^^^^^^^^^^^^
108108
|
109109
= help: only `#[cfg]` and `#[cfg_attr]` are supported
110110

111111
error: most attributes are not supported in `where` clauses
112-
--> $DIR/cfg_attribute.rs:100:5
112+
--> $DIR/cfg_attribute.rs:54:9
113113
|
114-
LL | #[derive(Clone)] ():,
115-
| ^^^^^^^^^^^^^^^^
114+
LL | #[derive(Clone)] ():,
115+
| ^^^^^^^^^^^^^^^^
116116
|
117117
= help: only `#[cfg]` and `#[cfg_attr]` are supported
118118

119119
error: most attributes are not supported in `where` clauses
120-
--> $DIR/cfg_attribute.rs:103:5
120+
--> $DIR/cfg_attribute.rs:57:9
121121
|
122-
LL | #[rustfmt::skip] ():,
123-
| ^^^^^^^^^^^^^^^^
122+
LL | #[rustfmt::skip] ():;
123+
| ^^^^^^^^^^^^^^^^
124124
|
125125
= help: only `#[cfg]` and `#[cfg_attr]` are supported
126126

127127
error: most attributes are not supported in `where` clauses
128-
--> $DIR/cfg_attribute.rs:114:5
128+
--> $DIR/cfg_attribute.rs:66:5
129129
|
130130
LL | #[derive(Clone)] ():,
131131
| ^^^^^^^^^^^^^^^^
132132
|
133133
= help: only `#[cfg]` and `#[cfg_attr]` are supported
134134

135135
error: most attributes are not supported in `where` clauses
136-
--> $DIR/cfg_attribute.rs:117:5
136+
--> $DIR/cfg_attribute.rs:69:5
137137
|
138138
LL | #[rustfmt::skip] ():,
139139
| ^^^^^^^^^^^^^^^^
140140
|
141141
= help: only `#[cfg]` and `#[cfg_attr]` are supported
142142

143143
error: most attributes are not supported in `where` clauses
144-
--> $DIR/cfg_attribute.rs:129:5
144+
--> $DIR/cfg_attribute.rs:76:9
145145
|
146-
LL | #[derive(Clone)] ():,
147-
| ^^^^^^^^^^^^^^^^
146+
LL | #[derive(Clone)] ():,
147+
| ^^^^^^^^^^^^^^^^
148148
|
149149
= help: only `#[cfg]` and `#[cfg_attr]` are supported
150150

151151
error: most attributes are not supported in `where` clauses
152-
--> $DIR/cfg_attribute.rs:132:5
152+
--> $DIR/cfg_attribute.rs:79:9
153153
|
154-
LL | #[rustfmt::skip] ():,
155-
| ^^^^^^^^^^^^^^^^
154+
LL | #[rustfmt::skip] ():;
155+
| ^^^^^^^^^^^^^^^^
156156
|
157157
= help: only `#[cfg]` and `#[cfg_attr]` are supported
158158

159159
error: most attributes are not supported in `where` clauses
160-
--> $DIR/cfg_attribute.rs:144:5
160+
--> $DIR/cfg_attribute.rs:87:9
161161
|
162-
LL | #[derive(Clone)] ():,
163-
| ^^^^^^^^^^^^^^^^
162+
LL | #[derive(Clone)] ():,
163+
| ^^^^^^^^^^^^^^^^
164164
|
165165
= help: only `#[cfg]` and `#[cfg_attr]` are supported
166166

167167
error: most attributes are not supported in `where` clauses
168-
--> $DIR/cfg_attribute.rs:147:5
168+
--> $DIR/cfg_attribute.rs:90:9
169169
|
170-
LL | #[rustfmt::skip] ():,
171-
| ^^^^^^^^^^^^^^^^
170+
LL | #[rustfmt::skip] ():,
171+
| ^^^^^^^^^^^^^^^^
172172
|
173173
= help: only `#[cfg]` and `#[cfg_attr]` are supported
174174

175175
error: most attributes are not supported in `where` clauses
176-
--> $DIR/cfg_attribute.rs:155:5
176+
--> $DIR/cfg_attribute.rs:100:5
177177
|
178178
LL | #[derive(Clone)] ():,
179179
| ^^^^^^^^^^^^^^^^
180180
|
181181
= help: only `#[cfg]` and `#[cfg_attr]` are supported
182182

183183
error: most attributes are not supported in `where` clauses
184-
--> $DIR/cfg_attribute.rs:158:5
184+
--> $DIR/cfg_attribute.rs:103:5
185185
|
186186
LL | #[rustfmt::skip] ():,
187187
| ^^^^^^^^^^^^^^^^
188188
|
189189
= help: only `#[cfg]` and `#[cfg_attr]` are supported
190190

191191
error: most attributes are not supported in `where` clauses
192-
--> $DIR/cfg_attribute.rs:178:5
192+
--> $DIR/cfg_attribute.rs:114:5
193193
|
194194
LL | #[derive(Clone)] ():,
195195
| ^^^^^^^^^^^^^^^^
196196
|
197197
= help: only `#[cfg]` and `#[cfg_attr]` are supported
198198

199199
error: most attributes are not supported in `where` clauses
200-
--> $DIR/cfg_attribute.rs:181:5
200+
--> $DIR/cfg_attribute.rs:117:5
201201
|
202202
LL | #[rustfmt::skip] ():,
203203
| ^^^^^^^^^^^^^^^^
204204
|
205205
= help: only `#[cfg]` and `#[cfg_attr]` are supported
206206

207207
error: most attributes are not supported in `where` clauses
208-
--> $DIR/cfg_attribute.rs:43:9
208+
--> $DIR/cfg_attribute.rs:129:5
209209
|
210-
LL | #[derive(Clone)] ():,
211-
| ^^^^^^^^^^^^^^^^
210+
LL | #[derive(Clone)] ():,
211+
| ^^^^^^^^^^^^^^^^
212212
|
213213
= help: only `#[cfg]` and `#[cfg_attr]` are supported
214214

215215
error: most attributes are not supported in `where` clauses
216-
--> $DIR/cfg_attribute.rs:46:9
216+
--> $DIR/cfg_attribute.rs:132:5
217217
|
218-
LL | #[rustfmt::skip] ():;
219-
| ^^^^^^^^^^^^^^^^
218+
LL | #[rustfmt::skip] ():,
219+
| ^^^^^^^^^^^^^^^^
220220
|
221221
= help: only `#[cfg]` and `#[cfg_attr]` are supported
222222

223223
error: most attributes are not supported in `where` clauses
224-
--> $DIR/cfg_attribute.rs:54:9
224+
--> $DIR/cfg_attribute.rs:144:5
225225
|
226-
LL | #[derive(Clone)] ():,
227-
| ^^^^^^^^^^^^^^^^
226+
LL | #[derive(Clone)] ():,
227+
| ^^^^^^^^^^^^^^^^
228228
|
229229
= help: only `#[cfg]` and `#[cfg_attr]` are supported
230230

231231
error: most attributes are not supported in `where` clauses
232-
--> $DIR/cfg_attribute.rs:57:9
232+
--> $DIR/cfg_attribute.rs:147:5
233233
|
234-
LL | #[rustfmt::skip] ():;
235-
| ^^^^^^^^^^^^^^^^
234+
LL | #[rustfmt::skip] ():,
235+
| ^^^^^^^^^^^^^^^^
236236
|
237237
= help: only `#[cfg]` and `#[cfg_attr]` are supported
238238

239239
error: most attributes are not supported in `where` clauses
240-
--> $DIR/cfg_attribute.rs:76:9
240+
--> $DIR/cfg_attribute.rs:155:5
241241
|
242-
LL | #[derive(Clone)] ():,
243-
| ^^^^^^^^^^^^^^^^
242+
LL | #[derive(Clone)] ():,
243+
| ^^^^^^^^^^^^^^^^
244244
|
245245
= help: only `#[cfg]` and `#[cfg_attr]` are supported
246246

247247
error: most attributes are not supported in `where` clauses
248-
--> $DIR/cfg_attribute.rs:79:9
248+
--> $DIR/cfg_attribute.rs:158:5
249249
|
250-
LL | #[rustfmt::skip] ():;
251-
| ^^^^^^^^^^^^^^^^
250+
LL | #[rustfmt::skip] ():,
251+
| ^^^^^^^^^^^^^^^^
252252
|
253253
= help: only `#[cfg]` and `#[cfg_attr]` are supported
254254

255255
error: most attributes are not supported in `where` clauses
256-
--> $DIR/cfg_attribute.rs:87:9
256+
--> $DIR/cfg_attribute.rs:165:9
257257
|
258258
LL | #[derive(Clone)] ():,
259259
| ^^^^^^^^^^^^^^^^
260260
|
261261
= help: only `#[cfg]` and `#[cfg_attr]` are supported
262262

263263
error: most attributes are not supported in `where` clauses
264-
--> $DIR/cfg_attribute.rs:90:9
264+
--> $DIR/cfg_attribute.rs:168:9
265265
|
266266
LL | #[rustfmt::skip] ():,
267267
| ^^^^^^^^^^^^^^^^
268268
|
269269
= help: only `#[cfg]` and `#[cfg_attr]` are supported
270270

271271
error: most attributes are not supported in `where` clauses
272-
--> $DIR/cfg_attribute.rs:165:9
272+
--> $DIR/cfg_attribute.rs:178:5
273273
|
274-
LL | #[derive(Clone)] ():,
275-
| ^^^^^^^^^^^^^^^^
274+
LL | #[derive(Clone)] ():,
275+
| ^^^^^^^^^^^^^^^^
276276
|
277277
= help: only `#[cfg]` and `#[cfg_attr]` are supported
278278

279279
error: most attributes are not supported in `where` clauses
280-
--> $DIR/cfg_attribute.rs:168:9
280+
--> $DIR/cfg_attribute.rs:181:5
281281
|
282-
LL | #[rustfmt::skip] ():,
283-
| ^^^^^^^^^^^^^^^^
282+
LL | #[rustfmt::skip] ():,
283+
| ^^^^^^^^^^^^^^^^
284284
|
285285
= help: only `#[cfg]` and `#[cfg_attr]` are supported
286286

0 commit comments

Comments
 (0)