Skip to content

Commit a0569fa

Browse files
committed
Auto merge of #122830 - matthiaskrgr:rollup-uk2by3f, r=matthiaskrgr
Rollup of 8 pull requests Successful merges: - #122402 (Make `#[diagnostic::on_unimplemented]` format string parsing more robust) - #122644 (pattern analysis: add a custom test harness) - #122733 (Strip placeholders from hidden types before remapping generic parameter) - #122752 (Interpolated cleanups) - #122771 (add some comments to hir::ModuleItems) - #122793 (Implement macro-based deref!() syntax for deref patterns) - #122810 (Remove `target_override`) - #122827 (Remove unnecessary braces from `bug`/`span_bug`) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 7d01878 + 6ae51a5 commit a0569fa

File tree

83 files changed

+1480
-422
lines changed

Some content is hidden

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

83 files changed

+1480
-422
lines changed

Cargo.lock

+2
Original file line numberDiff line numberDiff line change
@@ -4440,6 +4440,8 @@ dependencies = [
44404440
"rustc_target",
44414441
"smallvec",
44424442
"tracing",
4443+
"tracing-subscriber",
4444+
"tracing-tree",
44434445
]
44444446

44454447
[[package]]

compiler/rustc_ast/src/ast.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,9 @@ impl Pat {
621621
| PatKind::Or(s) => s.iter().for_each(|p| p.walk(it)),
622622

623623
// Trivial wrappers over inner patterns.
624-
PatKind::Box(s) | PatKind::Ref(s, _) | PatKind::Paren(s) => s.walk(it),
624+
PatKind::Box(s) | PatKind::Deref(s) | PatKind::Ref(s, _) | PatKind::Paren(s) => {
625+
s.walk(it)
626+
}
625627

626628
// These patterns do not contain subpatterns, skip.
627629
PatKind::Wild
@@ -792,6 +794,9 @@ pub enum PatKind {
792794
/// A `box` pattern.
793795
Box(P<Pat>),
794796

797+
/// A `deref` pattern (currently `deref!()` macro-based syntax).
798+
Deref(P<Pat>),
799+
795800
/// A reference pattern (e.g., `&mut (a, b)`).
796801
Ref(P<Pat>, Mutability),
797802

compiler/rustc_ast/src/mut_visit.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1295,6 +1295,7 @@ pub fn noop_visit_pat<T: MutVisitor>(pat: &mut P<Pat>, vis: &mut T) {
12951295
fields.flat_map_in_place(|field| vis.flat_map_pat_field(field));
12961296
}
12971297
PatKind::Box(inner) => vis.visit_pat(inner),
1298+
PatKind::Deref(inner) => vis.visit_pat(inner),
12981299
PatKind::Ref(inner, _mutbl) => vis.visit_pat(inner),
12991300
PatKind::Range(e1, e2, Spanned { span: _, node: _ }) => {
13001301
visit_opt(e1, |e| vis.visit_expr(e));

compiler/rustc_ast/src/token.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ impl Lit {
105105
}
106106
}
107107

108-
/// Keep this in sync with `Token::can_begin_literal_or_bool` excluding unary negation.
108+
/// Keep this in sync with `Token::can_begin_literal_maybe_minus` excluding unary negation.
109109
pub fn from_token(token: &Token) -> Option<Lit> {
110110
match token.uninterpolate().kind {
111111
Ident(name, IdentIsRaw::No) if name.is_bool_lit() => Some(Lit::new(Bool, name, None)),
@@ -664,7 +664,7 @@ impl Token {
664664
}
665665

666666
/// Returns `true` if the token is an interpolated path.
667-
fn is_path(&self) -> bool {
667+
fn is_whole_path(&self) -> bool {
668668
if let Interpolated(nt) = &self.kind
669669
&& let NtPath(..) = &nt.0
670670
{
@@ -710,7 +710,7 @@ impl Token {
710710
pub fn is_path_start(&self) -> bool {
711711
self == &ModSep
712712
|| self.is_qpath_start()
713-
|| self.is_path()
713+
|| self.is_whole_path()
714714
|| self.is_path_segment_keyword()
715715
|| self.is_ident() && !self.is_reserved_ident()
716716
}

compiler/rustc_ast/src/tokenstream.rs

+1-12
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,7 @@ use smallvec::{smallvec, SmallVec};
2828
use std::borrow::Cow;
2929
use std::{cmp, fmt, iter};
3030

31-
/// When the main Rust parser encounters a syntax-extension invocation, it
32-
/// parses the arguments to the invocation as a token tree. This is a very
33-
/// loose structure, such that all sorts of different AST fragments can
34-
/// be passed to syntax extensions using a uniform type.
35-
///
36-
/// If the syntax extension is an MBE macro, it will attempt to match its
37-
/// LHS token tree against the provided token tree, and if it finds a
38-
/// match, will transcribe the RHS token tree, splicing in any captured
39-
/// `macro_parser::matched_nonterminals` into the `SubstNt`s it finds.
40-
///
41-
/// The RHS of an MBE macro is the only place `SubstNt`s are substituted.
42-
/// Nothing special happens to misnamed or misplaced `SubstNt`s.
31+
/// Part of a `TokenStream`.
4332
#[derive(Debug, Clone, PartialEq, Encodable, Decodable, HashStable_Generic)]
4433
pub enum TokenTree {
4534
/// A single token. Should never be `OpenDelim` or `CloseDelim`, because

compiler/rustc_ast/src/visit.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,10 @@ pub fn walk_pat<'a, V: Visitor<'a>>(visitor: &mut V, pattern: &'a Pat) -> V::Res
576576
try_visit!(visitor.visit_path(path, pattern.id));
577577
walk_list!(visitor, visit_pat_field, fields);
578578
}
579-
PatKind::Box(subpattern) | PatKind::Ref(subpattern, _) | PatKind::Paren(subpattern) => {
579+
PatKind::Box(subpattern)
580+
| PatKind::Deref(subpattern)
581+
| PatKind::Ref(subpattern, _)
582+
| PatKind::Paren(subpattern) => {
580583
try_visit!(visitor.visit_pat(subpattern));
581584
}
582585
PatKind::Ident(_, ident, optional_subpattern) => {

compiler/rustc_ast_lowering/src/pat.rs

+3
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
9191
PatKind::Box(inner) => {
9292
break hir::PatKind::Box(self.lower_pat(inner));
9393
}
94+
PatKind::Deref(inner) => {
95+
break hir::PatKind::Deref(self.lower_pat(inner));
96+
}
9497
PatKind::Ref(inner, mutbl) => {
9598
break hir::PatKind::Ref(self.lower_pat(inner), *mutbl);
9699
}

compiler/rustc_ast_passes/src/feature_gate.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -413,10 +413,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
413413
}
414414
}
415415
PatKind::Box(..) => {
416-
if !self.features.deref_patterns {
417-
// Allow box patterns under `deref_patterns`.
418-
gate!(&self, box_patterns, pattern.span, "box pattern syntax is experimental");
419-
}
416+
gate!(&self, box_patterns, pattern.span, "box pattern syntax is experimental");
420417
}
421418
PatKind::Range(_, Some(_), Spanned { node: RangeEnd::Excluded, .. }) => {
422419
gate!(
@@ -610,10 +607,7 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session, features: &Features) {
610607
};
611608
}
612609

613-
if !visitor.features.deref_patterns {
614-
// Allow box patterns under `deref_patterns`.
615-
gate_all_legacy_dont_use!(box_patterns, "box pattern syntax is experimental");
616-
}
610+
gate_all_legacy_dont_use!(box_patterns, "box pattern syntax is experimental");
617611
gate_all_legacy_dont_use!(trait_alias, "trait aliases are experimental");
618612
// Despite being a new feature, `where T: Trait<Assoc(): Sized>`, which is RTN syntax now,
619613
// used to be gated under associated_type_bounds, which are right above, so RTN needs to

compiler/rustc_ast_pretty/src/pprust/state.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1626,6 +1626,12 @@ impl<'a> State<'a> {
16261626
self.word("box ");
16271627
self.print_pat(inner);
16281628
}
1629+
PatKind::Deref(inner) => {
1630+
self.word("deref!");
1631+
self.popen();
1632+
self.print_pat(inner);
1633+
self.pclose();
1634+
}
16291635
PatKind::Ref(inner, mutbl) => {
16301636
self.word("&");
16311637
if mutbl.is_mut() {

compiler/rustc_borrowck/src/region_infer/opaque_types.rs

+5
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,11 @@ impl<'tcx> RegionInferenceContext<'tcx> {
192192
.find(|ur_vid| self.eval_equal(vid, **ur_vid))
193193
.and_then(|ur_vid| self.definitions[*ur_vid].external_name)
194194
.unwrap_or(infcx.tcx.lifetimes.re_erased),
195+
ty::RePlaceholder(_) => ty::Region::new_error_with_message(
196+
infcx.tcx,
197+
concrete_type.span,
198+
"hidden type contains placeholders, we don't support higher kinded opaques yet",
199+
),
195200
_ => region,
196201
});
197202
debug!(?universal_concrete_type);

compiler/rustc_codegen_ssa/src/traits/backend.rs

-7
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use rustc_session::{
2121
};
2222
use rustc_span::symbol::Symbol;
2323
use rustc_target::abi::call::FnAbi;
24-
use rustc_target::spec::Target;
2524

2625
use std::fmt;
2726

@@ -70,12 +69,6 @@ pub trait CodegenBackend {
7069
fn print_passes(&self) {}
7170
fn print_version(&self) {}
7271

73-
/// If this plugin provides additional builtin targets, provide the one enabled by the options here.
74-
/// Be careful: this is called *before* init() is called.
75-
fn target_override(&self, _opts: &config::Options) -> Option<Target> {
76-
None
77-
}
78-
7972
/// The metadata loader used to load rlib and dylib metadata.
8073
///
8174
/// Alternative codegen backends may want to use different rlib or dylib formats than the

compiler/rustc_driver_impl/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -890,7 +890,7 @@ pub fn version_at_macro_invocation(
890890
let backend_name = debug_flags.iter().find_map(|x| x.strip_prefix("codegen-backend="));
891891
let opts = config::Options::default();
892892
let sysroot = filesearch::materialize_sysroot(opts.maybe_sysroot.clone());
893-
let target = config::build_target_config(early_dcx, &opts, None, &sysroot);
893+
let target = config::build_target_config(early_dcx, &opts, &sysroot);
894894

895895
get_codegen_backend(early_dcx, &sysroot, backend_name, &target).print_version();
896896
}
@@ -1100,7 +1100,7 @@ pub fn describe_flag_categories(early_dcx: &EarlyDiagCtxt, matches: &Matches) ->
11001100

11011101
let opts = config::Options::default();
11021102
let sysroot = filesearch::materialize_sysroot(opts.maybe_sysroot.clone());
1103-
let target = config::build_target_config(early_dcx, &opts, None, &sysroot);
1103+
let target = config::build_target_config(early_dcx, &opts, &sysroot);
11041104

11051105
get_codegen_backend(early_dcx, &sysroot, backend_name, &target).print_passes();
11061106
return true;

compiler/rustc_expand/src/mbe/macro_parser.rs

+6-11
Original file line numberDiff line numberDiff line change
@@ -392,12 +392,7 @@ pub(super) fn count_metavar_decls(matcher: &[TokenTree]) -> usize {
392392
#[derive(Debug, Clone)]
393393
pub(crate) enum NamedMatch {
394394
MatchedSeq(Vec<NamedMatch>),
395-
396-
// A metavar match of type `tt`.
397-
MatchedTokenTree(rustc_ast::tokenstream::TokenTree),
398-
399-
// A metavar match of any type other than `tt`.
400-
MatchedNonterminal(Lrc<(Nonterminal, rustc_span::Span)>),
395+
MatchedSingle(ParseNtResult<Lrc<(Nonterminal, Span)>>),
401396
}
402397

403398
/// Performs a token equality check, ignoring syntax context (that is, an unhygienic comparison)
@@ -691,11 +686,11 @@ impl TtParser {
691686
}
692687
Ok(nt) => nt,
693688
};
694-
let m = match nt {
695-
ParseNtResult::Nt(nt) => MatchedNonterminal(Lrc::new((nt, span))),
696-
ParseNtResult::Tt(tt) => MatchedTokenTree(tt),
697-
};
698-
mp.push_match(next_metavar, seq_depth, m);
689+
mp.push_match(
690+
next_metavar,
691+
seq_depth,
692+
MatchedSingle(nt.map_nt(|nt| (Lrc::new((nt, span))))),
693+
);
699694
mp.idx += 1;
700695
} else {
701696
unreachable!()

compiler/rustc_expand/src/mbe/macro_rules.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::mbe;
55
use crate::mbe::diagnostics::{annotate_doc_comment, parse_failure_msg};
66
use crate::mbe::macro_check;
77
use crate::mbe::macro_parser::{Error, ErrorReported, Failure, Success, TtParser};
8-
use crate::mbe::macro_parser::{MatchedSeq, MatchedTokenTree, MatcherLoc};
8+
use crate::mbe::macro_parser::{MatcherLoc, NamedMatch::*};
99
use crate::mbe::transcribe::transcribe;
1010

1111
use ast::token::IdentIsRaw;
@@ -22,7 +22,7 @@ use rustc_lint_defs::builtin::{
2222
RUST_2021_INCOMPATIBLE_OR_PATTERNS, SEMICOLON_IN_EXPRESSIONS_FROM_MACROS,
2323
};
2424
use rustc_lint_defs::BuiltinLintDiag;
25-
use rustc_parse::parser::{Parser, Recovery};
25+
use rustc_parse::parser::{ParseNtResult, Parser, Recovery};
2626
use rustc_session::parse::ParseSess;
2727
use rustc_session::Session;
2828
use rustc_span::edition::Edition;
@@ -479,7 +479,7 @@ pub fn compile_declarative_macro(
479479
MatchedSeq(s) => s
480480
.iter()
481481
.map(|m| {
482-
if let MatchedTokenTree(tt) = m {
482+
if let MatchedSingle(ParseNtResult::Tt(tt)) = m {
483483
let tt = mbe::quoted::parse(
484484
&TokenStream::new(vec![tt.clone()]),
485485
true,
@@ -505,7 +505,7 @@ pub fn compile_declarative_macro(
505505
MatchedSeq(s) => s
506506
.iter()
507507
.map(|m| {
508-
if let MatchedTokenTree(tt) = m {
508+
if let MatchedSingle(ParseNtResult::Tt(tt)) = m {
509509
return mbe::quoted::parse(
510510
&TokenStream::new(vec![tt.clone()]),
511511
false,

compiler/rustc_expand/src/mbe/transcribe.rs

+15-16
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ use crate::errors::{
33
CountRepetitionMisplaced, MetaVarExprUnrecognizedVar, MetaVarsDifSeqMatchers, MustRepeatOnce,
44
NoSyntaxVarsExprRepeat, VarStillRepeating,
55
};
6-
use crate::mbe::macro_parser::{MatchedNonterminal, MatchedSeq, MatchedTokenTree, NamedMatch};
6+
use crate::mbe::macro_parser::{NamedMatch, NamedMatch::*};
77
use crate::mbe::{self, KleeneOp, MetaVarExpr};
88
use rustc_ast::mut_visit::{self, MutVisitor};
99
use rustc_ast::token::{self, Delimiter, Token, TokenKind};
1010
use rustc_ast::tokenstream::{DelimSpacing, DelimSpan, Spacing, TokenStream, TokenTree};
1111
use rustc_data_structures::fx::FxHashMap;
12-
use rustc_errors::Diag;
13-
use rustc_errors::{pluralize, PResult};
12+
use rustc_errors::{pluralize, Diag, PResult};
13+
use rustc_parse::parser::ParseNtResult;
1414
use rustc_span::hygiene::{LocalExpnId, Transparency};
1515
use rustc_span::symbol::{sym, Ident, MacroRulesNormalizedIdent};
1616
use rustc_span::{with_metavar_spans, Span, SyntaxContext};
@@ -250,26 +250,25 @@ pub(super) fn transcribe<'a>(
250250
// the meta-var.
251251
let ident = MacroRulesNormalizedIdent::new(original_ident);
252252
if let Some(cur_matched) = lookup_cur_matched(ident, interp, &repeats) {
253-
match cur_matched {
254-
MatchedTokenTree(tt) => {
253+
let tt = match cur_matched {
254+
MatchedSingle(ParseNtResult::Tt(tt)) => {
255255
// `tt`s are emitted into the output stream directly as "raw tokens",
256256
// without wrapping them into groups.
257-
let tt = maybe_use_metavar_location(cx, &stack, sp, tt, &mut marker);
258-
result.push(tt);
257+
maybe_use_metavar_location(cx, &stack, sp, tt, &mut marker)
259258
}
260-
MatchedNonterminal(nt) => {
259+
MatchedSingle(ParseNtResult::Nt(nt)) => {
261260
// Other variables are emitted into the output stream as groups with
262261
// `Delimiter::Invisible` to maintain parsing priorities.
263262
// `Interpolated` is currently used for such groups in rustc parser.
264263
marker.visit_span(&mut sp);
265-
result
266-
.push(TokenTree::token_alone(token::Interpolated(nt.clone()), sp));
264+
TokenTree::token_alone(token::Interpolated(nt.clone()), sp)
267265
}
268266
MatchedSeq(..) => {
269267
// We were unable to descend far enough. This is an error.
270268
return Err(cx.dcx().create_err(VarStillRepeating { span: sp, ident }));
271269
}
272-
}
270+
};
271+
result.push(tt)
273272
} else {
274273
// If we aren't able to match the meta-var, we push it back into the result but
275274
// with modified syntax context. (I believe this supports nested macros).
@@ -424,7 +423,7 @@ fn lookup_cur_matched<'a>(
424423
interpolations.get(&ident).map(|mut matched| {
425424
for &(idx, _) in repeats {
426425
match matched {
427-
MatchedTokenTree(_) | MatchedNonterminal(_) => break,
426+
MatchedSingle(_) => break,
428427
MatchedSeq(ads) => matched = ads.get(idx).unwrap(),
429428
}
430429
}
@@ -514,7 +513,7 @@ fn lockstep_iter_size(
514513
let name = MacroRulesNormalizedIdent::new(*name);
515514
match lookup_cur_matched(name, interpolations, repeats) {
516515
Some(matched) => match matched {
517-
MatchedTokenTree(_) | MatchedNonterminal(_) => LockstepIterSize::Unconstrained,
516+
MatchedSingle(_) => LockstepIterSize::Unconstrained,
518517
MatchedSeq(ads) => LockstepIterSize::Constraint(ads.len(), name),
519518
},
520519
_ => LockstepIterSize::Unconstrained,
@@ -557,7 +556,7 @@ fn count_repetitions<'a>(
557556
// (or at the top-level of `matched` if no depth is given).
558557
fn count<'a>(depth_curr: usize, depth_max: usize, matched: &NamedMatch) -> PResult<'a, usize> {
559558
match matched {
560-
MatchedTokenTree(_) | MatchedNonterminal(_) => Ok(1),
559+
MatchedSingle(_) => Ok(1),
561560
MatchedSeq(named_matches) => {
562561
if depth_curr == depth_max {
563562
Ok(named_matches.len())
@@ -571,7 +570,7 @@ fn count_repetitions<'a>(
571570
/// Maximum depth
572571
fn depth(counter: usize, matched: &NamedMatch) -> usize {
573572
match matched {
574-
MatchedTokenTree(_) | MatchedNonterminal(_) => counter,
573+
MatchedSingle(_) => counter,
575574
MatchedSeq(named_matches) => {
576575
let rslt = counter + 1;
577576
if let Some(elem) = named_matches.first() { depth(rslt, elem) } else { rslt }
@@ -599,7 +598,7 @@ fn count_repetitions<'a>(
599598
}
600599
}
601600

602-
if let MatchedTokenTree(_) | MatchedNonterminal(_) = matched {
601+
if let MatchedSingle(_) = matched {
603602
return Err(cx.dcx().create_err(CountRepetitionMisplaced { span: sp.entire() }));
604603
}
605604

compiler/rustc_hir/src/hir.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -1015,7 +1015,7 @@ impl<'hir> Pat<'hir> {
10151015
use PatKind::*;
10161016
match self.kind {
10171017
Wild | Never | Lit(_) | Range(..) | Binding(.., None) | Path(_) | Err(_) => true,
1018-
Box(s) | Ref(s, _) | Binding(.., Some(s)) => s.walk_short_(it),
1018+
Box(s) | Deref(s) | Ref(s, _) | Binding(.., Some(s)) => s.walk_short_(it),
10191019
Struct(_, fields, _) => fields.iter().all(|field| field.pat.walk_short_(it)),
10201020
TupleStruct(_, s, _) | Tuple(s, _) | Or(s) => s.iter().all(|p| p.walk_short_(it)),
10211021
Slice(before, slice, after) => {
@@ -1042,7 +1042,7 @@ impl<'hir> Pat<'hir> {
10421042
use PatKind::*;
10431043
match self.kind {
10441044
Wild | Never | Lit(_) | Range(..) | Binding(.., None) | Path(_) | Err(_) => {}
1045-
Box(s) | Ref(s, _) | Binding(.., Some(s)) => s.walk_(it),
1045+
Box(s) | Deref(s) | Ref(s, _) | Binding(.., Some(s)) => s.walk_(it),
10461046
Struct(_, fields, _) => fields.iter().for_each(|field| field.pat.walk_(it)),
10471047
TupleStruct(_, s, _) | Tuple(s, _) | Or(s) => s.iter().for_each(|p| p.walk_(it)),
10481048
Slice(before, slice, after) => {
@@ -1185,6 +1185,9 @@ pub enum PatKind<'hir> {
11851185
/// A `box` pattern.
11861186
Box(&'hir Pat<'hir>),
11871187

1188+
/// A `deref` pattern (currently `deref!()` macro-based syntax).
1189+
Deref(&'hir Pat<'hir>),
1190+
11881191
/// A reference pattern (e.g., `&mut (a, b)`).
11891192
Ref(&'hir Pat<'hir>, Mutability),
11901193

compiler/rustc_hir/src/intravisit.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,9 @@ pub fn walk_pat<'v, V: Visitor<'v>>(visitor: &mut V, pattern: &'v Pat<'v>) -> V:
660660
PatKind::Tuple(tuple_elements, _) => {
661661
walk_list!(visitor, visit_pat, tuple_elements);
662662
}
663-
PatKind::Box(ref subpattern) | PatKind::Ref(ref subpattern, _) => {
663+
PatKind::Box(ref subpattern)
664+
| PatKind::Deref(ref subpattern)
665+
| PatKind::Ref(ref subpattern, _) => {
664666
try_visit!(visitor.visit_pat(subpattern));
665667
}
666668
PatKind::Binding(_, _hir_id, ident, ref optional_subpattern) => {

0 commit comments

Comments
 (0)