Skip to content

Commit 2174489

Browse files
committed
Auto merge of #155773 - JonathanBrouwer:rollup-As77GJX, r=JonathanBrouwer
Rollup of 6 pull requests Successful merges: - #154803 (Fix ICE from cfg_attr_trace ) - #155485 (Add an edge-case test for `--remap-path-prefix` for `rustc` & `rustdoc`) - #155659 (cleanup, restructure and merge `tests/ui/deriving` into `tests/ui/derives`) - #155696 (Add a higher-level API for parsing attributes) - #155734 (Lint doc comments in cfg_select branches) - #155769 (triagebot.toml: Ping Enselic when tests/debuginfo/basic-stepping.rs changes)
2 parents fb76025 + 1237cfc commit 2174489

129 files changed

Lines changed: 304 additions & 406 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

compiler/rustc_attr_parsing/src/attributes/allow_unstable.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ fn parse_unstable<S: Stage>(
8181
) -> impl IntoIterator<Item = Symbol> {
8282
let mut res = Vec::new();
8383

84-
let Some(list) = args.list() else {
84+
let Some(list) = args.as_list() else {
8585
cx.emit_err(session_diagnostics::ExpectsFeatureList {
8686
span: cx.attr_span,
8787
name: symbol.to_ident_string(),

compiler/rustc_attr_parsing/src/attributes/cfg.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,9 @@ pub fn parse_cfg<S: Stage>(
4444
cx: &mut AcceptContext<'_, '_, S>,
4545
args: &ArgParser,
4646
) -> Option<CfgEntry> {
47-
let ArgParser::List(list) = args else {
48-
let attr_span = cx.attr_span;
49-
cx.adcx().expected_list(attr_span, args);
50-
return None;
51-
};
47+
let list = cx.expect_list(args, cx.attr_span)?;
5248

53-
let Some(single) = list.single() else {
49+
let Some(single) = list.as_single() else {
5450
let target = cx.target;
5551
let mut adcx = cx.adcx();
5652
if list.is_empty() {
@@ -93,7 +89,7 @@ pub fn parse_cfg_entry<S: Stage>(
9389
MetaItemOrLitParser::MetaItemParser(meta) => match meta.args() {
9490
ArgParser::List(list) => match meta.path().word_sym() {
9591
Some(sym::not) => {
96-
let Some(single) = list.single() else {
92+
let Some(single) = list.as_single() else {
9793
return Err(cx.adcx().expected_single_argument(list.span, list.len()));
9894
};
9995
CfgEntry::Not(Box::new(parse_cfg_entry(cx, single)?), list.span)
@@ -136,7 +132,7 @@ fn parse_cfg_entry_version<S: Stage>(
136132
meta_span: Span,
137133
) -> Result<CfgEntry, ErrorGuaranteed> {
138134
try_gate_cfg(sym::version, meta_span, cx.sess(), cx.features_option());
139-
let Some(version) = list.single() else {
135+
let Some(version) = list.as_single() else {
140136
return Err(
141137
cx.emit_err(session_diagnostics::ExpectedSingleVersionLiteral { span: list.span })
142138
);

compiler/rustc_attr_parsing/src/attributes/cfg_select.rs

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
use rustc_ast::token::Token;
1+
use rustc_ast::token::{CommentKind, Token};
22
use rustc_ast::tokenstream::TokenStream;
33
use rustc_ast::{AttrStyle, NodeId, token};
44
use rustc_data_structures::fx::FxHashMap;
5-
use rustc_errors::Diagnostic;
5+
use rustc_errors::{DiagDecorator, Diagnostic};
66
use rustc_feature::{AttributeTemplate, Features};
77
use rustc_hir::attrs::CfgEntry;
88
use rustc_hir::{AttrPath, Target};
99
use rustc_parse::exp;
1010
use rustc_parse::parser::{Parser, Recovery};
1111
use rustc_session::Session;
12-
use rustc_session::lint::builtin::UNREACHABLE_CFG_SELECT_PREDICATES;
12+
use rustc_session::lint::builtin::{UNREACHABLE_CFG_SELECT_PREDICATES, UNUSED_DOC_COMMENTS};
1313
use rustc_span::{ErrorGuaranteed, Span, Symbol, sym};
1414

1515
use crate::attributes::AttributeSafety;
@@ -78,12 +78,15 @@ pub fn parse_cfg_select(
7878
let mut branches = CfgSelectBranches::default();
7979

8080
while p.token != token::Eof {
81+
let doc_comment = eat_outer_doc_comments(p);
82+
8183
if p.eat_keyword(exp!(Underscore)) {
8284
let underscore = p.prev_token;
8385
p.expect(exp!(FatArrow)).map_err(|e| e.emit())?;
8486

8587
let tts = p.parse_delimited_token_tree().map_err(|e| e.emit())?;
8688
let span = underscore.span.to(p.token.span);
89+
lint_unused_doc_comment(p, doc_comment, lint_node_id);
8790

8891
match branches.wildcard {
8992
None => branches.wildcard = Some((underscore, tts, span)),
@@ -123,6 +126,7 @@ pub fn parse_cfg_select(
123126

124127
let tts = p.parse_delimited_token_tree().map_err(|e| e.emit())?;
125128
let span = cfg_span.to(p.token.span);
129+
lint_unused_doc_comment(p, doc_comment, lint_node_id);
126130

127131
match branches.wildcard {
128132
None => branches.reachable.push((cfg, tts, span)),
@@ -143,6 +147,41 @@ pub fn parse_cfg_select(
143147
Ok(branches)
144148
}
145149

150+
fn eat_outer_doc_comments(p: &mut Parser<'_>) -> Option<(Span, CommentKind)> {
151+
let mut doc_comment: Option<(Span, CommentKind)> = None;
152+
153+
while let token::DocComment(comment_kind, AttrStyle::Outer, _) = p.token.kind {
154+
let span = p.token.span;
155+
doc_comment = Some(match doc_comment {
156+
Some((prev_span, _)) => (prev_span.with_hi(span.hi()), comment_kind),
157+
None => (span, comment_kind),
158+
});
159+
p.bump();
160+
}
161+
162+
doc_comment
163+
}
164+
165+
fn lint_unused_doc_comment(
166+
p: &mut Parser<'_>,
167+
doc_comment: Option<(Span, CommentKind)>,
168+
lint_node_id: NodeId,
169+
) {
170+
let Some((span, comment_kind)) = doc_comment else { return };
171+
let help = match comment_kind {
172+
CommentKind::Line => "use `//` for a plain comment",
173+
CommentKind::Block => "use `/* */` for a plain comment",
174+
};
175+
p.psess.buffer_lint(
176+
UNUSED_DOC_COMMENTS,
177+
span,
178+
lint_node_id,
179+
DiagDecorator(move |diag| {
180+
diag.primary_message("unused doc comment").help(help);
181+
}),
182+
);
183+
}
184+
146185
fn lint_unreachable(
147186
p: &mut Parser<'_>,
148187
predicates: impl Iterator<Item = CfgSelectPredicate>,

compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ impl<S: Stage> SingleAttributeParser<S> for OptimizeParser {
2424
const TEMPLATE: AttributeTemplate = template!(List: &["size", "speed", "none"]);
2525

2626
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
27-
let single = cx.single_element_list(args, cx.attr_span)?;
27+
let single = cx.expect_single_element_list(args, cx.attr_span)?;
2828

2929
let res = match single.meta_item().and_then(|i| i.path().word().map(|i| i.name)) {
3030
Some(sym::size) => OptimizeAttr::Size,
@@ -75,7 +75,7 @@ impl<S: Stage> SingleAttributeParser<S> for CoverageParser {
7575
const TEMPLATE: AttributeTemplate = template!(OneOf: &[sym::off, sym::on]);
7676

7777
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
78-
let arg = cx.single_element_list(args, cx.attr_span)?;
78+
let arg = cx.expect_single_element_list(args, cx.attr_span)?;
7979

8080
let mut fail_incorrect_argument =
8181
|span| cx.adcx().expected_specific_argument(span, &[sym::on, sym::off]);
@@ -371,8 +371,7 @@ impl<S: Stage> AttributeParser<S> for UsedParser {
371371
let used_by = match args {
372372
ArgParser::NoArgs => UsedBy::Default,
373373
ArgParser::List(list) => {
374-
let Some(l) = list.single() else {
375-
cx.adcx().expected_single_argument(list.span, list.len());
374+
let Some(l) = cx.expect_single(list) else {
376375
return;
377376
};
378377

@@ -463,9 +462,7 @@ fn parse_tf_attribute<S: Stage>(
463462
args: &ArgParser,
464463
) -> impl IntoIterator<Item = (Symbol, Span)> {
465464
let mut features = Vec::new();
466-
let ArgParser::List(list) = args else {
467-
let attr_span = cx.attr_span;
468-
cx.adcx().expected_list(attr_span, args);
465+
let Some(list) = cx.expect_list(args, cx.attr_span) else {
469466
return features;
470467
};
471468
if list.is_empty() {
@@ -588,11 +585,7 @@ impl<S: Stage> SingleAttributeParser<S> for SanitizeParser {
588585
]);
589586

590587
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
591-
let Some(list) = args.list() else {
592-
let attr_span = cx.attr_span;
593-
cx.adcx().expected_list(attr_span, args);
594-
return None;
595-
};
588+
let list = cx.expect_list(args, cx.attr_span)?;
596589

597590
let mut on_set = SanitizerSet::empty();
598591
let mut off_set = SanitizerSet::empty();
@@ -719,11 +712,7 @@ impl<S: Stage> SingleAttributeParser<S> for PatchableFunctionEntryParser {
719712
const TEMPLATE: AttributeTemplate = template!(List: &["prefix_nops = m, entry_nops = n"]);
720713

721714
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
722-
let Some(meta_item_list) = args.list() else {
723-
let attr_span = cx.attr_span;
724-
cx.adcx().expected_list(attr_span, args);
725-
return None;
726-
};
715+
let meta_item_list = cx.expect_list(args, cx.attr_span)?;
727716

728717
let mut prefix = None;
729718
let mut entry = None;

compiler/rustc_attr_parsing/src/attributes/confusables.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,7 @@ impl<S: Stage> AttributeParser<S> for ConfusablesParser {
1212
&[sym::rustc_confusables],
1313
template!(List: &[r#""name1", "name2", ..."#]),
1414
|this, cx, args| {
15-
let Some(list) = args.list() else {
16-
let attr_span = cx.attr_span;
17-
cx.adcx().expected_list(attr_span, args);
18-
return;
19-
};
15+
let Some(list) = cx.expect_list(args, cx.attr_span) else { return };
2016

2117
if list.is_empty() {
2218
cx.emit_err(EmptyConfusables { span: cx.attr_span });

compiler/rustc_attr_parsing/src/attributes/crate_level.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -314,9 +314,7 @@ impl<S: Stage> CombineAttributeParser<S> for FeatureParser {
314314
cx: &mut AcceptContext<'_, '_, S>,
315315
args: &ArgParser,
316316
) -> impl IntoIterator<Item = Self::Item> {
317-
let ArgParser::List(list) = args else {
318-
let attr_span = cx.attr_span;
319-
cx.adcx().expected_list(attr_span, args);
317+
let Some(list) = cx.expect_list(args, cx.attr_span) else {
320318
return Vec::new();
321319
};
322320

@@ -362,9 +360,7 @@ impl<S: Stage> CombineAttributeParser<S> for RegisterToolParser {
362360
cx: &mut AcceptContext<'_, '_, S>,
363361
args: &ArgParser,
364362
) -> impl IntoIterator<Item = Self::Item> {
365-
let ArgParser::List(list) = args else {
366-
let attr_span = cx.attr_span;
367-
cx.adcx().expected_list(attr_span, args);
363+
let Some(list) = cx.expect_list(args, cx.attr_span) else {
368364
return Vec::new();
369365
};
370366

compiler/rustc_attr_parsing/src/attributes/debugger.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ impl<S: Stage> CombineAttributeParser<S> for DebuggerViualizerParser {
2020
cx: &mut AcceptContext<'_, '_, S>,
2121
args: &ArgParser,
2222
) -> impl IntoIterator<Item = Self::Item> {
23-
let single = cx.single_element_list(args, cx.attr_span)?;
23+
let single = cx.expect_single_element_list(args, cx.attr_span)?;
2424
let Some(mi) = single.meta_item() else {
2525
cx.adcx().expected_name_value(single.span(), None);
2626
return None;

compiler/rustc_attr_parsing/src/attributes/diagnostic/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ fn parse_directive_items<'p, S: Stage>(
344344
}
345345
(Mode::RustcOnUnimplemented, sym::on) => {
346346
if is_root {
347-
let items = or_malformed!(item.args().list()?);
347+
let items = or_malformed!(item.args().as_list()?);
348348
let mut iter = items.mixed();
349349
let condition: &MetaItemOrLitParser = match iter.next() {
350350
Some(c) => c,
@@ -554,7 +554,7 @@ fn parse_predicate(input: &MetaItemOrLitParser) -> Result<Predicate, InvalidOnCl
554554
sym::any => Ok(Predicate::Any(parse_predicate_sequence(mis)?)),
555555
sym::all => Ok(Predicate::All(parse_predicate_sequence(mis)?)),
556556
sym::not => {
557-
if let Some(single) = mis.single() {
557+
if let Some(single) = mis.as_single() {
558558
Ok(Predicate::Not(Box::new(parse_predicate(single)?)))
559559
} else {
560560
Err(InvalidOnClause::ExpectedOnePredInNot { span: mis.span })

compiler/rustc_attr_parsing/src/attributes/doc.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ impl DocParser {
199199
self.attribute.no_crate_inject = Some(path.span())
200200
}
201201
Some(sym::attr) => {
202-
let Some(list) = args.list() else {
202+
let Some(list) = args.as_list() else {
203203
// FIXME: remove this method once merged and uncomment the line below instead.
204204
// cx.expected_list(cx.attr_span, args);
205205
let span = cx.attr_span;
@@ -587,7 +587,7 @@ impl DocParser {
587587
}),
588588
Some(sym::auto_cfg) => self.parse_auto_cfg(cx, path, args),
589589
Some(sym::test) => {
590-
let Some(list) = args.list() else {
590+
let Some(list) = args.as_list() else {
591591
cx.emit_dyn_lint(
592592
rustc_session::lint::builtin::INVALID_DOC_ATTRIBUTES,
593593
|dcx, level| DocTestTakesList.into_diag(dcx, level),

compiler/rustc_attr_parsing/src/attributes/inline.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,7 @@ impl<S: Stage> SingleAttributeParser<S> for InlineParser {
3737
match args {
3838
ArgParser::NoArgs => Some(AttributeKind::Inline(InlineAttr::Hint, cx.attr_span)),
3939
ArgParser::List(list) => {
40-
let Some(l) = list.single() else {
41-
cx.adcx().expected_single_argument(list.span, list.len());
42-
return None;
43-
};
40+
let l = cx.expect_single(list)?;
4441

4542
match l.meta_item().and_then(|i| i.path().word_sym()) {
4643
Some(sym::always) => {
@@ -78,10 +75,7 @@ impl<S: Stage> SingleAttributeParser<S> for RustcForceInlineParser {
7875
let reason = match args {
7976
ArgParser::NoArgs => None,
8077
ArgParser::List(list) => {
81-
let Some(l) = list.single() else {
82-
cx.adcx().expected_single_argument(list.span, list.len());
83-
return None;
84-
};
78+
let l = cx.expect_single(list)?;
8579

8680
let Some(reason) = l.lit().and_then(|i| i.kind.str()) else {
8781
cx.adcx().expected_string_literal(l.span(), l.lit());

0 commit comments

Comments
 (0)