Skip to content

Commit ca9a134

Browse files
committed
Auto merge of #155837 - JonathanBrouwer:rollup-aPhy30j, r=JonathanBrouwer
Rollup of 15 pull requests Successful merges: - #152995 (ACP Implementation of PermissionsExt for Windows ) - #153457 (prevent deref coercions in `pin!`) - #155250 (Windows: Cache the pipe filesystem handle) - #155574 (Move `std::io::RawOsError` to `core::io`) - #155757 (macro_metavar_expr_concat: explain why idents are invalid) - #155823 (miri subtree update) - #155693 (Suggest enclosing format string with `""` under special cases) - #155707 (Fix minor panic-unsoundness in CString::clone_into) - #155719 (Suggest `.iter()` for shared projections) - #155779 (ssa_range_prop: use `if let` guards) - #155789 (Cleanups to `AttributeExt`) - #155805 (Mention `DEPRECATED_LLVM_INTRINSIC` lint for internal use) - #155806 (Remove the incomplete marker from `impl` restrictions) - #155820 (Avoid improper spans when `...` or `..=` is recovered from non-ASCII) - #155822 (Add default field values to diagnostic FormatArgs)
2 parents d4f7856 + 139bcba commit ca9a134

82 files changed

Lines changed: 1581 additions & 598 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_ast/src/attr/mod.rs

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -235,34 +235,6 @@ impl AttributeExt for Attribute {
235235
}
236236
}
237237

238-
fn deprecation_note(&self) -> Option<Ident> {
239-
match &self.kind {
240-
AttrKind::Normal(normal) if normal.item.path == sym::deprecated => {
241-
let meta = &normal.item;
242-
243-
// #[deprecated = "..."]
244-
if let Some(s) = meta.value_str() {
245-
return Some(Ident { name: s, span: meta.span() });
246-
}
247-
248-
// #[deprecated(note = "...")]
249-
if let Some(list) = meta.meta_item_list() {
250-
for nested in list {
251-
if let Some(mi) = nested.meta_item()
252-
&& mi.path == sym::note
253-
&& let Some(s) = mi.value_str()
254-
{
255-
return Some(Ident { name: s, span: mi.span });
256-
}
257-
}
258-
}
259-
260-
None
261-
}
262-
_ => None,
263-
}
264-
}
265-
266238
fn doc_resolution_scope(&self) -> Option<AttrStyle> {
267239
match &self.kind {
268240
AttrKind::DocComment(..) => Some(self.style),
@@ -341,6 +313,34 @@ impl Attribute {
341313
)],
342314
}
343315
}
316+
317+
pub fn deprecation_note(&self) -> Option<Ident> {
318+
match &self.kind {
319+
AttrKind::Normal(normal) if normal.item.path == sym::deprecated => {
320+
let meta = &normal.item;
321+
322+
// #[deprecated = "..."]
323+
if let Some(s) = meta.value_str() {
324+
return Some(Ident { name: s, span: meta.span() });
325+
}
326+
327+
// #[deprecated(note = "...")]
328+
if let Some(list) = meta.meta_item_list() {
329+
for nested in list {
330+
if let Some(mi) = nested.meta_item()
331+
&& mi.path == sym::note
332+
&& let Some(s) = mi.value_str()
333+
{
334+
return Some(Ident { name: s, span: mi.span });
335+
}
336+
}
337+
}
338+
339+
None
340+
}
341+
_ => None,
342+
}
343+
}
344344
}
345345

346346
impl AttrItem {
@@ -824,19 +824,19 @@ pub fn mk_attr_name_value_str(
824824
mk_attr(g, style, unsafety, path, args, span)
825825
}
826826

827-
pub fn filter_by_name<A: AttributeExt>(attrs: &[A], name: Symbol) -> impl Iterator<Item = &A> {
827+
pub fn filter_by_name(attrs: &[Attribute], name: Symbol) -> impl Iterator<Item = &Attribute> {
828828
attrs.iter().filter(move |attr| attr.has_name(name))
829829
}
830830

831-
pub fn find_by_name<A: AttributeExt>(attrs: &[A], name: Symbol) -> Option<&A> {
831+
pub fn find_by_name(attrs: &[Attribute], name: Symbol) -> Option<&Attribute> {
832832
filter_by_name(attrs, name).next()
833833
}
834834

835-
pub fn first_attr_value_str_by_name(attrs: &[impl AttributeExt], name: Symbol) -> Option<Symbol> {
835+
pub fn first_attr_value_str_by_name(attrs: &[Attribute], name: Symbol) -> Option<Symbol> {
836836
find_by_name(attrs, name).and_then(|attr| attr.value_str())
837837
}
838838

839-
pub fn contains_name(attrs: &[impl AttributeExt], name: Symbol) -> bool {
839+
pub fn contains_name(attrs: &[Attribute], name: Symbol) -> bool {
840840
find_by_name(attrs, name).is_some()
841841
}
842842

@@ -911,11 +911,6 @@ pub trait AttributeExt: Debug {
911911
/// * `#[doc(...)]` returns `None`.
912912
fn doc_str(&self) -> Option<Symbol>;
913913

914-
/// Returns the deprecation note if this is deprecation attribute.
915-
/// * `#[deprecated = "note"]` returns `Some("note")`.
916-
/// * `#[deprecated(note = "note", ...)]` returns `Some("note")`.
917-
fn deprecation_note(&self) -> Option<Ident>;
918-
919914
/// Returns whether this attribute is any of the proc macro attributes.
920915
/// i.e. `proc_macro`, `proc_macro_attribute` or `proc_macro_derive`.
921916
fn is_proc_macro_attr(&self) -> bool {

compiler/rustc_attr_parsing/src/attributes/util.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::num::IntErrorKind;
22

3-
use rustc_ast::LitKind;
4-
use rustc_ast::attr::AttributeExt;
3+
use rustc_ast::{LitKind, ast};
54
use rustc_feature::is_builtin_attr_name;
65
use rustc_hir::RustcVersion;
76
use rustc_hir::limit::Limit;
@@ -27,8 +26,8 @@ pub fn parse_version(s: Symbol) -> Option<RustcVersion> {
2726
Some(RustcVersion { major, minor, patch })
2827
}
2928

30-
pub fn is_builtin_attr(attr: &impl AttributeExt) -> bool {
31-
attr.is_doc_comment().is_some() || attr.name().is_some_and(|name| is_builtin_attr_name(name))
29+
pub fn is_builtin_attr(attr: &ast::Attribute) -> bool {
30+
attr.is_doc_comment() || attr.name().is_some_and(|name| is_builtin_attr_name(name))
3231
}
3332

3433
/// Parse a single integer.

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
156156
.collect();
157157
generic_args.push((kw::SelfUpper, this.clone()));
158158

159-
let args = FormatArgs {
160-
this,
161-
// Unused
162-
this_sugared: String::new(),
163-
// Unused
164-
item_context: "",
165-
generic_args,
166-
};
159+
let args = FormatArgs { this, generic_args, .. };
167160
let CustomDiagnostic { message, label, notes, parent_label: _ } =
168161
directive.eval(None, &args);
169162

compiler/rustc_builtin_macros/src/format.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,16 @@ fn make_format_args(
173173
style: fmt_style,
174174
uncooked_symbol: uncooked_fmt_str,
175175
} = {
176+
// Extract snippet so that we can check cases `{}`, `{:?}` and `{:#?}` and emit help for
177+
// them later.
178+
let snippet = if let ExprKind::Block(b, None) = &efmt.kind
179+
&& b.stmts.len() <= 1
180+
{
181+
Some(ecx.sess.source_map().span_to_snippet(unexpanded_fmt_span))
182+
} else {
183+
None
184+
};
185+
176186
let ExpandResult::Ready(mac) = expr_to_spanned_string(ecx, efmt.clone(), msg) else {
177187
return ExpandResult::Retry(());
178188
};
@@ -222,12 +232,26 @@ fn make_format_args(
222232
});
223233
}
224234
sugg_fmt = sugg_fmt.trim_end().to_string();
225-
err.span_suggestion(
235+
err.span_suggestion_verbose(
226236
unexpanded_fmt_span.shrink_to_lo(),
227237
"you might be missing a string literal to format with",
228238
format!("\"{sugg_fmt}\", "),
229239
Applicability::MaybeIncorrect,
230240
);
241+
242+
if let Some(Ok(snippet)) = snippet.as_ref() {
243+
match snippet.as_str() {
244+
"{}" | "{:?}" | "{:#?}" => {
245+
err.span_suggestion_verbose(
246+
unexpanded_fmt_span,
247+
format!("you might want to enclose `{snippet}` with `\"\"`"),
248+
format!("\"{snippet}\""),
249+
Applicability::MaybeIncorrect,
250+
);
251+
}
252+
_ => {}
253+
};
254+
}
231255
}
232256
}
233257
err.emit()

compiler/rustc_expand/src/errors.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,49 @@ mod metavar_exprs {
545545
pub span: Span,
546546
pub key: MacroRulesNormalizedIdent,
547547
}
548+
549+
#[derive(Diagnostic)]
550+
#[diag(r#"`${"{"}concat(..){"}"}` is not generating a valid identifier"#)]
551+
pub(crate) struct ConcatInvalidIdent {
552+
#[primary_span]
553+
pub span: Span,
554+
#[subdiagnostic]
555+
pub reason: InvalidIdentReason,
556+
}
557+
558+
#[derive(Subdiagnostic)]
559+
pub(crate) enum InvalidIdentReason {
560+
#[note(r#"this `${"{"}concat(..){"}"}` invocation generated an empty ident"#)]
561+
Empty,
562+
#[note(r#"this `${"{"}concat(..){"}"}` invocation generated `{$symbol}`, but {$start} is neither '_' nor XID_Start"#)]
563+
#[note(
564+
"see <https://doc.rust-lang.org/reference/identifiers.html> for the definition of valid identifiers"
565+
)]
566+
InvalidStart { symbol: Symbol, start: char },
567+
#[note(r#"this `${"{"}concat(..){"}"}` invocation generated `{$symbol}`, but {$not_continue} is not XID_Continue"#)]
568+
#[note(
569+
"see <https://doc.rust-lang.org/reference/identifiers.html> for the definition of valid identifiers"
570+
)]
571+
InvalidContinue { symbol: Symbol, not_continue: char },
572+
}
573+
574+
impl InvalidIdentReason {
575+
pub(crate) fn new(symbol: Symbol) -> Self {
576+
let mut chars = symbol.as_str().chars();
577+
if let Some(start) = chars.next() {
578+
if rustc_lexer::is_id_start(start) {
579+
let not_continue = chars
580+
.find(|c| !rustc_lexer::is_id_continue(*c))
581+
.expect("InvalidIdentReason: cannot find invalid ident reason");
582+
InvalidIdentReason::InvalidContinue { symbol, not_continue }
583+
} else {
584+
InvalidIdentReason::InvalidStart { symbol, start }
585+
}
586+
} else {
587+
InvalidIdentReason::Empty
588+
}
589+
}
590+
}
548591
}
549592

550593
#[derive(Diagnostic)]

compiler/rustc_expand/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// tidy-alphabetical-start
22
#![allow(internal_features)]
33
#![feature(associated_type_defaults)]
4+
#![feature(default_field_values)]
45
#![feature(macro_metavar_expr)]
56
#![feature(proc_macro_diagnostic)]
67
#![feature(proc_macro_internals)]

compiler/rustc_expand/src/mbe/diagnostics.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -77,19 +77,8 @@ pub(super) fn failed_to_match_macro(
7777
let CustomDiagnostic {
7878
message: custom_message, label: custom_label, notes: custom_notes, ..
7979
} = {
80-
let macro_name = name.to_string();
8180
on_unmatch_args
82-
.map(|directive| {
83-
directive.eval(
84-
None,
85-
&FormatArgs {
86-
this: macro_name.clone(),
87-
this_sugared: macro_name,
88-
item_context: "macro invocation",
89-
generic_args: Vec::new(),
90-
},
91-
)
92-
})
81+
.map(|directive| directive.eval(None, &FormatArgs { this: name.to_string(), .. }))
9382
.unwrap_or_default()
9483
};
9584

compiler/rustc_expand/src/mbe/transcribe.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ use rustc_span::{
1818
use smallvec::{SmallVec, smallvec};
1919

2020
use crate::errors::{
21-
CountRepetitionMisplaced, MacroVarStillRepeating, MetaVarsDifSeqMatchers, MustRepeatOnce,
22-
MveUnrecognizedVar, NoRepeatableVar, NoSyntaxVarsExprRepeat, VarNoTypo,
23-
VarTypoSuggestionRepeatable, VarTypoSuggestionUnrepeatable, VarTypoSuggestionUnrepeatableLabel,
21+
ConcatInvalidIdent, CountRepetitionMisplaced, InvalidIdentReason, MacroVarStillRepeating,
22+
MetaVarsDifSeqMatchers, MustRepeatOnce, MveUnrecognizedVar, NoRepeatableVar,
23+
NoSyntaxVarsExprRepeat, VarNoTypo, VarTypoSuggestionRepeatable, VarTypoSuggestionUnrepeatable,
24+
VarTypoSuggestionUnrepeatableLabel,
2425
};
2526
use crate::mbe::macro_parser::NamedMatch;
2627
use crate::mbe::macro_parser::NamedMatch::*;
@@ -656,10 +657,10 @@ fn metavar_expr_concat<'tx>(
656657
let symbol = nfc_normalize(&concatenated);
657658
let concatenated_span = tscx.visited_dspan(dspan);
658659
if !rustc_lexer::is_ident(symbol.as_str()) {
659-
return Err(dcx.struct_span_err(
660-
concatenated_span,
661-
"`${concat(..)}` is not generating a valid identifier",
662-
));
660+
return Err(dcx.create_err(ConcatInvalidIdent {
661+
span: concatenated_span,
662+
reason: InvalidIdentReason::new(symbol),
663+
}));
663664
}
664665
tscx.psess.symbol_gallery.insert(symbol, concatenated_span);
665666

compiler/rustc_feature/src/unstable.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ declare_features! (
549549
/// Target features on hexagon.
550550
(unstable, hexagon_target_feature, "1.27.0", Some(150250)),
551551
/// Allows `impl(crate) trait Foo` restrictions.
552-
(incomplete, impl_restriction, "1.96.0", Some(105077)),
552+
(unstable, impl_restriction, "CURRENT_RUSTC_VERSION", Some(105077)),
553553
/// Allows `impl Trait` to be used inside associated types (RFC 2515).
554554
(unstable, impl_trait_in_assoc_type, "1.70.0", Some(63063)),
555555
/// Allows `impl Trait` in bindings (`let`).

compiler/rustc_hir/src/attrs/diagnostic.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,10 +218,11 @@ impl FormatString {
218218
/// ```
219219
#[derive(Debug)]
220220
pub struct FormatArgs {
221+
/// The name of the item the attribute is on.
221222
pub this: String,
222-
pub this_sugared: String,
223-
pub item_context: &'static str,
224-
pub generic_args: Vec<(Symbol, String)>,
223+
pub this_sugared: String = String::new(),
224+
pub item_context: &'static str = "",
225+
pub generic_args: Vec<(Symbol, String)> = Vec::new(),
225226
}
226227

227228
#[derive(Clone, Debug, HashStable_Generic, Encodable, Decodable, PrintAttribute)]

0 commit comments

Comments
 (0)