Skip to content

Commit e85edd9

Browse files
committed
Auto merge of #96528 - Dylan-DPC:rollup-iedbjli, r=Dylan-DPC
Rollup of 5 pull requests Successful merges: - #95312 (Ensure that `'_` and GAT yields errors) - #96405 (Migrate ambiguous plus diagnostic to the new derive macro) - #96409 (Recover suggestions to introduce named lifetime under NLL) - #96433 (rustc_ast: Harmonize delimiter naming with `proc_macro::Delimiter`) - #96480 (Fixed grammatical error in example comment) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 1388b38 + 2c1d58b commit e85edd9

File tree

70 files changed

+851
-518
lines changed

Some content is hidden

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

70 files changed

+851
-518
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -4158,6 +4158,7 @@ dependencies = [
41584158
"rustc_errors",
41594159
"rustc_feature",
41604160
"rustc_lexer",
4161+
"rustc_macros",
41614162
"rustc_session",
41624163
"rustc_span",
41634164
"tracing",

compiler/rustc_ast/src/ast.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub use GenericArgs::*;
2323
pub use UnsafeSource::*;
2424

2525
use crate::ptr::P;
26-
use crate::token::{self, CommentKind, DelimToken, Token};
26+
use crate::token::{self, CommentKind, Delimiter, Token};
2727
use crate::tokenstream::{DelimSpan, LazyTokenStream, TokenStream, TokenTree};
2828

2929
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
@@ -1542,7 +1542,7 @@ pub enum MacArgs {
15421542
}
15431543

15441544
impl MacArgs {
1545-
pub fn delim(&self) -> Option<DelimToken> {
1545+
pub fn delim(&self) -> Option<Delimiter> {
15461546
match self {
15471547
MacArgs::Delimited(_, delim, _) => Some(delim.to_token()),
15481548
MacArgs::Empty | MacArgs::Eq(..) => None,
@@ -1582,20 +1582,20 @@ pub enum MacDelimiter {
15821582
}
15831583

15841584
impl MacDelimiter {
1585-
pub fn to_token(self) -> DelimToken {
1585+
pub fn to_token(self) -> Delimiter {
15861586
match self {
1587-
MacDelimiter::Parenthesis => DelimToken::Paren,
1588-
MacDelimiter::Bracket => DelimToken::Bracket,
1589-
MacDelimiter::Brace => DelimToken::Brace,
1587+
MacDelimiter::Parenthesis => Delimiter::Parenthesis,
1588+
MacDelimiter::Bracket => Delimiter::Bracket,
1589+
MacDelimiter::Brace => Delimiter::Brace,
15901590
}
15911591
}
15921592

1593-
pub fn from_token(delim: DelimToken) -> Option<MacDelimiter> {
1593+
pub fn from_token(delim: Delimiter) -> Option<MacDelimiter> {
15941594
match delim {
1595-
token::Paren => Some(MacDelimiter::Parenthesis),
1596-
token::Bracket => Some(MacDelimiter::Bracket),
1597-
token::Brace => Some(MacDelimiter::Brace),
1598-
token::NoDelim => None,
1595+
Delimiter::Parenthesis => Some(MacDelimiter::Parenthesis),
1596+
Delimiter::Bracket => Some(MacDelimiter::Bracket),
1597+
Delimiter::Brace => Some(MacDelimiter::Brace),
1598+
Delimiter::Invisible => None,
15991599
}
16001600
}
16011601
}

compiler/rustc_ast/src/attr/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::ast::{AttrId, AttrItem, AttrKind, AttrStyle, Attribute};
55
use crate::ast::{Lit, LitKind};
66
use crate::ast::{MacArgs, MacDelimiter, MetaItem, MetaItemKind, NestedMetaItem};
77
use crate::ast::{Path, PathSegment};
8-
use crate::token::{self, CommentKind, Token};
8+
use crate::token::{self, CommentKind, Delimiter, Token};
99
use crate::tokenstream::{AttrAnnotatedTokenStream, AttrAnnotatedTokenTree};
1010
use crate::tokenstream::{DelimSpan, Spacing, TokenTree, TreeAndSpacing};
1111
use crate::tokenstream::{LazyTokenStream, TokenStream};
@@ -513,7 +513,7 @@ impl MetaItemKind {
513513
vec![
514514
TokenTree::Delimited(
515515
DelimSpan::from_single(span),
516-
token::Paren,
516+
Delimiter::Parenthesis,
517517
TokenStream::new(tokens),
518518
)
519519
.into(),
@@ -540,7 +540,7 @@ impl MetaItemKind {
540540
tokens: &mut impl Iterator<Item = TokenTree>,
541541
) -> Option<MetaItemKind> {
542542
match tokens.next() {
543-
Some(TokenTree::Delimited(_, token::NoDelim, inner_tokens)) => {
543+
Some(TokenTree::Delimited(_, Delimiter::Invisible, inner_tokens)) => {
544544
MetaItemKind::name_value_from_tokens(&mut inner_tokens.trees())
545545
}
546546
Some(TokenTree::Token(token)) => {
@@ -565,7 +565,7 @@ impl MetaItemKind {
565565
tokens: &mut iter::Peekable<impl Iterator<Item = TokenTree>>,
566566
) -> Option<MetaItemKind> {
567567
match tokens.peek() {
568-
Some(TokenTree::Delimited(_, token::Paren, inner_tokens)) => {
568+
Some(TokenTree::Delimited(_, Delimiter::Parenthesis, inner_tokens)) => {
569569
let inner_tokens = inner_tokens.clone();
570570
tokens.next();
571571
MetaItemKind::list_from_tokens(inner_tokens)
@@ -606,7 +606,7 @@ impl NestedMetaItem {
606606
tokens.next();
607607
return Some(NestedMetaItem::Literal(lit));
608608
}
609-
Some(TokenTree::Delimited(_, token::NoDelim, inner_tokens)) => {
609+
Some(TokenTree::Delimited(_, Delimiter::Invisible, inner_tokens)) => {
610610
let inner_tokens = inner_tokens.clone();
611611
tokens.next();
612612
return NestedMetaItem::from_tokens(&mut inner_tokens.into_trees().peekable());

compiler/rustc_ast/src/token.rs

+25-18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
pub use BinOpToken::*;
2-
pub use DelimToken::*;
32
pub use LitKind::*;
43
pub use Nonterminal::*;
54
pub use TokenKind::*;
@@ -37,18 +36,26 @@ pub enum BinOpToken {
3736
Shr,
3837
}
3938

40-
/// A delimiter token.
41-
#[derive(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Debug, Copy)]
42-
#[derive(HashStable_Generic)]
43-
pub enum DelimToken {
44-
/// A round parenthesis (i.e., `(` or `)`).
45-
Paren,
46-
/// A square bracket (i.e., `[` or `]`).
47-
Bracket,
48-
/// A curly brace (i.e., `{` or `}`).
39+
/// Describes how a sequence of token trees is delimited.
40+
/// Cannot use `proc_macro::Delimiter` directly because this
41+
/// structure should implement some additional traits.
42+
/// The `None` variant is also renamed to `Invisible` to be
43+
/// less confusing and better convey the semantics.
44+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
45+
#[derive(Encodable, Decodable, Hash, HashStable_Generic)]
46+
pub enum Delimiter {
47+
/// `( ... )`
48+
Parenthesis,
49+
/// `{ ... }`
4950
Brace,
50-
/// An empty delimiter.
51-
NoDelim,
51+
/// `[ ... ]`
52+
Bracket,
53+
/// `Ø ... Ø`
54+
/// An invisible delimiter, that may, for example, appear around tokens coming from a
55+
/// "macro variable" `$var`. It is important to preserve operator priorities in cases like
56+
/// `$var * 3` where `$var` is `1 + 2`.
57+
/// Invisible delimiters might not survive roundtrip of a token stream through a string.
58+
Invisible,
5259
}
5360

5461
#[derive(Clone, Copy, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]
@@ -212,9 +219,9 @@ pub enum TokenKind {
212219
/// Used by proc macros for representing lifetimes, not generated by lexer right now.
213220
SingleQuote,
214221
/// An opening delimiter (e.g., `{`).
215-
OpenDelim(DelimToken),
222+
OpenDelim(Delimiter),
216223
/// A closing delimiter (e.g., `}`).
217-
CloseDelim(DelimToken),
224+
CloseDelim(Delimiter),
218225

219226
/* Literals */
220227
Literal(Lit),
@@ -387,8 +394,8 @@ impl Token {
387394
match self.uninterpolate().kind {
388395
Ident(name, is_raw) =>
389396
ident_can_begin_type(name, self.span, is_raw), // type name or keyword
390-
OpenDelim(Paren) | // tuple
391-
OpenDelim(Bracket) | // array
397+
OpenDelim(Delimiter::Parenthesis) | // tuple
398+
OpenDelim(Delimiter::Bracket) | // array
392399
Not | // never
393400
BinOp(Star) | // raw pointer
394401
BinOp(And) | // reference
@@ -405,7 +412,7 @@ impl Token {
405412
/// Returns `true` if the token can appear at the start of a const param.
406413
pub fn can_begin_const_arg(&self) -> bool {
407414
match self.kind {
408-
OpenDelim(Brace) => true,
415+
OpenDelim(Delimiter::Brace) => true,
409416
Interpolated(ref nt) => matches!(**nt, NtExpr(..) | NtBlock(..) | NtLiteral(..)),
410417
_ => self.can_begin_literal_maybe_minus(),
411418
}
@@ -417,7 +424,7 @@ impl Token {
417424
|| self.is_lifetime()
418425
|| self.is_keyword(kw::For)
419426
|| self == &Question
420-
|| self == &OpenDelim(Paren)
427+
|| self == &OpenDelim(Delimiter::Parenthesis)
421428
}
422429

423430
/// Returns `true` if the token is any literal.

compiler/rustc_ast/src/tokenstream.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
//! and a borrowed `TokenStream` is sufficient to build an owned `TokenStream` without taking
1414
//! ownership of the original.
1515
16-
use crate::token::{self, DelimToken, Token, TokenKind};
16+
use crate::token::{self, Delimiter, Token, TokenKind};
1717
use crate::AttrVec;
1818

1919
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
@@ -42,7 +42,7 @@ pub enum TokenTree {
4242
/// A single token.
4343
Token(Token),
4444
/// A delimited sequence of token trees.
45-
Delimited(DelimSpan, DelimToken, TokenStream),
45+
Delimited(DelimSpan, Delimiter, TokenStream),
4646
}
4747

4848
#[derive(Copy, Clone)]
@@ -57,7 +57,7 @@ fn _dummy()
5757
where
5858
Token: Send + Sync,
5959
DelimSpan: Send + Sync,
60-
DelimToken: Send + Sync,
60+
Delimiter: Send + Sync,
6161
TokenStream: Send + Sync,
6262
{
6363
}
@@ -175,7 +175,7 @@ pub struct AttrAnnotatedTokenStream(pub Lrc<Vec<(AttrAnnotatedTokenTree, Spacing
175175
#[derive(Clone, Debug, Encodable, Decodable)]
176176
pub enum AttrAnnotatedTokenTree {
177177
Token(Token),
178-
Delimited(DelimSpan, DelimToken, AttrAnnotatedTokenStream),
178+
Delimited(DelimSpan, Delimiter, AttrAnnotatedTokenStream),
179179
/// Stores the attributes for an attribute target,
180180
/// along with the tokens for that attribute target.
181181
/// See `AttributesData` for more information

compiler/rustc_ast_lowering/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
#![recursion_limit = "256"]
3939
#![allow(rustc::potential_query_instability)]
4040

41-
use rustc_ast::token::{self, Token};
41+
use rustc_ast::token::{Delimiter, Token};
4242
use rustc_ast::tokenstream::{CanSynthesizeMissingTokens, TokenStream, TokenTree};
4343
use rustc_ast::visit;
4444
use rustc_ast::{self as ast, *};
@@ -886,7 +886,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
886886
match tokens.into_trees().next() {
887887
Some(TokenTree::Token(token)) => token,
888888
Some(TokenTree::Delimited(_, delim, tokens)) => {
889-
if delim != token::NoDelim {
889+
if delim != Delimiter::Invisible {
890890
sess.diagnostic().delay_span_bug(
891891
span,
892892
"unexpected delimiter in key-value attribute's value",

compiler/rustc_ast_pretty/src/pprust/state.rs

+15-13
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::pp::Breaks::{Consistent, Inconsistent};
66
use crate::pp::{self, Breaks};
77

88
use rustc_ast::ptr::P;
9-
use rustc_ast::token::{self, BinOpToken, CommentKind, DelimToken, Nonterminal, Token, TokenKind};
9+
use rustc_ast::token::{self, BinOpToken, CommentKind, Delimiter, Nonterminal, Token, TokenKind};
1010
use rustc_ast::tokenstream::{TokenStream, TokenTree};
1111
use rustc_ast::util::classify;
1212
use rustc_ast::util::comments::{gather_comments, Comment, CommentStyle};
@@ -155,10 +155,10 @@ fn tt_prepend_space(tt: &TokenTree, prev: &TokenTree) -> bool {
155155
}
156156
match tt {
157157
TokenTree::Token(token) => !matches!(token.kind, token::Comma | token::Not | token::Dot),
158-
TokenTree::Delimited(_, DelimToken::Paren, _) => {
158+
TokenTree::Delimited(_, Delimiter::Parenthesis, _) => {
159159
!matches!(prev, TokenTree::Token(Token { kind: token::Ident(..), .. }))
160160
}
161-
TokenTree::Delimited(_, DelimToken::Bracket, _) => {
161+
TokenTree::Delimited(_, Delimiter::Bracket, _) => {
162162
!matches!(prev, TokenTree::Token(Token { kind: token::Pound, .. }))
163163
}
164164
TokenTree::Delimited(..) => true,
@@ -556,12 +556,12 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
556556
header: Option<MacHeader<'_>>,
557557
has_bang: bool,
558558
ident: Option<Ident>,
559-
delim: Option<DelimToken>,
559+
delim: Option<Delimiter>,
560560
tts: &TokenStream,
561561
convert_dollar_crate: bool,
562562
span: Span,
563563
) {
564-
if delim == Some(DelimToken::Brace) {
564+
if delim == Some(Delimiter::Brace) {
565565
self.cbox(INDENT_UNIT);
566566
}
567567
match header {
@@ -577,7 +577,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
577577
self.print_ident(ident);
578578
}
579579
match delim {
580-
Some(DelimToken::Brace) => {
580+
Some(Delimiter::Brace) => {
581581
if header.is_some() || has_bang || ident.is_some() {
582582
self.nbsp();
583583
}
@@ -758,13 +758,15 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
758758
token::RArrow => "->".into(),
759759
token::LArrow => "<-".into(),
760760
token::FatArrow => "=>".into(),
761-
token::OpenDelim(token::Paren) => "(".into(),
762-
token::CloseDelim(token::Paren) => ")".into(),
763-
token::OpenDelim(token::Bracket) => "[".into(),
764-
token::CloseDelim(token::Bracket) => "]".into(),
765-
token::OpenDelim(token::Brace) => "{".into(),
766-
token::CloseDelim(token::Brace) => "}".into(),
767-
token::OpenDelim(token::NoDelim) | token::CloseDelim(token::NoDelim) => "".into(),
761+
token::OpenDelim(Delimiter::Parenthesis) => "(".into(),
762+
token::CloseDelim(Delimiter::Parenthesis) => ")".into(),
763+
token::OpenDelim(Delimiter::Bracket) => "[".into(),
764+
token::CloseDelim(Delimiter::Bracket) => "]".into(),
765+
token::OpenDelim(Delimiter::Brace) => "{".into(),
766+
token::CloseDelim(Delimiter::Brace) => "}".into(),
767+
token::OpenDelim(Delimiter::Invisible) | token::CloseDelim(Delimiter::Invisible) => {
768+
"".into()
769+
}
768770
token::Pound => "#".into(),
769771
token::Dollar => "$".into(),
770772
token::Question => "?".into(),

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

+34-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
33
use rustc_errors::{Diagnostic, DiagnosticBuilder, ErrorGuaranteed};
44
use rustc_infer::infer::{
5-
error_reporting::nice_region_error::{self, find_param_with_region, NiceRegionError},
5+
error_reporting::nice_region_error::{
6+
self, find_anon_type, find_param_with_region, suggest_adding_lifetime_params,
7+
NiceRegionError,
8+
},
69
error_reporting::unexpected_hidden_region_diagnostic,
710
NllRegionVariableOrigin, RelateParamBound,
811
};
@@ -630,6 +633,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
630633
}
631634

632635
self.add_static_impl_trait_suggestion(&mut diag, *fr, fr_name, *outlived_fr);
636+
self.suggest_adding_lifetime_params(&mut diag, *fr, *outlived_fr);
633637

634638
diag
635639
}
@@ -694,4 +698,33 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
694698
);
695699
}
696700
}
701+
702+
fn suggest_adding_lifetime_params(
703+
&self,
704+
diag: &mut Diagnostic,
705+
sub: RegionVid,
706+
sup: RegionVid,
707+
) {
708+
let (Some(sub), Some(sup)) = (self.to_error_region(sub), self.to_error_region(sup)) else {
709+
return
710+
};
711+
712+
let Some((ty_sub, _)) = self
713+
.infcx
714+
.tcx
715+
.is_suitable_region(sub)
716+
.and_then(|anon_reg| find_anon_type(self.infcx.tcx, sub, &anon_reg.boundregion)) else {
717+
return
718+
};
719+
720+
let Some((ty_sup, _)) = self
721+
.infcx
722+
.tcx
723+
.is_suitable_region(sup)
724+
.and_then(|anon_reg| find_anon_type(self.infcx.tcx, sup, &anon_reg.boundregion)) else {
725+
return
726+
};
727+
728+
suggest_adding_lifetime_params(self.infcx.tcx, sub, ty_sup, ty_sub, diag);
729+
}
697730
}

0 commit comments

Comments
 (0)