Skip to content

Commit 0b58bb3

Browse files
committed
Support cfg and cfg_attr on generic parameters
1 parent 605ea9d commit 0b58bb3

File tree

6 files changed

+111
-106
lines changed

6 files changed

+111
-106
lines changed

src/libsyntax/config.rs

+4-16
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,10 @@ impl<'a> StripUnconfigured<'a> {
240240
items.flat_map_in_place(|item| self.configure(item));
241241
}
242242

243+
pub fn configure_generic_params(&mut self, params: &mut Vec<ast::GenericParam>) {
244+
params.flat_map_in_place(|param| self.configure(param));
245+
}
246+
243247
fn configure_variant_data(&mut self, vdata: &mut ast::VariantData) {
244248
match vdata {
245249
ast::VariantData::Struct(fields, ..) | ast::VariantData::Tuple(fields, _) =>
@@ -301,22 +305,6 @@ impl<'a> StripUnconfigured<'a> {
301305
pub fn configure_fn_decl(&mut self, fn_decl: &mut ast::FnDecl) {
302306
fn_decl.inputs.flat_map_in_place(|arg| self.configure(arg));
303307
}
304-
305-
/// Denies `#[cfg]` on generic parameters until we decide what to do with it.
306-
/// See issue #51279.
307-
pub fn disallow_cfg_on_generic_param(&mut self, param: &ast::GenericParam) {
308-
for attr in param.attrs() {
309-
let offending_attr = if attr.check_name(sym::cfg) {
310-
"cfg"
311-
} else if attr.check_name(sym::cfg_attr) {
312-
"cfg_attr"
313-
} else {
314-
continue;
315-
};
316-
let msg = format!("#[{}] cannot be applied on a generic parameter", offending_attr);
317-
self.sess.span_diagnostic.span_err(attr.span, &msg);
318-
}
319-
}
320308
}
321309

322310
impl<'a> MutVisitor for StripUnconfigured<'a> {

src/libsyntax/ext/expand.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1439,9 +1439,9 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
14391439
}
14401440
}
14411441

1442-
fn visit_generic_param(&mut self, param: &mut ast::GenericParam) {
1443-
self.cfg.disallow_cfg_on_generic_param(&param);
1444-
noop_visit_generic_param(param, self)
1442+
fn visit_generic_params(&mut self, params: &mut Vec<ast::GenericParam>) {
1443+
self.cfg.configure_generic_params(params);
1444+
noop_visit_generic_params(params, self);
14451445
}
14461446

14471447
fn visit_attribute(&mut self, at: &mut ast::Attribute) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// compile-flags:--cfg yes
2+
3+
fn f_lt<#[cfg(yes)] 'a: 'a, #[cfg(no)] T>() {}
4+
fn f_ty<#[cfg(no)] 'a: 'a, #[cfg(yes)] T>() {}
5+
6+
type FnGood = for<#[cfg(yes)] 'a, #[cfg(no)] T> fn(); // OK
7+
type FnBad = for<#[cfg(no)] 'a, #[cfg(yes)] T> fn();
8+
//~^ ERROR only lifetime parameters can be used in this context
9+
10+
type PolyGood = dyn for<#[cfg(yes)] 'a, #[cfg(no)] T> Copy; // OK
11+
type PolyBad = dyn for<#[cfg(no)] 'a, #[cfg(yes)] T> Copy;
12+
//~^ ERROR only lifetime parameters can be used in this context
13+
14+
struct WhereGood where for<#[cfg(yes)] 'a, #[cfg(no)] T> u8: Copy; // OK
15+
struct WhereBad where for<#[cfg(no)] 'a, #[cfg(yes)] T> u8: Copy;
16+
//~^ ERROR only lifetime parameters can be used in this context
17+
18+
fn f_lt_no<#[cfg_attr(no, unknown)] 'a>() {} // OK
19+
fn f_lt_yes<#[cfg_attr(yes, unknown)] 'a>() {} //~ ERROR attribute `unknown` is currently unknown
20+
fn f_ty_no<#[cfg_attr(no, unknown)] T>() {} // OK
21+
fn f_ty_yes<#[cfg_attr(yes, unknown)] T>() {} //~ ERROR attribute `unknown` is currently unknown
22+
23+
type FnNo = for<#[cfg_attr(no, unknown)] 'a> fn(); // OK
24+
type FnYes = for<#[cfg_attr(yes, unknown)] 'a> fn();
25+
//~^ ERROR attribute `unknown` is currently unknown
26+
27+
type PolyNo = dyn for<#[cfg_attr(no, unknown)] 'a> Copy; // OK
28+
type PolyYes = dyn for<#[cfg_attr(yes, unknown)] 'a> Copy;
29+
//~^ ERROR attribute `unknown` is currently unknown
30+
31+
struct WhereNo where for<#[cfg_attr(no, unknown)] 'a> u8: Copy; // OK
32+
struct WhereYes where for<#[cfg_attr(yes, unknown)] 'a> u8: Copy;
33+
//~^ ERROR attribute `unknown` is currently unknown
34+
35+
fn main() {
36+
f_lt::<'static>();
37+
f_ty::<u8>();
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
error: only lifetime parameters can be used in this context
2+
--> $DIR/cfg-generic-params.rs:7:45
3+
|
4+
LL | type FnBad = for<#[cfg(no)] 'a, #[cfg(yes)] T> fn();
5+
| ^
6+
7+
error: only lifetime parameters can be used in this context
8+
--> $DIR/cfg-generic-params.rs:11:51
9+
|
10+
LL | type PolyBad = dyn for<#[cfg(no)] 'a, #[cfg(yes)] T> Copy;
11+
| ^
12+
13+
error: only lifetime parameters can be used in this context
14+
--> $DIR/cfg-generic-params.rs:15:54
15+
|
16+
LL | struct WhereBad where for<#[cfg(no)] 'a, #[cfg(yes)] T> u8: Copy;
17+
| ^
18+
19+
error[E0658]: The attribute `unknown` is currently unknown to the compiler and may have meaning added to it in the future
20+
--> $DIR/cfg-generic-params.rs:19:29
21+
|
22+
LL | fn f_lt_yes<#[cfg_attr(yes, unknown)] 'a>() {}
23+
| ^^^^^^^
24+
|
25+
= note: for more information, see https://github.com/rust-lang/rust/issues/29642
26+
= help: add #![feature(custom_attribute)] to the crate attributes to enable
27+
28+
error[E0658]: The attribute `unknown` is currently unknown to the compiler and may have meaning added to it in the future
29+
--> $DIR/cfg-generic-params.rs:21:29
30+
|
31+
LL | fn f_ty_yes<#[cfg_attr(yes, unknown)] T>() {}
32+
| ^^^^^^^
33+
|
34+
= note: for more information, see https://github.com/rust-lang/rust/issues/29642
35+
= help: add #![feature(custom_attribute)] to the crate attributes to enable
36+
37+
error[E0658]: The attribute `unknown` is currently unknown to the compiler and may have meaning added to it in the future
38+
--> $DIR/cfg-generic-params.rs:24:34
39+
|
40+
LL | type FnYes = for<#[cfg_attr(yes, unknown)] 'a> fn();
41+
| ^^^^^^^
42+
|
43+
= note: for more information, see https://github.com/rust-lang/rust/issues/29642
44+
= help: add #![feature(custom_attribute)] to the crate attributes to enable
45+
46+
error[E0658]: The attribute `unknown` is currently unknown to the compiler and may have meaning added to it in the future
47+
--> $DIR/cfg-generic-params.rs:28:40
48+
|
49+
LL | type PolyYes = dyn for<#[cfg_attr(yes, unknown)] 'a> Copy;
50+
| ^^^^^^^
51+
|
52+
= note: for more information, see https://github.com/rust-lang/rust/issues/29642
53+
= help: add #![feature(custom_attribute)] to the crate attributes to enable
54+
55+
error[E0658]: The attribute `unknown` is currently unknown to the compiler and may have meaning added to it in the future
56+
--> $DIR/cfg-generic-params.rs:32:43
57+
|
58+
LL | struct WhereYes where for<#[cfg_attr(yes, unknown)] 'a> u8: Copy;
59+
| ^^^^^^^
60+
|
61+
= note: for more information, see https://github.com/rust-lang/rust/issues/29642
62+
= help: add #![feature(custom_attribute)] to the crate attributes to enable
63+
64+
error: aborting due to 8 previous errors
65+
66+
For more information about this error, try `rustc --explain E0658`.

src/test/ui/issues/issue-51279.rs

-27
This file was deleted.

src/test/ui/issues/issue-51279.stderr

-60
This file was deleted.

0 commit comments

Comments
 (0)