Skip to content

Commit 2fe69a9

Browse files
authored
Rollup merge of #148673 - fmease:del-dyn_star-remnant, r=JonathanBrouwer
Remove a remnant of `dyn*` from the parser Follow-up to #146664 and #143036. `is_explicit_dyn_type` still checked for `TokenKind::Star` which made no sense now that `dyn*` is no more. Removing it doesn't represent a functional change and merely affects diagnostics. That's because the check only dictated whether to interpret `dyn` as the start of a trait object type in Rust 2015 (where this identifier is only a *contextual* keyword). However, we would still fail at the `*` later on as it doesn't start a bound. While at it, I also took the time to clean up in the vicinity.
2 parents ec4e6e6 + 474501b commit 2fe69a9

File tree

1 file changed

+19
-25
lines changed
  • compiler/rustc_parse/src/parser

1 file changed

+19
-25
lines changed

compiler/rustc_parse/src/parser/ty.rs

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -82,25 +82,24 @@ enum AllowCVariadic {
8282
No,
8383
}
8484

85-
/// Returns `true` if `IDENT t` can start a type -- `IDENT::a::b`, `IDENT<u8, u8>`,
86-
/// `IDENT<<u8 as Trait>::AssocTy>`.
85+
/// Determine if the given token can begin a bound assuming it follows Rust 2015 identifier `dyn`.
8786
///
88-
/// Types can also be of the form `IDENT(u8, u8) -> u8`, however this assumes
89-
/// that `IDENT` is not the ident of a fn trait.
90-
fn can_continue_type_after_non_fn_ident(t: &Token) -> bool {
91-
t == &token::PathSep || t == &token::Lt || t == &token::Shl
92-
}
87+
/// In Rust 2015, `dyn` is a contextual keyword, not a full one.
88+
fn can_begin_dyn_bound_in_edition_2015(t: Token) -> bool {
89+
if t.is_path_start() {
90+
// In `dyn::x`, `dyn<X>` and `dyn<<X>::Y>`, `dyn` should (continue to) denote a regular path
91+
// segment for backward compatibility. We make an exception for `dyn(X)` which used to be
92+
// interpreted as a path with parenthesized generic arguments which can be semantically
93+
// well-formed (consider: `use std::ops::Fn as dyn;`). Instead, we treat it as a trait
94+
// object type whose first bound is parenthesized.
95+
return t != token::PathSep && t != token::Lt && t != token::Shl;
96+
}
9397

94-
fn can_begin_dyn_bound_in_edition_2015(t: &Token) -> bool {
95-
// `!`, `const`, `[`, `async` are deliberately not part of this list to
96-
// contain the number of potential regressions esp. in MBE code.
97-
// `const` and `[` would regress UI test `macro-dyn-const-2015.rs`.
98-
// `!` would regress `dyn!(...)` macro calls in Rust 2015.
99-
t.is_path_start()
100-
|| t.is_lifetime()
101-
|| t == &TokenKind::Question
102-
|| t.is_keyword(kw::For)
103-
|| t == &TokenKind::OpenParen
98+
// Contrary to `Parser::can_begin_bound`, `!`, `const`, `[` and `async` are deliberately not
99+
// part of this list to contain the number of potential regressions esp. in MBE code.
100+
// `const` and `[` would regress UI test `macro-dyn-const-2015.rs` and
101+
// `!` would regress `dyn!(...)` macro calls in Rust 2015 for example.
102+
t == token::OpenParen || t == token::Question || t.is_lifetime() || t.is_keyword(kw::For)
104103
}
105104

106105
impl<'a> Parser<'a> {
@@ -969,10 +968,7 @@ impl<'a> Parser<'a> {
969968
fn is_explicit_dyn_type(&mut self) -> bool {
970969
self.check_keyword(exp!(Dyn))
971970
&& (self.token_uninterpolated_span().at_least_rust_2018()
972-
|| self.look_ahead(1, |t| {
973-
(can_begin_dyn_bound_in_edition_2015(t) || *t == TokenKind::Star)
974-
&& !can_continue_type_after_non_fn_ident(t)
975-
}))
971+
|| self.look_ahead(1, |&t| can_begin_dyn_bound_in_edition_2015(t)))
976972
}
977973

978974
/// Parses a `dyn B0 + ... + Bn` type.
@@ -981,13 +977,11 @@ impl<'a> Parser<'a> {
981977
fn parse_dyn_ty(&mut self, impl_dyn_multi: &mut bool) -> PResult<'a, TyKind> {
982978
self.bump(); // `dyn`
983979

984-
// We used to parse `*` for `dyn*` here.
985-
let syntax = TraitObjectSyntax::Dyn;
986-
987980
// Always parse bounds greedily for better error recovery.
988981
let bounds = self.parse_generic_bounds()?;
989982
*impl_dyn_multi = bounds.len() > 1 || self.prev_token == TokenKind::Plus;
990-
Ok(TyKind::TraitObject(bounds, syntax))
983+
984+
Ok(TyKind::TraitObject(bounds, TraitObjectSyntax::Dyn))
991985
}
992986

993987
/// Parses a type starting with a path.

0 commit comments

Comments
 (0)