Skip to content

Commit 3c2c533

Browse files
Rollup merge of #151321 - no_main-attr, r=JonathanBrouwer
Port #![no_main] to the attribute parser. Tracking issue: #131229 r? @JonathanBrouwer
2 parents 4df80b1 + ea77786 commit 3c2c533

8 files changed

Lines changed: 96 additions & 73 deletions

File tree

compiler/rustc_attr_parsing/src/attributes/crate_level.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,15 @@ impl<S: Stage> NoArgsAttributeParser<S> for NoStdParser {
136136
const CREATE: fn(Span) -> AttributeKind = AttributeKind::NoStd;
137137
}
138138

139+
pub(crate) struct NoMainParser;
140+
141+
impl<S: Stage> NoArgsAttributeParser<S> for NoMainParser {
142+
const PATH: &[Symbol] = &[sym::no_main];
143+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
144+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Crate)]);
145+
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::NoMain;
146+
}
147+
139148
pub(crate) struct RustcCoherenceIsCoreParser;
140149

141150
impl<S: Stage> NoArgsAttributeParser<S> for RustcCoherenceIsCoreParser {

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ use crate::attributes::codegen_attrs::{
2828
};
2929
use crate::attributes::confusables::ConfusablesParser;
3030
use crate::attributes::crate_level::{
31-
CrateNameParser, MoveSizeLimitParser, NoCoreParser, NoStdParser, PatternComplexityLimitParser,
32-
RecursionLimitParser, RustcCoherenceIsCoreParser, TypeLengthLimitParser,
33-
WindowsSubsystemParser,
31+
CrateNameParser, MoveSizeLimitParser, NoCoreParser, NoMainParser, NoStdParser,
32+
PatternComplexityLimitParser, RecursionLimitParser, RustcCoherenceIsCoreParser,
33+
TypeLengthLimitParser, WindowsSubsystemParser,
3434
};
3535
use crate::attributes::debugger::DebuggerViualizerParser;
3636
use crate::attributes::deprecation::DeprecationParser;
@@ -263,6 +263,7 @@ attribute_parsers!(
263263
Single<WithoutArgs<NoCoreParser>>,
264264
Single<WithoutArgs<NoImplicitPreludeParser>>,
265265
Single<WithoutArgs<NoLinkParser>>,
266+
Single<WithoutArgs<NoMainParser>>,
266267
Single<WithoutArgs<NoMangleParser>>,
267268
Single<WithoutArgs<NoStdParser>>,
268269
Single<WithoutArgs<NonExhaustiveParser>>,

compiler/rustc_hir/src/attrs/data_structures.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,9 @@ pub enum AttributeKind {
852852
/// Represents `#[no_link]`
853853
NoLink,
854854

855+
/// Represents `#[no_main]`
856+
NoMain,
857+
855858
/// Represents `#[no_mangle]`
856859
NoMangle(Span),
857860

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ impl AttributeKind {
7979
NoCore(..) => No,
8080
NoImplicitPrelude(..) => No,
8181
NoLink => No,
82+
NoMain => No,
8283
NoMangle(..) => Yes, // Needed for rustdoc
8384
NoStd(..) => No,
8485
NonExhaustive(..) => Yes, // Needed for rustdoc

compiler/rustc_passes/src/check_attr.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
297297
| AttributeKind::PatternComplexityLimit { .. }
298298
| AttributeKind::NoCore { .. }
299299
| AttributeKind::NoStd { .. }
300+
| AttributeKind::NoMain
300301
| AttributeKind::ObjcClass { .. }
301302
| AttributeKind::ObjcSelector { .. }
302303
| AttributeKind::RustcCoherenceIsCore(..)

compiler/rustc_passes/src/entry.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use rustc_ast::attr;
21
use rustc_ast::entry::EntryPointType;
32
use rustc_errors::codes::*;
43
use rustc_hir::attrs::AttributeKind;
@@ -7,7 +6,7 @@ use rustc_hir::{CRATE_HIR_ID, ItemId, Node, find_attr};
76
use rustc_middle::query::Providers;
87
use rustc_middle::ty::TyCtxt;
98
use rustc_session::config::{CrateType, EntryFnType, sigpipe};
10-
use rustc_span::{RemapPathScopeComponents, Span, sym};
9+
use rustc_span::{RemapPathScopeComponents, Span};
1110

1211
use crate::errors::{ExternMain, MultipleRustcMain, NoMainErr};
1312

@@ -30,7 +29,7 @@ fn entry_fn(tcx: TyCtxt<'_>, (): ()) -> Option<(DefId, EntryFnType)> {
3029
}
3130

3231
// If the user wants no main function at all, then stop here.
33-
if attr::contains_name(tcx.hir_attrs(CRATE_HIR_ID), sym::no_main) {
32+
if find_attr!(tcx.hir_attrs(CRATE_HIR_ID), AttributeKind::NoMain) {
3433
return None;
3534
}
3635

tests/ui/feature-gates/issue-43106-gating-of-builtin-attrs.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -884,26 +884,26 @@ mod feature {
884884

885885
#[no_main]
886886
//~^ WARN crate-level attribute should be an inner attribute
887-
//~| HELP add a `!`
888887
mod no_main_1 {
888+
//~^ NOTE: This attribute does not have an `!`, which means it is applied to this module
889889
mod inner { #![no_main] }
890-
//~^ WARN crate-level attribute should be in the root module
890+
//~^ WARN the `#![no_main]` attribute can only be used at the crate root
891891

892892
#[no_main] fn f() { }
893893
//~^ WARN crate-level attribute should be an inner attribute
894-
//~| HELP add a `!`
894+
//~| NOTE This attribute does not have an `!`, which means it is applied to this function
895895

896896
#[no_main] struct S;
897897
//~^ WARN crate-level attribute should be an inner attribute
898-
//~| HELP add a `!`
898+
//~| NOTE This attribute does not have an `!`, which means it is applied to this struct
899899

900900
#[no_main] type T = S;
901901
//~^ WARN crate-level attribute should be an inner attribute
902-
//~| HELP add a `!`
902+
//~| NOTE This attribute does not have an `!`, which means it is applied to this type alias
903903

904904
#[no_main] impl S { }
905905
//~^ WARN crate-level attribute should be an inner attribute
906-
//~| HELP add a `!`
906+
//~| NOTE This attribute does not have an `!`, which means it is applied to this implementation
907907
}
908908

909909
#[no_builtins]

tests/ui/feature-gates/issue-43106-gating-of-builtin-attrs.stderr

Lines changed: 70 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -240,17 +240,6 @@ help: add a `!`
240240
LL | #![feature(x0600)]
241241
| +
242242

243-
warning: crate-level attribute should be an inner attribute
244-
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:885:1
245-
|
246-
LL | #[no_main]
247-
| ^^^^^^^^^^
248-
|
249-
help: add a `!`
250-
|
251-
LL | #![no_main]
252-
| +
253-
254243
warning: crate-level attribute should be an inner attribute
255244
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:909:1
256245
|
@@ -476,56 +465,6 @@ help: add a `!`
476465
LL | #![feature(x0600)] impl S { }
477466
| +
478467

479-
warning: crate-level attribute should be in the root module
480-
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:889:17
481-
|
482-
LL | mod inner { #![no_main] }
483-
| ^^^^^^^^^^^
484-
485-
warning: crate-level attribute should be an inner attribute
486-
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:892:5
487-
|
488-
LL | #[no_main] fn f() { }
489-
| ^^^^^^^^^^
490-
|
491-
help: add a `!`
492-
|
493-
LL | #![no_main] fn f() { }
494-
| +
495-
496-
warning: crate-level attribute should be an inner attribute
497-
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:896:5
498-
|
499-
LL | #[no_main] struct S;
500-
| ^^^^^^^^^^
501-
|
502-
help: add a `!`
503-
|
504-
LL | #![no_main] struct S;
505-
| +
506-
507-
warning: crate-level attribute should be an inner attribute
508-
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:900:5
509-
|
510-
LL | #[no_main] type T = S;
511-
| ^^^^^^^^^^
512-
|
513-
help: add a `!`
514-
|
515-
LL | #![no_main] type T = S;
516-
| +
517-
518-
warning: crate-level attribute should be an inner attribute
519-
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:904:5
520-
|
521-
LL | #[no_main] impl S { }
522-
| ^^^^^^^^^^
523-
|
524-
help: add a `!`
525-
|
526-
LL | #![no_main] impl S { }
527-
| +
528-
529468
warning: crate-level attribute should be in the root module
530469
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:913:17
531470
|
@@ -1407,6 +1346,76 @@ note: This attribute does not have an `!`, which means it is applied to this imp
14071346
LL | #[crate_name = "0900"] impl S { }
14081347
| ^^^^^^^^^^
14091348

1349+
warning: crate-level attribute should be an inner attribute: add an exclamation mark: `#![no_main]`
1350+
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:885:1
1351+
|
1352+
LL | #[no_main]
1353+
| ^^^^^^^^^^
1354+
|
1355+
note: This attribute does not have an `!`, which means it is applied to this module
1356+
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:887:1
1357+
|
1358+
LL | / mod no_main_1 {
1359+
LL | |
1360+
LL | | mod inner { #![no_main] }
1361+
... |
1362+
LL | | }
1363+
| |_^
1364+
1365+
warning: the `#![no_main]` attribute can only be used at the crate root
1366+
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:889:17
1367+
|
1368+
LL | mod inner { #![no_main] }
1369+
| ^^^^^^^^^^^
1370+
1371+
warning: crate-level attribute should be an inner attribute: add an exclamation mark: `#![no_main]`
1372+
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:892:5
1373+
|
1374+
LL | #[no_main] fn f() { }
1375+
| ^^^^^^^^^^
1376+
|
1377+
note: This attribute does not have an `!`, which means it is applied to this function
1378+
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:892:16
1379+
|
1380+
LL | #[no_main] fn f() { }
1381+
| ^^^^^^^^^^
1382+
1383+
warning: crate-level attribute should be an inner attribute: add an exclamation mark: `#![no_main]`
1384+
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:896:5
1385+
|
1386+
LL | #[no_main] struct S;
1387+
| ^^^^^^^^^^
1388+
|
1389+
note: This attribute does not have an `!`, which means it is applied to this struct
1390+
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:896:16
1391+
|
1392+
LL | #[no_main] struct S;
1393+
| ^^^^^^^^^
1394+
1395+
warning: crate-level attribute should be an inner attribute: add an exclamation mark: `#![no_main]`
1396+
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:900:5
1397+
|
1398+
LL | #[no_main] type T = S;
1399+
| ^^^^^^^^^^
1400+
|
1401+
note: This attribute does not have an `!`, which means it is applied to this type alias
1402+
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:900:16
1403+
|
1404+
LL | #[no_main] type T = S;
1405+
| ^^^^^^^^^^^
1406+
1407+
warning: crate-level attribute should be an inner attribute: add an exclamation mark: `#![no_main]`
1408+
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:904:5
1409+
|
1410+
LL | #[no_main] impl S { }
1411+
| ^^^^^^^^^^
1412+
|
1413+
note: This attribute does not have an `!`, which means it is applied to this implementation block
1414+
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:904:16
1415+
|
1416+
LL | #[no_main] impl S { }
1417+
| ^^^^^^^^^^
1418+
14101419
warning: crate-level attribute should be an inner attribute: add an exclamation mark: `#![recursion_limit]`
14111420
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:933:1
14121421
|

0 commit comments

Comments
 (0)