Skip to content

Commit 7fb1d02

Browse files
committed
Auto merge of rust-lang#128403 - matthiaskrgr:rollup-za99w4l, r=matthiaskrgr
Rollup of 8 pull requests Successful merges: - rust-lang#127543 (More unsafe attr verification) - rust-lang#128357 (Detect non-lifetime binder params shadowing item params) - rust-lang#128367 (CI: rfl: build the generated doctests and documentation) - rust-lang#128376 (Mark `Parser::eat`/`check` methods as `#[must_use]`) - rust-lang#128379 (the output in stderr expects panic-unwind) - rust-lang#128380 (make `///` doc comments compatible with naked functions) - rust-lang#128382 (cargo-miri: better error when we seem to run inside bootstrap but something is wrong) - rust-lang#128398 (tidy: Fix quote in error message) r? `@ghost` `@rustbot` modify labels: rollup
2 parents f8060d2 + 582ae3a commit 7fb1d02

Some content is hidden

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

45 files changed

+636
-132
lines changed

compiler/rustc_builtin_macros/messages.ftl

-3
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,6 @@ builtin_macros_derive_path_args_list = traits in `#[derive(...)]` don't accept a
115115
builtin_macros_derive_path_args_value = traits in `#[derive(...)]` don't accept values
116116
.suggestion = remove the value
117117
118-
builtin_macros_derive_unsafe_path = traits in `#[derive(...)]` don't accept `unsafe(...)`
119-
.suggestion = remove the `unsafe(...)`
120-
121118
builtin_macros_env_not_defined = environment variable `{$var}` not defined at compile time
122119
.cargo = Cargo sets build script variables at run time. Use `std::env::var({$var_expr})` instead
123120
.custom = use `std::env::var({$var_expr})` to read the variable at run time

compiler/rustc_builtin_macros/src/cfg.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use rustc_ast::token;
66
use rustc_ast::tokenstream::TokenStream;
77
use rustc_errors::PResult;
88
use rustc_expand::base::{DummyResult, ExpandResult, ExtCtxt, MacEager, MacroExpanderResult};
9+
use rustc_parse::parser::attr::AllowLeadingUnsafe;
910
use rustc_span::Span;
1011
use {rustc_ast as ast, rustc_attr as attr};
1112

@@ -42,7 +43,7 @@ fn parse_cfg<'a>(cx: &ExtCtxt<'a>, span: Span, tts: TokenStream) -> PResult<'a,
4243
return Err(cx.dcx().create_err(errors::RequiresCfgPattern { span }));
4344
}
4445

45-
let cfg = p.parse_meta_item()?;
46+
let cfg = p.parse_meta_item(AllowLeadingUnsafe::Yes)?;
4647

4748
let _ = p.eat(&token::Comma);
4849

compiler/rustc_builtin_macros/src/cfg_accessible.rs

+2
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,13 @@ impl MultiItemModifier for Expander {
4747
) -> ExpandResult<Vec<Annotatable>, Annotatable> {
4848
let template = AttributeTemplate { list: Some("path"), ..Default::default() };
4949
validate_attr::check_builtin_meta_item(
50+
&ecx.ecfg.features,
5051
&ecx.sess.psess,
5152
meta_item,
5253
ast::AttrStyle::Outer,
5354
sym::cfg_accessible,
5455
template,
56+
true,
5557
);
5658

5759
let Some(path) = validate_input(ecx, meta_item) else {

compiler/rustc_builtin_macros/src/derive.rs

+3-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use rustc_ast as ast;
2-
use rustc_ast::{GenericParamKind, ItemKind, MetaItemKind, NestedMetaItem, Safety, StmtKind};
2+
use rustc_ast::{GenericParamKind, ItemKind, MetaItemKind, NestedMetaItem, StmtKind};
33
use rustc_expand::base::{
44
Annotatable, DeriveResolution, ExpandResult, ExtCtxt, Indeterminate, MultiItemModifier,
55
};
@@ -38,11 +38,13 @@ impl MultiItemModifier for Expander {
3838
let template =
3939
AttributeTemplate { list: Some("Trait1, Trait2, ..."), ..Default::default() };
4040
validate_attr::check_builtin_meta_item(
41+
features,
4142
&sess.psess,
4243
meta_item,
4344
ast::AttrStyle::Outer,
4445
sym::derive,
4546
template,
47+
true,
4648
);
4749

4850
let mut resolutions = match &meta_item.kind {
@@ -60,7 +62,6 @@ impl MultiItemModifier for Expander {
6062
// Reject `#[derive(Debug = "value", Debug(abc))]`, but recover the
6163
// paths.
6264
report_path_args(sess, meta);
63-
report_unsafe_args(sess, meta);
6465
meta.path.clone()
6566
})
6667
.map(|path| DeriveResolution {
@@ -160,13 +161,3 @@ fn report_path_args(sess: &Session, meta: &ast::MetaItem) {
160161
}
161162
}
162163
}
163-
164-
fn report_unsafe_args(sess: &Session, meta: &ast::MetaItem) {
165-
match meta.unsafety {
166-
Safety::Unsafe(span) => {
167-
sess.dcx().emit_err(errors::DeriveUnsafePath { span });
168-
}
169-
Safety::Default => {}
170-
Safety::Safe(_) => unreachable!(),
171-
}
172-
}

compiler/rustc_builtin_macros/src/errors.rs

-7
Original file line numberDiff line numberDiff line change
@@ -297,13 +297,6 @@ pub(crate) struct DerivePathArgsValue {
297297
pub(crate) span: Span,
298298
}
299299

300-
#[derive(Diagnostic)]
301-
#[diag(builtin_macros_derive_unsafe_path)]
302-
pub(crate) struct DeriveUnsafePath {
303-
#[primary_span]
304-
pub(crate) span: Span,
305-
}
306-
307300
#[derive(Diagnostic)]
308301
#[diag(builtin_macros_no_default_variant)]
309302
#[help]

compiler/rustc_builtin_macros/src/pattern_type.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ fn parse_pat_ty<'a>(cx: &mut ExtCtxt<'a>, stream: TokenStream) -> PResult<'a, (P
2424
let mut parser = cx.new_parser_from_tts(stream);
2525

2626
let ty = parser.parse_ty()?;
27-
parser.eat_keyword(sym::is);
27+
parser.expect_keyword(sym::is)?;
2828
let pat = parser.parse_pat_no_top_alt(None, None)?;
2929

3030
Ok((ty, pat))

compiler/rustc_builtin_macros/src/util.rs

+2
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ pub(crate) fn check_builtin_macro_attribute(ecx: &ExtCtxt<'_>, meta_item: &MetaI
1717
// All the built-in macro attributes are "words" at the moment.
1818
let template = AttributeTemplate { word: true, ..Default::default() };
1919
validate_attr::check_builtin_meta_item(
20+
&ecx.ecfg.features,
2021
&ecx.sess.psess,
2122
meta_item,
2223
AttrStyle::Outer,
2324
name,
2425
template,
26+
true,
2527
);
2628
}
2729

compiler/rustc_expand/src/config.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ use rustc_ast::tokenstream::{
88
use rustc_ast::{self as ast, AttrStyle, Attribute, HasAttrs, HasTokens, MetaItem, NodeId};
99
use rustc_attr as attr;
1010
use rustc_data_structures::flat_map_in_place::FlatMapInPlace;
11-
use rustc_feature::{Features, ACCEPTED_FEATURES, REMOVED_FEATURES, UNSTABLE_FEATURES};
11+
use rustc_feature::{
12+
AttributeSafety, Features, ACCEPTED_FEATURES, REMOVED_FEATURES, UNSTABLE_FEATURES,
13+
};
1214
use rustc_lint_defs::BuiltinLintDiag;
1315
use rustc_parse::validate_attr;
1416
use rustc_session::parse::feature_err;
@@ -263,6 +265,13 @@ impl<'a> StripUnconfigured<'a> {
263265
/// is in the original source file. Gives a compiler error if the syntax of
264266
/// the attribute is incorrect.
265267
pub(crate) fn expand_cfg_attr(&self, cfg_attr: &Attribute, recursive: bool) -> Vec<Attribute> {
268+
validate_attr::check_attribute_safety(
269+
self.features.unwrap_or(&Features::default()),
270+
&self.sess.psess,
271+
AttributeSafety::Normal,
272+
&cfg_attr,
273+
);
274+
266275
let Some((cfg_predicate, expanded_attrs)) =
267276
rustc_parse::parse_cfg_attr(cfg_attr, &self.sess.psess)
268277
else {
@@ -385,6 +394,13 @@ impl<'a> StripUnconfigured<'a> {
385394
return (true, None);
386395
}
387396
};
397+
398+
validate_attr::deny_builtin_meta_unsafety(
399+
self.features.unwrap_or(&Features::default()),
400+
&self.sess.psess,
401+
&meta_item,
402+
);
403+
388404
(
389405
parse_cfg(&meta_item, self.sess).map_or(true, |meta_item| {
390406
attr::cfg_matches(meta_item, &self.sess, self.lint_node_id, self.features)

compiler/rustc_interface/src/interface.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use rustc_middle::ty;
1515
use rustc_middle::ty::CurrentGcx;
1616
use rustc_middle::util::Providers;
1717
use rustc_parse::new_parser_from_source_str;
18+
use rustc_parse::parser::attr::AllowLeadingUnsafe;
1819
use rustc_query_impl::QueryCtxt;
1920
use rustc_query_system::query::print_query_stack;
2021
use rustc_session::config::{self, Cfg, CheckCfg, ExpectedValues, Input, OutFileName};
@@ -67,7 +68,7 @@ pub(crate) fn parse_cfg(dcx: DiagCtxtHandle<'_>, cfgs: Vec<String>) -> Cfg {
6768
}
6869

6970
match new_parser_from_source_str(&psess, filename, s.to_string()) {
70-
Ok(mut parser) => match parser.parse_meta_item() {
71+
Ok(mut parser) => match parser.parse_meta_item(AllowLeadingUnsafe::No) {
7172
Ok(meta_item) if parser.token == token::Eof => {
7273
if meta_item.path.segments.len() != 1 {
7374
error!("argument key must be an identifier");
@@ -173,7 +174,7 @@ pub(crate) fn parse_check_cfg(dcx: DiagCtxtHandle<'_>, specs: Vec<String>) -> Ch
173174
}
174175
};
175176

176-
let meta_item = match parser.parse_meta_item() {
177+
let meta_item = match parser.parse_meta_item(AllowLeadingUnsafe::Yes) {
177178
Ok(meta_item) if parser.token == token::Eof => meta_item,
178179
Ok(..) => expected_error(),
179180
Err(err) => {

compiler/rustc_parse/messages.ftl

+1
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ parse_inner_doc_comment_not_permitted = expected outer doc comment
365365
.sugg_change_inner_to_outer = to annotate the {$item}, change the doc comment from inner to outer style
366366
367367
parse_invalid_attr_unsafe = `{$name}` is not an unsafe attribute
368+
.label = this is not an unsafe attribute
368369
.suggestion = remove the `unsafe(...)`
369370
.note = extraneous unsafe is not allowed in attributes
370371

compiler/rustc_parse/src/errors.rs

+1
Original file line numberDiff line numberDiff line change
@@ -3183,6 +3183,7 @@ pub(crate) struct DotDotRangeAttribute {
31833183
#[note]
31843184
pub struct InvalidAttrUnsafe {
31853185
#[primary_span]
3186+
#[label]
31863187
pub span: Span,
31873188
pub name: Path,
31883189
}

compiler/rustc_parse/src/parser/attr.rs

+17-4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ enum OuterAttributeType {
3131
Attribute,
3232
}
3333

34+
#[derive(Clone, Copy, PartialEq, Eq)]
35+
pub enum AllowLeadingUnsafe {
36+
Yes,
37+
No,
38+
}
39+
3440
impl<'a> Parser<'a> {
3541
/// Parses attributes that appear before an item.
3642
pub(super) fn parse_outer_attributes(&mut self) -> PResult<'a, AttrWrapper> {
@@ -332,7 +338,7 @@ impl<'a> Parser<'a> {
332338

333339
/// Parses `cfg_attr(pred, attr_item_list)` where `attr_item_list` is comma-delimited.
334340
pub fn parse_cfg_attr(&mut self) -> PResult<'a, (ast::MetaItem, Vec<(ast::AttrItem, Span)>)> {
335-
let cfg_predicate = self.parse_meta_item()?;
341+
let cfg_predicate = self.parse_meta_item(AllowLeadingUnsafe::No)?;
336342
self.expect(&token::Comma)?;
337343

338344
// Presumably, the majority of the time there will only be one attr.
@@ -368,7 +374,10 @@ impl<'a> Parser<'a> {
368374
/// MetaItem = SimplePath ( '=' UNSUFFIXED_LIT | '(' MetaSeq? ')' )? ;
369375
/// MetaSeq = MetaItemInner (',' MetaItemInner)* ','? ;
370376
/// ```
371-
pub fn parse_meta_item(&mut self) -> PResult<'a, ast::MetaItem> {
377+
pub fn parse_meta_item(
378+
&mut self,
379+
unsafe_allowed: AllowLeadingUnsafe,
380+
) -> PResult<'a, ast::MetaItem> {
372381
// We can't use `maybe_whole` here because it would bump in the `None`
373382
// case, which we don't want.
374383
if let token::Interpolated(nt) = &self.token.kind
@@ -384,7 +393,11 @@ impl<'a> Parser<'a> {
384393
}
385394

386395
let lo = self.token.span;
387-
let is_unsafe = self.eat_keyword(kw::Unsafe);
396+
let is_unsafe = if unsafe_allowed == AllowLeadingUnsafe::Yes {
397+
self.eat_keyword(kw::Unsafe)
398+
} else {
399+
false
400+
};
388401
let unsafety = if is_unsafe {
389402
let unsafe_span = self.prev_token.span;
390403
self.psess.gated_spans.gate(sym::unsafe_attributes, unsafe_span);
@@ -427,7 +440,7 @@ impl<'a> Parser<'a> {
427440
Err(err) => err.cancel(), // we provide a better error below
428441
}
429442

430-
match self.parse_meta_item() {
443+
match self.parse_meta_item(AllowLeadingUnsafe::No) {
431444
Ok(mi) => return Ok(ast::NestedMetaItem::MetaItem(mi)),
432445
Err(err) => err.cancel(), // we provide a better error below
433446
}

compiler/rustc_parse/src/parser/expr.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -3153,7 +3153,8 @@ impl<'a> Parser<'a> {
31533153

31543154
if !require_comma {
31553155
arm_body = Some(expr);
3156-
this.eat(&token::Comma);
3156+
// Eat a comma if it exists, though.
3157+
let _ = this.eat(&token::Comma);
31573158
Ok(Recovered::No)
31583159
} else if let Some((span, guar)) =
31593160
this.parse_arm_body_missing_braces(&expr, arrow_span)
@@ -3654,7 +3655,7 @@ impl<'a> Parser<'a> {
36543655
fields.push(f);
36553656
}
36563657
self.recover_stmt_(SemiColonMode::Comma, BlockMode::Ignore);
3657-
self.eat(&token::Comma);
3658+
let _ = self.eat(&token::Comma);
36583659
}
36593660
}
36603661
}

compiler/rustc_parse/src/parser/generics.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,8 @@ impl<'a> Parser<'a> {
178178
span: this.prev_token.span,
179179
});
180180

181-
this.eat(&token::Comma);
181+
// Eat a trailing comma, if it exists.
182+
let _ = this.eat(&token::Comma);
182183
}
183184

184185
let param = if this.check_lifetime() {

compiler/rustc_parse/src/parser/item.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1192,13 +1192,14 @@ impl<'a> Parser<'a> {
11921192
mut safety: Safety,
11931193
) -> PResult<'a, ItemInfo> {
11941194
let abi = self.parse_abi(); // ABI?
1195+
// FIXME: This recovery should be tested better.
11951196
if safety == Safety::Default
11961197
&& self.token.is_keyword(kw::Unsafe)
11971198
&& self.look_ahead(1, |t| t.kind == token::OpenDelim(Delimiter::Brace))
11981199
{
11991200
self.expect(&token::OpenDelim(Delimiter::Brace)).unwrap_err().emit();
12001201
safety = Safety::Unsafe(self.token.span);
1201-
self.eat_keyword(kw::Unsafe);
1202+
let _ = self.eat_keyword(kw::Unsafe);
12021203
}
12031204
let module = ast::ForeignMod {
12041205
safety,
@@ -1759,7 +1760,7 @@ impl<'a> Parser<'a> {
17591760
}
17601761
}
17611762
}
1762-
self.eat(&token::CloseDelim(Delimiter::Brace));
1763+
self.expect(&token::CloseDelim(Delimiter::Brace))?;
17631764
} else {
17641765
let token_str = super::token_descr(&self.token);
17651766
let where_str = if parsed_where { "" } else { "`where`, or " };
@@ -1902,7 +1903,7 @@ impl<'a> Parser<'a> {
19021903
if let Some(_guar) = guar {
19031904
// Handle a case like `Vec<u8>>,` where we can continue parsing fields
19041905
// after the comma
1905-
self.eat(&token::Comma);
1906+
let _ = self.eat(&token::Comma);
19061907

19071908
// `check_trailing_angle_brackets` already emitted a nicer error, as
19081909
// proven by the presence of `_guar`. We can continue parsing.

0 commit comments

Comments
 (0)