1 // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 // This test enumerates as many compiler-builtin ungated attributes as
12 // possible (that is, all the mutually compatible ones), and checks
13 // that we get "expected" (*) warnings for each in the various weird
14 // places that users might put them in the syntax.
16 // (*): The word "expected" is in quotes above because the cases where
17 // warnings are and are not emitted might not match a user's intuition
18 // nor the rustc developers' intent. I am really just trying to
19 // capture today's behavior in a test, not so that it become enshrined
20 // as the absolute behavior going forward, but rather so that we do
21 // not change the behavior in the future without even being *aware* of
22 // the change when it happens.
24 // At the time of authoring, the attributes here are listed in the
25 // order that they occur in libsyntax/feature_gate.rs.
27 // Any builtin attributes that:
29 // - are not stable, or
31 // - could not be included here covering the same cases as the other
32 // attributes without raising an *error* from rustc (note though
33 // that warnings are of course expected)
35 // have their own test case referenced by filename in an inline
38 // The test feeds numeric inputs to each attribute that accepts them
39 // without error. We do this for two reasons: (1.) to exercise how
40 // inputs are handled by each, and (2.) to ease searching for related
41 // occurrences in the source text.
43 #![feature(rustc_attrs)] // For `rustc_error`; see note below.
44 #![warn(unused_attributes, unknown_lints)]
46 #![allow(stable_features)]
48 // UNGATED WHITE-LISTED BUILT-IN ATTRIBUTES
50 #![warn (x5400)] //~ WARN unknown lint: `x5400`
51 #![allow (x5300)] //~ WARN unknown lint: `x5300`
52 #![forbid (x5200)] //~ WARN unknown lint: `x5200`
53 #![deny (x5100)] //~ WARN unknown lint: `x5100`
54 #![macro_use] // (allowed if no argument; see issue-43160-gating-of-macro_use.rs)
55 #![macro_export = "4800"] //~ WARN unused attribute
56 #![plugin_registrar = "4700"] //~ WARN unused attribute
57 // skipping testing of cfg
58 // skipping testing of cfg_attr
59 #![main = "x4400"] //~ WARN unused attribute
60 #![start = "x4300"] //~ WARN unused attribute
61 // see issue-43106-gating-of-test.rs for crate-level; but non crate-level is below at "4200"
62 // see issue-43106-gating-of-bench.rs for crate-level; but non crate-level is below at "4100"
64 //~^ WARN unused attribute
65 //~| WARN `repr` attribute isn't configurable with a literal
66 #![path = "3800"] //~ WARN unused attribute
67 #![abi = "3700"] //~ WARN unused attribute
68 #![automatically_derived = "3600"] //~ WARN unused attribute
69 #![no_mangle = "3500"]
70 #![no_link = "3400"] //~ WARN unused attribute
71 // see issue-43106-gating-of-derive.rs
72 #![should_panic = "3200"] //~ WARN unused attribute
73 #![ignore = "3100"] //~ WARN unused attribute
74 #![no_implicit_prelude = "3000"]
75 #![reexport_test_harness_main = "2900"]
76 // see gated-link-args.rs
77 // see issue-43106-gating-of-macro_escape.rs for crate-level; but non crate-level is below at "2700"
78 // (cannot easily test gating of crate-level #[no_std]; but non crate-level is below at "2600")
79 #![proc_macro_derive = "2500"] //~ WARN unused attribute
82 #![export_name = "2200"]
83 // see issue-43106-gating-of-inline.rs
85 #![link_name = "1900"]
86 #![link_section = "1800"]
87 #![no_builtins = "1700"] // Yikes, dupe'd on BUILTIN_ATTRIBUTES list (see "0300")
88 #![no_mangle = "1600"] // Yikes, dupe'd on BUILTIN_ATTRIBUTES list (see "3500")
89 // see issue-43106-gating-of-rustc_deprecated.rs
91 // see issue-43106-gating-of-stable.rs
92 // see issue-43106-gating-of-unstable.rs
93 // see issue-43106-gating-of-deprecated.rs
94 #![windows_subsystem = "1000"]
96 // UNGATED CRATE-LEVEL BUILT-IN ATTRIBUTES
98 #![crate_name = "0900"]
99 #![crate_type = "bin"] // cannot pass "0800" here
101 // For #![crate_id], see issue #43142. (I cannot bear to enshrine current behavior in a test)
103 // FIXME(#44232) we should warn that this isn't used.
106 // For #![no_start], see issue #43144. (I cannot bear to enshrine current behavior in a test)
108 // (cannot easily gating state of crate-level #[no_main]; but non crate-level is below at "0400")
109 #![no_builtins = "0300"]
110 #![recursion_limit = "0200"]
111 #![type_length_limit = "0100"]
113 // USES OF BUILT-IN ATTRIBUTES IN OTHER ("UNUSUAL") PLACES
116 //~^ WARN unknown lint: `x5400`
118 mod inner { #![warn(x5400)] }
119 //~^ WARN unknown lint: `x5400`
121 #[warn(x5400)] fn f() { }
122 //~^ WARN unknown lint: `x5400`
124 #[warn(x5400)] struct S;
125 //~^ WARN unknown lint: `x5400`
127 #[warn(x5400)] type T = S;
128 //~^ WARN unknown lint: `x5400`
130 #[warn(x5400)] impl S { }
131 //~^ WARN unknown lint: `x5400`
135 //~^ WARN unknown lint: `x5300`
137 mod inner { #![allow(x5300)] }
138 //~^ WARN unknown lint: `x5300`
140 #[allow(x5300)] fn f() { }
141 //~^ WARN unknown lint: `x5300`
143 #[allow(x5300)] struct S;
144 //~^ WARN unknown lint: `x5300`
146 #[allow(x5300)] type T = S;
147 //~^ WARN unknown lint: `x5300`
149 #[allow(x5300)] impl S { }
150 //~^ WARN unknown lint: `x5300`
154 //~^ WARN unknown lint: `x5200`
156 mod inner { #![forbid(x5200)] }
157 //~^ WARN unknown lint: `x5200`
159 #[forbid(x5200)] fn f() { }
160 //~^ WARN unknown lint: `x5200`
162 #[forbid(x5200)] struct S;
163 //~^ WARN unknown lint: `x5200`
165 #[forbid(x5200)] type T = S;
166 //~^ WARN unknown lint: `x5200`
168 #[forbid(x5200)] impl S { }
169 //~^ WARN unknown lint: `x5200`
173 //~^ WARN unknown lint: `x5100`
175 mod inner { #![deny(x5100)] }
176 //~^ WARN unknown lint: `x5100`
178 #[deny(x5100)] fn f() { }
179 //~^ WARN unknown lint: `x5100`
181 #[deny(x5100)] struct S;
182 //~^ WARN unknown lint: `x5100`
184 #[deny(x5100)] type T = S;
185 //~^ WARN unknown lint: `x5100`
187 #[deny(x5100)] impl S { }
188 //~^ WARN unknown lint: `x5100`
193 mod inner { #![macro_use] }
195 #[macro_use] fn f() { }
196 //~^ WARN unused attribute
198 #[macro_use] struct S;
199 //~^ WARN unused attribute
201 #[macro_use] type T = S;
202 //~^ WARN unused attribute
204 #[macro_use] impl S { }
205 //~^ WARN unused attribute
208 #[macro_export = "4800"]
209 //~^ WARN unused attribute
211 mod inner { #![macro_export="4800"] }
212 //~^ WARN unused attribute
214 #[macro_export = "4800"] fn f() { }
215 //~^ WARN unused attribute
217 #[macro_export = "4800"] struct S;
218 //~^ WARN unused attribute
220 #[macro_export = "4800"] type T = S;
221 //~^ WARN unused attribute
223 #[macro_export = "4800"] impl S { }
224 //~^ WARN unused attribute
227 #[plugin_registrar = "4700"]
228 //~^ WARN unused attribute
229 mod plugin_registrar
{
230 mod inner { #![plugin_registrar="4700"] }
231 //~^ WARN unused attribute
233 // for `fn f()` case, see gated-plugin_registrar.rs
235 #[plugin_registrar = "4700"] struct S;
236 //~^ WARN unused attribute
238 #[plugin_registrar = "4700"] type T = S;
239 //~^ WARN unused attribute
241 #[plugin_registrar = "4700"] impl S { }
242 //~^ WARN unused attribute
246 //~^ WARN unused attribute
248 mod inner { #![main="4300"] }
249 //~^ WARN unused attribute
251 // for `fn f()` case, see feature-gate-main.rs
253 #[main = "4400"] struct S;
254 //~^ WARN unused attribute
256 #[main = "4400"] type T = S;
257 //~^ WARN unused attribute
259 #[main = "4400"] impl S { }
260 //~^ WARN unused attribute
264 //~^ WARN unused attribute
266 mod inner { #![start="4300"] }
267 //~^ WARN unused attribute
269 // for `fn f()` case, see feature-gate-start.rs
271 #[start = "4300"] struct S;
272 //~^ WARN unused attribute
274 #[start = "4300"] type T = S;
275 //~^ WARN unused attribute
277 #[start = "4300"] impl S { }
278 //~^ WARN unused attribute
281 // At time of unit test authorship, if compiling without `--test` then
282 // non-crate-level #[test] attributes seem to be ignored.
285 mod test { mod inner { #![test="4200"] }
296 // At time of unit test authorship, if compiling without `--test` then
297 // non-crate-level #[bench] attributes seem to be ignored.
301 mod inner { #![bench="4100"] }
314 //~^ WARN unused attribute
315 //~| WARN `repr` attribute isn't configurable with a literal
317 mod inner { #![repr="3900"] }
318 //~^ WARN unused attribute
319 //~| WARN `repr` attribute isn't configurable with a literal
321 #[repr = "3900"] fn f() { }
322 //~^ WARN unused attribute
323 //~| WARN `repr` attribute isn't configurable with a literal
327 #[repr = "3900"] type T = S;
328 //~^ WARN unused attribute
329 //~| WARN `repr` attribute isn't configurable with a literal
331 #[repr = "3900"] impl S { }
332 //~^ WARN unused attribute
333 //~| WARN `repr` attribute isn't configurable with a literal
338 mod inner { #![path="3800"] }
340 #[path = "3800"] fn f() { }
341 //~^ WARN unused attribute
343 #[path = "3800"] struct S;
344 //~^ WARN unused attribute
346 #[path = "3800"] type T = S;
347 //~^ WARN unused attribute
349 #[path = "3800"] impl S { }
350 //~^ WARN unused attribute
354 //~^ WARN unused attribute
356 mod inner { #![abi="3700"] }
357 //~^ WARN unused attribute
359 #[abi = "3700"] fn f() { }
360 //~^ WARN unused attribute
362 #[abi = "3700"] struct S;
363 //~^ WARN unused attribute
365 #[abi = "3700"] type T = S;
366 //~^ WARN unused attribute
368 #[abi = "3700"] impl S { }
369 //~^ WARN unused attribute
372 #[automatically_derived = "3600"]
373 //~^ WARN unused attribute
374 mod automatically_derived
{
375 mod inner { #![automatically_derived="3600"] }
376 //~^ WARN unused attribute
378 #[automatically_derived = "3600"] fn f() { }
379 //~^ WARN unused attribute
381 #[automatically_derived = "3600"] struct S;
382 //~^ WARN unused attribute
384 #[automatically_derived = "3600"] type T = S;
385 //~^ WARN unused attribute
387 #[automatically_derived = "3600"] impl S { }
388 //~^ WARN unused attribute
391 #[no_mangle = "3500"]
393 mod inner { #![no_mangle="3500"] }
395 #[no_mangle = "3500"] fn f() { }
397 #[no_mangle = "3500"] struct S;
399 #[no_mangle = "3500"] type T = S;
401 #[no_mangle = "3500"] impl S { }
405 //~^ WARN unused attribute
407 mod inner { #![no_link="3400"] }
408 //~^ WARN unused attribute
410 #[no_link = "3400"] fn f() { }
411 //~^ WARN unused attribute
413 #[no_link = "3400"] struct S;
414 //~^ WARN unused attribute
416 #[no_link = "3400"]type T = S;
417 //~^ WARN unused attribute
419 #[no_link = "3400"] impl S { }
420 //~^ WARN unused attribute
423 #[should_panic = "3200"]
424 //~^ WARN unused attribute
426 mod inner { #![should_panic="3200"] }
427 //~^ WARN unused attribute
429 #[should_panic = "3200"] fn f() { }
430 //~^ WARN unused attribute
432 #[should_panic = "3200"] struct S;
433 //~^ WARN unused attribute
435 #[should_panic = "3200"] type T = S;
436 //~^ WARN unused attribute
438 #[should_panic = "3200"] impl S { }
439 //~^ WARN unused attribute
443 //~^ WARN unused attribute
445 mod inner { #![ignore="3100"] }
446 //~^ WARN unused attribute
448 #[ignore = "3100"] fn f() { }
449 //~^ WARN unused attribute
451 #[ignore = "3100"] struct S;
452 //~^ WARN unused attribute
454 #[ignore = "3100"] type T = S;
455 //~^ WARN unused attribute
457 #[ignore = "3100"] impl S { }
458 //~^ WARN unused attribute
461 #[no_implicit_prelude = "3000"]
462 //~^ WARN unused attribute
463 mod no_implicit_prelude
{
464 mod inner { #![no_implicit_prelude="3000"] }
465 //~^ WARN unused attribute
467 #[no_implicit_prelude = "3000"] fn f() { }
468 //~^ WARN unused attribute
470 #[no_implicit_prelude = "3000"] struct S;
471 //~^ WARN unused attribute
473 #[no_implicit_prelude = "3000"] type T = S;
474 //~^ WARN unused attribute
476 #[no_implicit_prelude = "3000"] impl S { }
477 //~^ WARN unused attribute
480 #[reexport_test_harness_main = "2900"]
481 //~^ WARN unused attribute
482 mod reexport_test_harness_main
{
483 mod inner { #![reexport_test_harness_main="2900"] }
484 //~^ WARN unused attribute
486 #[reexport_test_harness_main = "2900"] fn f() { }
487 //~^ WARN unused attribute
489 #[reexport_test_harness_main = "2900"] struct S;
490 //~^ WARN unused attribute
492 #[reexport_test_harness_main = "2900"] type T = S;
493 //~^ WARN unused attribute
495 #[reexport_test_harness_main = "2900"] impl S { }
496 //~^ WARN unused attribute
499 // Cannot feed "2700" to `#[macro_escape]` without signaling an error.
501 //~^ WARN macro_escape is a deprecated synonym for macro_use
503 mod inner { #![macro_escape] }
504 //~^ WARN macro_escape is a deprecated synonym for macro_use
506 #[macro_escape] fn f() { }
507 //~^ WARN unused attribute
509 #[macro_escape] struct S;
510 //~^ WARN unused attribute
512 #[macro_escape] type T = S;
513 //~^ WARN unused attribute
515 #[macro_escape] impl S { }
516 //~^ WARN unused attribute
520 //~^ WARN unused attribute
521 //~| WARN crate-level attribute should be an inner attribute
523 mod inner { #![no_std="2600"] }
524 //~^ WARN unused attribute
525 //~| WARN crate-level attribute should be in the root module
527 #[no_std = "2600"] fn f() { }
528 //~^ WARN unused attribute
529 //~| WARN crate-level attribute should be an inner attribute
531 #[no_std = "2600"] struct S;
532 //~^ WARN unused attribute
533 //~| WARN crate-level attribute should be an inner attribute
535 #[no_std = "2600"] type T = S;
536 //~^ WARN unused attribute
537 //~| WARN crate-level attribute should be an inner attribute
539 #[no_std = "2600"] impl S { }
540 //~^ WARN unused attribute
541 //~| WARN crate-level attribute should be an inner attribute
544 // At time of authorship, #[proc_macro_derive = "2500"] signals error
545 // when it occurs on a mod (apart from crate-level). Therefore it goes
546 // into its own file; see issue-43106-gating-of-proc_macro_derive.rs
550 mod inner { #![doc="2400"] }
552 #[doc = "2400"] fn f() { }
554 #[doc = "2400"] struct S;
556 #[doc = "2400"] type T = S;
558 #[doc = "2400"] impl S { }
563 mod inner { #![cold="2300"] }
565 #[cold = "2300"] fn f() { }
567 #[cold = "2300"] struct S;
569 #[cold = "2300"] type T = S;
571 #[cold = "2300"] impl S { }
574 #[export_name = "2200"]
576 mod inner { #![export_name="2200"] }
578 #[export_name = "2200"] fn f() { }
580 #[export_name = "2200"] struct S;
582 #[export_name = "2200"] type T = S;
584 #[export_name = "2200"] impl S { }
587 // Note that this test ends with a `#[rustc_error] fn main()`, so it
588 // will never invoke the linker. These are here nonetheless to point
589 // out that we allow them at non-crate-level (though I do not know
590 // whether they have the same effect here as at crate-level).
594 mod inner { #![link="2000"] }
596 #[link = "2000"] fn f() { }
598 #[link = "2000"] struct S;
600 #[link = "2000"] type T = S;
602 #[link = "2000"] impl S { }
605 #[link_name = "1900"]
607 mod inner { #![link_name="1900"] }
609 #[link_name = "1900"] fn f() { }
611 #[link_name = "1900"] struct S;
613 #[link_name = "1900"] type T = S;
615 #[link_name = "1900"] impl S { }
618 #[link_section = "1800"]
620 mod inner { #![link_section="1800"] }
622 #[link_section = "1800"] fn f() { }
624 #[link_section = "1800"] struct S;
626 #[link_section = "1800"] type T = S;
628 #[link_section = "1800"] impl S { }
631 struct StructForDeprecated
;
633 #[deprecated = "1500"]
635 mod inner { #![deprecated="1500"] }
637 #[deprecated = "1500"] fn f() { }
639 #[deprecated = "1500"] struct S1;
641 #[deprecated = "1500"] type T = super::StructForDeprecated;
643 #[deprecated = "1500"] impl super::StructForDeprecated { }
648 mod inner { #![must_use="1400"] }
650 #[must_use = "1400"] fn f() { }
652 #[must_use = "1400"] struct S;
654 #[must_use = "1400"] type T = S;
656 #[must_use = "1400"] impl S { }
659 #[windows_subsystem = "1000"]
660 mod windows_subsystem
{
661 mod inner { #![windows_subsystem="1000"] }
663 #[windows_subsystem = "1000"] fn f() { }
665 #[windows_subsystem = "1000"] struct S;
667 #[windows_subsystem = "1000"] type T = S;
669 #[windows_subsystem = "1000"] impl S { }
672 // BROKEN USES OF CRATE-LEVEL BUILT-IN ATTRIBUTES
674 #[crate_name = "0900"]
675 //~^ WARN unused attribute
676 //~| WARN crate-level attribute should be an inner attribute
678 mod inner { #![crate_name="0900"] }
679 //~^ WARN unused attribute
680 //~| WARN crate-level attribute should be in the root module
682 #[crate_name = "0900"] fn f() { }
683 //~^ WARN unused attribute
684 //~| WARN crate-level attribute should be an inner attribute
686 #[crate_name = "0900"] struct S;
687 //~^ WARN unused attribute
688 //~| WARN crate-level attribute should be an inner attribute
690 #[crate_name = "0900"] type T = S;
691 //~^ WARN unused attribute
692 //~| WARN crate-level attribute should be an inner attribute
694 #[crate_name = "0900"] impl S { }
695 //~^ WARN unused attribute
696 //~| WARN crate-level attribute should be an inner attribute
699 #[crate_type = "0800"]
700 //~^ WARN unused attribute
701 //~| WARN crate-level attribute should be an inner attribute
703 mod inner { #![crate_type="0800"] }
704 //~^ WARN unused attribute
705 //~| WARN crate-level attribute should be in the root module
707 #[crate_type = "0800"] fn f() { }
708 //~^ WARN unused attribute
709 //~| WARN crate-level attribute should be an inner attribute
711 #[crate_type = "0800"] struct S;
712 //~^ WARN unused attribute
713 //~| WARN crate-level attribute should be an inner attribute
715 #[crate_type = "0800"] type T = S;
716 //~^ WARN unused attribute
717 //~| WARN crate-level attribute should be an inner attribute
719 #[crate_type = "0800"] impl S { }
720 //~^ WARN unused attribute
721 //~| WARN crate-level attribute should be an inner attribute
725 //~^ WARN unused attribute
726 //~| WARN crate-level attribute should be an inner attribute
728 mod inner { #![feature(x0600)] }
729 //~^ WARN unused attribute
730 //~| WARN crate-level attribute should be in the root module
732 #[feature(x0600)] fn f() { }
733 //~^ WARN unused attribute
734 //~| WARN crate-level attribute should be an inner attribute
736 #[feature(x0600)] struct S;
737 //~^ WARN unused attribute
738 //~| WARN crate-level attribute should be an inner attribute
740 #[feature(x0600)] type T = S;
741 //~^ WARN unused attribute
742 //~| WARN crate-level attribute should be an inner attribute
744 #[feature(x0600)] impl S { }
745 //~^ WARN unused attribute
746 //~| WARN crate-level attribute should be an inner attribute
751 //~^ WARN unused attribute
752 //~| WARN crate-level attribute should be an inner attribute
754 mod inner { #![no_main="0400"] }
755 //~^ WARN unused attribute
756 //~| WARN crate-level attribute should be in the root module
758 #[no_main = "0400"] fn f() { }
759 //~^ WARN unused attribute
760 //~| WARN crate-level attribute should be an inner attribute
762 #[no_main = "0400"] struct S;
763 //~^ WARN unused attribute
764 //~| WARN crate-level attribute should be an inner attribute
766 #[no_main = "0400"] type T = S;
767 //~^ WARN unused attribute
768 //~| WARN crate-level attribute should be an inner attribute
770 #[no_main = "0400"] impl S { }
771 //~^ WARN unused attribute
772 //~| WARN crate-level attribute should be an inner attribute
775 #[no_builtins = "0300"]
777 mod inner { #![no_builtins="0200"] }
779 #[no_builtins = "0300"] fn f() { }
781 #[no_builtins = "0300"] struct S;
783 #[no_builtins = "0300"] type T = S;
785 #[no_builtins = "0300"] impl S { }
788 #[recursion_limit="0200"]
789 //~^ WARN unused attribute
790 //~| WARN crate-level attribute should be an inner attribute
791 mod recursion_limit
{
792 mod inner { #![recursion_limit="0200"] }
793 //~^ WARN unused attribute
794 //~| WARN crate-level attribute should be in the root module
796 #[recursion_limit="0200"] fn f() { }
797 //~^ WARN unused attribute
798 //~| WARN crate-level attribute should be an inner attribute
800 #[recursion_limit="0200"] struct S;
801 //~^ WARN unused attribute
802 //~| WARN crate-level attribute should be an inner attribute
804 #[recursion_limit="0200"] type T = S;
805 //~^ WARN unused attribute
806 //~| WARN crate-level attribute should be an inner attribute
808 #[recursion_limit="0200"] impl S { }
809 //~^ WARN unused attribute
810 //~| WARN crate-level attribute should be an inner attribute
813 #[type_length_limit="0100"]
814 //~^ WARN unused attribute
815 //~| WARN crate-level attribute should be an inner attribute
816 mod type_length_limit
{
817 mod inner { #![type_length_limit="0100"] }
818 //~^ WARN unused attribute
819 //~| WARN crate-level attribute should be in the root module
821 #[type_length_limit="0100"] fn f() { }
822 //~^ WARN unused attribute
823 //~| WARN crate-level attribute should be an inner attribute
825 #[type_length_limit="0100"] struct S;
826 //~^ WARN unused attribute
827 //~| WARN crate-level attribute should be an inner attribute
829 #[type_length_limit="0100"] type T = S;
830 //~^ WARN unused attribute
831 //~| WARN crate-level attribute should be an inner attribute
833 #[type_length_limit="0100"] impl S { }
834 //~^ WARN unused attribute
835 //~| WARN crate-level attribute should be an inner attribute
838 // Since we expect for the mix of attributes used here to compile
839 // successfully, and we are just testing for the expected warnings of
840 // various (mis)uses of attributes, we use the `rustc_error` attribute
841 // on the `fn main()`.
844 fn main() { //~ ERROR compilation successful
845 println
!("Hello World");