1use std::fmt::Write;
2use std::mem;
3
4use ast::token::IdentIsRaw;
5use rustc_ast::ast::*;
6use rustc_ast::token::{self, Delimiter, InvisibleOrigin, MetaVarKind, TokenKind};
7use rustc_ast::tokenstream::{DelimSpan, TokenStream, TokenTree};
8use rustc_ast::util::case::Case;
9use rustc_ast::{self as ast};
10use rustc_ast_pretty::pprust;
11use rustc_errors::codes::*;
12use rustc_errors::{Applicability, PResult, StashKey, inline_fluent, struct_span_code_err};
13use rustc_session::lint::builtin::VARARGS_WITHOUT_PATTERN;
14use rustc_span::edit_distance::edit_distance;
15use rustc_span::edition::Edition;
16use rustc_span::{DUMMY_SP, ErrorGuaranteed, Ident, Span, Symbol, kw, source_map, sym};
17use thin_vec::{ThinVec, thin_vec};
18use tracing::debug;
19
20use super::diagnostics::{ConsumeClosingDelim, dummy_arg};
21use super::ty::{AllowPlus, RecoverQPath, RecoverReturnSign};
22use super::{
23 AllowConstBlockItems, AttrWrapper, ExpKeywordPair, ExpTokenPair, FollowedByType, ForceCollect,
24 Parser, PathStyle, Recovered, Trailing, UsePreAttrPos,
25};
26use crate::errors::{self, FnPointerCannotBeAsync, FnPointerCannotBeConst, MacroExpandsToAdtField};
27use crate::exp;
28
29impl<'a> Parser<'a> {
30 pub fn parse_crate_mod(&mut self) -> PResult<'a, ast::Crate> {
32 let (attrs, items, spans) = self.parse_mod(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Eof,
token_type: crate::parser::token_type::TokenType::Eof,
}exp!(Eof))?;
33 Ok(ast::Crate { attrs, items, spans, id: DUMMY_NODE_ID, is_placeholder: false })
34 }
35
36 fn parse_item_mod(&mut self, attrs: &mut AttrVec) -> PResult<'a, ItemKind> {
38 let safety = self.parse_safety(Case::Sensitive);
39 self.expect_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Mod,
token_type: crate::parser::token_type::TokenType::KwMod,
}exp!(Mod))?;
40 let ident = self.parse_ident()?;
41 let mod_kind = if self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Semi,
token_type: crate::parser::token_type::TokenType::Semi,
}exp!(Semi)) {
42 ModKind::Unloaded
43 } else {
44 self.expect(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBrace,
token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace))?;
45 let (inner_attrs, items, inner_span) = self.parse_mod(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::CloseBrace,
token_type: crate::parser::token_type::TokenType::CloseBrace,
}exp!(CloseBrace))?;
46 attrs.extend(inner_attrs);
47 ModKind::Loaded(items, Inline::Yes, inner_span)
48 };
49 Ok(ItemKind::Mod(safety, ident, mod_kind))
50 }
51
52 pub fn parse_mod(
57 &mut self,
58 term: ExpTokenPair,
59 ) -> PResult<'a, (AttrVec, ThinVec<Box<Item>>, ModSpans)> {
60 let lo = self.token.span;
61 let attrs = self.parse_inner_attributes()?;
62
63 let post_attr_lo = self.token.span;
64 let mut items: ThinVec<Box<_>> = ThinVec::new();
65
66 loop {
69 while self.maybe_consume_incorrect_semicolon(items.last().map(|x| &**x)) {} let Some(item) = self.parse_item(ForceCollect::No, AllowConstBlockItems::Yes)? else {
71 break;
72 };
73 items.push(item);
74 }
75
76 if !self.eat(term) {
77 let token_str = super::token_descr(&self.token);
78 if !self.maybe_consume_incorrect_semicolon(items.last().map(|x| &**x)) {
79 let is_let = self.token.is_keyword(kw::Let);
80 let is_let_mut = is_let && self.look_ahead(1, |t| t.is_keyword(kw::Mut));
81 let let_has_ident = is_let && !is_let_mut && self.is_kw_followed_by_ident(kw::Let);
82
83 let msg = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("expected item, found {0}",
token_str))
})format!("expected item, found {token_str}");
84 let mut err = self.dcx().struct_span_err(self.token.span, msg);
85
86 let label = if is_let {
87 "`let` cannot be used for global variables"
88 } else {
89 "expected item"
90 };
91 err.span_label(self.token.span, label);
92
93 if is_let {
94 if is_let_mut {
95 err.help("consider using `static` and a `Mutex` instead of `let mut`");
96 } else if let_has_ident {
97 err.span_suggestion_short(
98 self.token.span,
99 "consider using `static` or `const` instead of `let`",
100 "static",
101 Applicability::MaybeIncorrect,
102 );
103 } else {
104 err.help("consider using `static` or `const` instead of `let`");
105 }
106 }
107 err.note("for a full list of items that can appear in modules, see <https://doc.rust-lang.org/reference/items.html>");
108 return Err(err);
109 }
110 }
111
112 let inject_use_span = post_attr_lo.data().with_hi(post_attr_lo.lo());
113 let mod_spans = ModSpans { inner_span: lo.to(self.prev_token.span), inject_use_span };
114 Ok((attrs, items, mod_spans))
115 }
116}
117
118enum ReuseKind {
119 Path,
120 Impl,
121}
122
123impl<'a> Parser<'a> {
124 pub fn parse_item(
125 &mut self,
126 force_collect: ForceCollect,
127 allow_const_block_items: AllowConstBlockItems,
128 ) -> PResult<'a, Option<Box<Item>>> {
129 let fn_parse_mode =
130 FnParseMode { req_name: |_, _| true, context: FnContext::Free, req_body: true };
131 self.parse_item_(fn_parse_mode, force_collect, allow_const_block_items)
132 .map(|i| i.map(Box::new))
133 }
134
135 fn parse_item_(
136 &mut self,
137 fn_parse_mode: FnParseMode,
138 force_collect: ForceCollect,
139 const_block_items_allowed: AllowConstBlockItems,
140 ) -> PResult<'a, Option<Item>> {
141 self.recover_vcs_conflict_marker();
142 let attrs = self.parse_outer_attributes()?;
143 self.recover_vcs_conflict_marker();
144 self.parse_item_common(
145 attrs,
146 true,
147 false,
148 fn_parse_mode,
149 force_collect,
150 const_block_items_allowed,
151 )
152 }
153
154 pub(super) fn parse_item_common(
155 &mut self,
156 attrs: AttrWrapper,
157 mac_allowed: bool,
158 attrs_allowed: bool,
159 fn_parse_mode: FnParseMode,
160 force_collect: ForceCollect,
161 allow_const_block_items: AllowConstBlockItems,
162 ) -> PResult<'a, Option<Item>> {
163 if let Some(item) = self.eat_metavar_seq(MetaVarKind::Item, |this| {
164 this.parse_item(ForceCollect::Yes, allow_const_block_items)
165 }) {
166 let mut item = item.expect("an actual item");
167 attrs.prepend_to_nt_inner(&mut item.attrs);
168 return Ok(Some(*item));
169 }
170
171 self.collect_tokens(None, attrs, force_collect, |this, mut attrs| {
172 let lo = this.token.span;
173 let vis = this.parse_visibility(FollowedByType::No)?;
174 let mut def = this.parse_defaultness();
175 let kind = this.parse_item_kind(
176 &mut attrs,
177 mac_allowed,
178 allow_const_block_items,
179 lo,
180 &vis,
181 &mut def,
182 fn_parse_mode,
183 Case::Sensitive,
184 )?;
185 if let Some(kind) = kind {
186 this.error_on_unconsumed_default(def, &kind);
187 let span = lo.to(this.prev_token.span);
188 let id = DUMMY_NODE_ID;
189 let item = Item { attrs, id, kind, vis, span, tokens: None };
190 return Ok((Some(item), Trailing::No, UsePreAttrPos::No));
191 }
192
193 if !#[allow(non_exhaustive_omitted_patterns)] match vis.kind {
VisibilityKind::Inherited => true,
_ => false,
}matches!(vis.kind, VisibilityKind::Inherited) {
195 this.dcx().emit_err(errors::VisibilityNotFollowedByItem { span: vis.span, vis });
196 }
197
198 if let Defaultness::Default(span) = def {
199 this.dcx().emit_err(errors::DefaultNotFollowedByItem { span });
200 }
201
202 if !attrs_allowed {
203 this.recover_attrs_no_item(&attrs)?;
204 }
205 Ok((None, Trailing::No, UsePreAttrPos::No))
206 })
207 }
208
209 fn error_on_unconsumed_default(&self, def: Defaultness, kind: &ItemKind) {
211 if let Defaultness::Default(span) = def {
212 self.dcx().emit_err(errors::InappropriateDefault {
213 span,
214 article: kind.article(),
215 descr: kind.descr(),
216 });
217 }
218 }
219
220 fn parse_item_kind(
222 &mut self,
223 attrs: &mut AttrVec,
224 macros_allowed: bool,
225 allow_const_block_items: AllowConstBlockItems,
226 lo: Span,
227 vis: &Visibility,
228 def: &mut Defaultness,
229 fn_parse_mode: FnParseMode,
230 case: Case,
231 ) -> PResult<'a, Option<ItemKind>> {
232 let check_pub = def == &Defaultness::Final;
233 let mut def_ = || mem::replace(def, Defaultness::Final);
234
235 let info = if !self.is_use_closure() && self.eat_keyword_case(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Use,
token_type: crate::parser::token_type::TokenType::KwUse,
}exp!(Use), case) {
236 self.parse_use_item()?
237 } else if self.check_fn_front_matter(check_pub, case) {
238 let (ident, sig, generics, contract, body) =
240 self.parse_fn(attrs, fn_parse_mode, lo, vis, case)?;
241 ItemKind::Fn(Box::new(Fn {
242 defaultness: def_(),
243 ident,
244 sig,
245 generics,
246 contract,
247 body,
248 define_opaque: None,
249 eii_impls: ThinVec::new(),
250 }))
251 } else if self.eat_keyword_case(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Extern,
token_type: crate::parser::token_type::TokenType::KwExtern,
}exp!(Extern), case) {
252 if self.eat_keyword_case(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Crate,
token_type: crate::parser::token_type::TokenType::KwCrate,
}exp!(Crate), case) {
253 self.parse_item_extern_crate()?
255 } else {
256 self.parse_item_foreign_mod(attrs, Safety::Default)?
258 }
259 } else if self.is_unsafe_foreign_mod() {
260 let safety = self.parse_safety(Case::Sensitive);
262 self.expect_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Extern,
token_type: crate::parser::token_type::TokenType::KwExtern,
}exp!(Extern))?;
263 self.parse_item_foreign_mod(attrs, safety)?
264 } else if let Some(safety) = self.parse_global_static_front_matter(case) {
265 let mutability = self.parse_mutability();
267 self.parse_static_item(safety, mutability)?
268 } else if self.check_keyword_case(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Trait,
token_type: crate::parser::token_type::TokenType::KwTrait,
}exp!(Trait), case) || self.check_trait_front_matter() {
269 self.parse_item_trait(attrs, lo)?
271 } else if self.check_impl_frontmatter(0) {
272 self.parse_item_impl(attrs, def_(), false)?
274 } else if let AllowConstBlockItems::Yes | AllowConstBlockItems::DoesNotMatter =
275 allow_const_block_items
276 && self.check_inline_const(0)
277 {
278 if let AllowConstBlockItems::DoesNotMatter = allow_const_block_items {
280 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_parse/src/parser/item.rs:280",
"rustc_parse::parser::item", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_parse/src/parser/item.rs"),
::tracing_core::__macro_support::Option::Some(280u32),
::tracing_core::__macro_support::Option::Some("rustc_parse::parser::item"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("Parsing a const block item that does not matter: {0:?}",
self.token.span) as &dyn Value))])
});
} else { ; }
};debug!("Parsing a const block item that does not matter: {:?}", self.token.span);
281 };
282 ItemKind::ConstBlock(self.parse_const_block_item()?)
283 } else if let Const::Yes(const_span) = self.parse_constness(case) {
284 self.recover_const_mut(const_span);
286 self.recover_missing_kw_before_item()?;
287 let (ident, generics, ty, rhs_kind) = self.parse_const_item(false)?;
288 ItemKind::Const(Box::new(ConstItem {
289 defaultness: def_(),
290 ident,
291 generics,
292 ty,
293 rhs_kind,
294 define_opaque: None,
295 }))
296 } else if let Some(kind) = self.is_reuse_item() {
297 self.parse_item_delegation(attrs, def_(), kind)?
298 } else if self.check_keyword_case(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Mod,
token_type: crate::parser::token_type::TokenType::KwMod,
}exp!(Mod), case)
299 || self.check_keyword_case(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Unsafe,
token_type: crate::parser::token_type::TokenType::KwUnsafe,
}exp!(Unsafe), case) && self.is_keyword_ahead(1, &[kw::Mod])
300 {
301 self.parse_item_mod(attrs)?
303 } else if self.eat_keyword_case(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Type,
token_type: crate::parser::token_type::TokenType::KwType,
}exp!(Type), case) {
304 if let Const::Yes(const_span) = self.parse_constness(case) {
305 self.recover_const_mut(const_span);
307 self.recover_missing_kw_before_item()?;
308 let (ident, generics, ty, rhs_kind) = self.parse_const_item(true)?;
309 self.psess.gated_spans.gate(sym::mgca_type_const_syntax, lo.to(const_span));
312 ItemKind::Const(Box::new(ConstItem {
313 defaultness: def_(),
314 ident,
315 generics,
316 ty,
317 rhs_kind,
318 define_opaque: None,
319 }))
320 } else {
321 self.parse_type_alias(def_())?
323 }
324 } else if self.eat_keyword_case(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Enum,
token_type: crate::parser::token_type::TokenType::KwEnum,
}exp!(Enum), case) {
325 self.parse_item_enum()?
327 } else if self.eat_keyword_case(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Struct,
token_type: crate::parser::token_type::TokenType::KwStruct,
}exp!(Struct), case) {
328 self.parse_item_struct()?
330 } else if self.is_kw_followed_by_ident(kw::Union) {
331 self.bump(); self.parse_item_union()?
334 } else if self.is_builtin() {
335 return self.parse_item_builtin();
337 } else if self.eat_keyword_case(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Macro,
token_type: crate::parser::token_type::TokenType::KwMacro,
}exp!(Macro), case) {
338 self.parse_item_decl_macro(lo)?
340 } else if let IsMacroRulesItem::Yes { has_bang } = self.is_macro_rules_item() {
341 self.parse_item_macro_rules(vis, has_bang)?
343 } else if self.isnt_macro_invocation()
344 && (self.token.is_ident_named(sym::import)
345 || self.token.is_ident_named(sym::using)
346 || self.token.is_ident_named(sym::include)
347 || self.token.is_ident_named(sym::require))
348 {
349 return self.recover_import_as_use();
350 } else if self.isnt_macro_invocation() && vis.kind.is_pub() {
351 self.recover_missing_kw_before_item()?;
352 return Ok(None);
353 } else if self.isnt_macro_invocation() && case == Case::Sensitive {
354 _ = def_;
355
356 return self.parse_item_kind(
358 attrs,
359 macros_allowed,
360 allow_const_block_items,
361 lo,
362 vis,
363 def,
364 fn_parse_mode,
365 Case::Insensitive,
366 );
367 } else if macros_allowed && self.check_path() {
368 if self.isnt_macro_invocation() {
369 self.recover_missing_kw_before_item()?;
370 }
371 ItemKind::MacCall(Box::new(self.parse_item_macro(vis)?))
373 } else {
374 return Ok(None);
375 };
376 Ok(Some(info))
377 }
378
379 fn recover_import_as_use(&mut self) -> PResult<'a, Option<ItemKind>> {
380 let span = self.token.span;
381 let token_name = super::token_descr(&self.token);
382 let snapshot = self.create_snapshot_for_diagnostic();
383 self.bump();
384 match self.parse_use_item() {
385 Ok(u) => {
386 self.dcx().emit_err(errors::RecoverImportAsUse { span, token_name });
387 Ok(Some(u))
388 }
389 Err(e) => {
390 e.cancel();
391 self.restore_snapshot(snapshot);
392 Ok(None)
393 }
394 }
395 }
396
397 fn parse_use_item(&mut self) -> PResult<'a, ItemKind> {
398 let tree = self.parse_use_tree()?;
399 if let Err(mut e) = self.expect_semi() {
400 match tree.kind {
401 UseTreeKind::Glob => {
402 e.note("the wildcard token must be last on the path");
403 }
404 UseTreeKind::Nested { .. } => {
405 e.note("glob-like brace syntax must be last on the path");
406 }
407 _ => (),
408 }
409 return Err(e);
410 }
411 Ok(ItemKind::Use(tree))
412 }
413
414 pub(super) fn is_path_start_item(&mut self) -> bool {
416 self.is_kw_followed_by_ident(kw::Union) || self.is_reuse_item().is_some() || self.check_trait_front_matter() || self.is_async_fn() || #[allow(non_exhaustive_omitted_patterns)] match self.is_macro_rules_item() {
IsMacroRulesItem::Yes { .. } => true,
_ => false,
}matches!(self.is_macro_rules_item(), IsMacroRulesItem::Yes{..}) }
422
423 fn is_reuse_item(&mut self) -> Option<ReuseKind> {
424 if !self.token.is_keyword(kw::Reuse) {
425 return None;
426 }
427
428 if self.look_ahead(1, |t| t.is_path_start() && *t != token::PathSep) {
430 Some(ReuseKind::Path)
431 } else if self.check_impl_frontmatter(1) {
432 Some(ReuseKind::Impl)
433 } else {
434 None
435 }
436 }
437
438 fn isnt_macro_invocation(&mut self) -> bool {
440 self.check_ident() && self.look_ahead(1, |t| *t != token::Bang && *t != token::PathSep)
441 }
442
443 fn recover_missing_kw_before_item(&mut self) -> PResult<'a, ()> {
446 let is_pub = self.prev_token.is_keyword(kw::Pub);
447 let is_const = self.prev_token.is_keyword(kw::Const);
448 let ident_span = self.token.span;
449 let span = if is_pub { self.prev_token.span.to(ident_span) } else { ident_span };
450 let insert_span = ident_span.shrink_to_lo();
451
452 let ident = if self.token.is_ident()
453 && (!is_const || self.look_ahead(1, |t| *t == token::OpenParen))
454 && self.look_ahead(1, |t| {
455 #[allow(non_exhaustive_omitted_patterns)] match t.kind {
token::Lt | token::OpenBrace | token::OpenParen => true,
_ => false,
}matches!(t.kind, token::Lt | token::OpenBrace | token::OpenParen)
456 }) {
457 self.parse_ident_common(true).unwrap()
458 } else {
459 return Ok(());
460 };
461
462 let mut found_generics = false;
463 if self.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Lt,
token_type: crate::parser::token_type::TokenType::Lt,
}exp!(Lt)) {
464 found_generics = true;
465 self.eat_to_tokens(&[crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Gt,
token_type: crate::parser::token_type::TokenType::Gt,
}exp!(Gt)]);
466 self.bump(); }
468
469 let err = if self.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBrace,
token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace)) {
470 if self.look_ahead(1, |t| *t == token::CloseBrace) {
472 Some(errors::MissingKeywordForItemDefinition::EnumOrStruct { span })
474 } else if self.look_ahead(2, |t| *t == token::Colon)
475 || self.look_ahead(3, |t| *t == token::Colon)
476 {
477 Some(errors::MissingKeywordForItemDefinition::Struct { span, insert_span, ident })
479 } else {
480 Some(errors::MissingKeywordForItemDefinition::Enum { span, insert_span, ident })
481 }
482 } else if self.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenParen,
token_type: crate::parser::token_type::TokenType::OpenParen,
}exp!(OpenParen)) {
483 self.bump(); let is_method = self.recover_self_param();
486
487 self.consume_block(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenParen,
token_type: crate::parser::token_type::TokenType::OpenParen,
}exp!(OpenParen), crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::CloseParen,
token_type: crate::parser::token_type::TokenType::CloseParen,
}exp!(CloseParen), ConsumeClosingDelim::Yes);
488
489 let err = if self.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::RArrow,
token_type: crate::parser::token_type::TokenType::RArrow,
}exp!(RArrow)) || self.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBrace,
token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace)) {
490 self.eat_to_tokens(&[crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBrace,
token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace)]);
491 self.bump(); self.consume_block(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBrace,
token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace), crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::CloseBrace,
token_type: crate::parser::token_type::TokenType::CloseBrace,
}exp!(CloseBrace), ConsumeClosingDelim::Yes);
493 if is_method {
494 errors::MissingKeywordForItemDefinition::Method { span, insert_span, ident }
495 } else {
496 errors::MissingKeywordForItemDefinition::Function { span, insert_span, ident }
497 }
498 } else if is_pub && self.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Semi,
token_type: crate::parser::token_type::TokenType::Semi,
}exp!(Semi)) {
499 errors::MissingKeywordForItemDefinition::Struct { span, insert_span, ident }
500 } else {
501 errors::MissingKeywordForItemDefinition::Ambiguous {
502 span,
503 subdiag: if found_generics {
504 None
505 } else if let Ok(snippet) = self.span_to_snippet(ident_span) {
506 Some(errors::AmbiguousMissingKwForItemSub::SuggestMacro {
507 span: ident_span,
508 snippet,
509 })
510 } else {
511 Some(errors::AmbiguousMissingKwForItemSub::HelpMacro)
512 },
513 }
514 };
515 Some(err)
516 } else if found_generics {
517 Some(errors::MissingKeywordForItemDefinition::Ambiguous { span, subdiag: None })
518 } else {
519 None
520 };
521
522 if let Some(err) = err { Err(self.dcx().create_err(err)) } else { Ok(()) }
523 }
524
525 fn parse_item_builtin(&mut self) -> PResult<'a, Option<ItemKind>> {
526 Ok(None)
528 }
529
530 fn parse_item_macro(&mut self, vis: &Visibility) -> PResult<'a, MacCall> {
532 let path = self.parse_path(PathStyle::Mod)?; self.expect(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Bang,
token_type: crate::parser::token_type::TokenType::Bang,
}exp!(Bang))?; match self.parse_delim_args() {
535 Ok(args) => {
537 self.eat_semi_for_macro_if_needed(&args, Some(&path));
538 self.complain_if_pub_macro(vis, false);
539 Ok(MacCall { path, args })
540 }
541
542 Err(mut err) => {
543 if self.token.is_ident()
545 && let [segment] = path.segments.as_slice()
546 && edit_distance("macro_rules", &segment.ident.to_string(), 2).is_some()
547 {
548 err.span_suggestion(
549 path.span,
550 "perhaps you meant to define a macro",
551 "macro_rules",
552 Applicability::MachineApplicable,
553 );
554 }
555 Err(err)
556 }
557 }
558 }
559
560 fn recover_attrs_no_item(&mut self, attrs: &[Attribute]) -> PResult<'a, ()> {
562 let ([start @ end] | [start, .., end]) = attrs else {
563 return Ok(());
564 };
565 let msg = if end.is_doc_comment() {
566 "expected item after doc comment"
567 } else {
568 "expected item after attributes"
569 };
570 let mut err = self.dcx().struct_span_err(end.span, msg);
571 if end.is_doc_comment() {
572 err.span_label(end.span, "this doc comment doesn't document anything");
573 } else if self.token == TokenKind::Semi {
574 err.span_suggestion_verbose(
575 self.token.span,
576 "consider removing this semicolon",
577 "",
578 Applicability::MaybeIncorrect,
579 );
580 }
581 if let [.., penultimate, _] = attrs {
582 err.span_label(start.span.to(penultimate.span), "other attributes here");
583 }
584 Err(err)
585 }
586
587 fn is_async_fn(&self) -> bool {
588 self.token.is_keyword(kw::Async) && self.is_keyword_ahead(1, &[kw::Fn])
589 }
590
591 fn parse_polarity(&mut self) -> ast::ImplPolarity {
592 if self.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Bang,
token_type: crate::parser::token_type::TokenType::Bang,
}exp!(Bang)) && self.look_ahead(1, |t| t.can_begin_type()) {
594 self.bump(); ast::ImplPolarity::Negative(self.prev_token.span)
596 } else {
597 ast::ImplPolarity::Positive
598 }
599 }
600
601 fn parse_item_impl(
616 &mut self,
617 attrs: &mut AttrVec,
618 defaultness: Defaultness,
619 is_reuse: bool,
620 ) -> PResult<'a, ItemKind> {
621 let mut constness = self.parse_constness(Case::Sensitive);
622 let safety = self.parse_safety(Case::Sensitive);
623 self.expect_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Impl,
token_type: crate::parser::token_type::TokenType::KwImpl,
}exp!(Impl))?;
624
625 let mut generics = if self.choose_generics_over_qpath(0) {
627 self.parse_generics()?
628 } else {
629 let mut generics = Generics::default();
630 generics.span = self.prev_token.span.shrink_to_hi();
633 generics
634 };
635
636 if let Const::No = constness {
637 constness = self.parse_constness(Case::Sensitive);
639 }
640
641 if let Const::Yes(span) = constness {
642 self.psess.gated_spans.gate(sym::const_trait_impl, span);
643 }
644
645 if (self.token_uninterpolated_span().at_least_rust_2018()
647 && self.token.is_keyword(kw::Async))
648 || self.is_kw_followed_by_ident(kw::Async)
649 {
650 self.bump();
651 self.dcx().emit_err(errors::AsyncImpl { span: self.prev_token.span });
652 }
653
654 let polarity = self.parse_polarity();
655
656 let ty_first = if self.token.is_keyword(kw::For) && self.look_ahead(1, |t| t != &token::Lt)
658 {
659 let span = self.prev_token.span.between(self.token.span);
660 return Err(self.dcx().create_err(errors::MissingTraitInTraitImpl {
661 span,
662 for_span: span.to(self.token.span),
663 }));
664 } else {
665 self.parse_ty_with_generics_recovery(&generics)?
666 };
667
668 let has_for = self.eat_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::For,
token_type: crate::parser::token_type::TokenType::KwFor,
}exp!(For));
670 let missing_for_span = self.prev_token.span.between(self.token.span);
671
672 let ty_second = if self.token == token::DotDot {
673 self.bump(); Some(self.mk_ty(self.prev_token.span, TyKind::Dummy))
680 } else if has_for || self.token.can_begin_type() {
681 Some(self.parse_ty()?)
682 } else {
683 None
684 };
685
686 generics.where_clause = self.parse_where_clause()?;
687
688 let impl_items = if is_reuse {
689 Default::default()
690 } else {
691 self.parse_item_list(attrs, |p| p.parse_impl_item(ForceCollect::No))?
692 };
693
694 let (of_trait, self_ty) = match ty_second {
695 Some(ty_second) => {
696 if !has_for {
698 self.dcx().emit_err(errors::MissingForInTraitImpl { span: missing_for_span });
699 }
700
701 let ty_first = *ty_first;
702 let path = match ty_first.kind {
703 TyKind::Path(None, path) => path,
705 other => {
706 if let TyKind::ImplTrait(_, bounds) = other
707 && let [bound] = bounds.as_slice()
708 && let GenericBound::Trait(poly_trait_ref) = bound
709 {
710 let extra_impl_kw = ty_first.span.until(bound.span());
714 self.dcx().emit_err(errors::ExtraImplKeywordInTraitImpl {
715 extra_impl_kw,
716 impl_trait_span: ty_first.span,
717 });
718 poly_trait_ref.trait_ref.path.clone()
719 } else {
720 return Err(self.dcx().create_err(
721 errors::ExpectedTraitInTraitImplFoundType { span: ty_first.span },
722 ));
723 }
724 }
725 };
726 let trait_ref = TraitRef { path, ref_id: ty_first.id };
727
728 let of_trait =
729 Some(Box::new(TraitImplHeader { defaultness, safety, polarity, trait_ref }));
730 (of_trait, ty_second)
731 }
732 None => {
733 let self_ty = ty_first;
734 let error = |modifier, modifier_name, modifier_span| {
735 self.dcx().create_err(errors::TraitImplModifierInInherentImpl {
736 span: self_ty.span,
737 modifier,
738 modifier_name,
739 modifier_span,
740 self_ty: self_ty.span,
741 })
742 };
743
744 if let Safety::Unsafe(span) = safety {
745 error("unsafe", "unsafe", span).with_code(E0197).emit();
746 }
747 if let ImplPolarity::Negative(span) = polarity {
748 error("!", "negative", span).emit();
749 }
750 if let Defaultness::Default(def_span) = defaultness {
751 error("default", "default", def_span).emit();
752 }
753 if let Const::Yes(span) = constness {
754 self.psess.gated_spans.gate(sym::const_trait_impl, span);
755 }
756 (None, self_ty)
757 }
758 };
759
760 Ok(ItemKind::Impl(Impl { generics, of_trait, self_ty, items: impl_items, constness }))
761 }
762
763 fn parse_item_delegation(
764 &mut self,
765 attrs: &mut AttrVec,
766 defaultness: Defaultness,
767 kind: ReuseKind,
768 ) -> PResult<'a, ItemKind> {
769 let span = self.token.span;
770 self.expect_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Reuse,
token_type: crate::parser::token_type::TokenType::KwReuse,
}exp!(Reuse))?;
771
772 let item_kind = match kind {
773 ReuseKind::Path => self.parse_path_like_delegation(),
774 ReuseKind::Impl => self.parse_impl_delegation(span, attrs, defaultness),
775 }?;
776
777 self.psess.gated_spans.gate(sym::fn_delegation, span.to(self.prev_token.span));
778
779 Ok(item_kind)
780 }
781
782 fn parse_delegation_body(&mut self) -> PResult<'a, Option<Box<Block>>> {
783 Ok(if self.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBrace,
token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace)) {
784 Some(self.parse_block()?)
785 } else {
786 self.expect(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Semi,
token_type: crate::parser::token_type::TokenType::Semi,
}exp!(Semi))?;
787 None
788 })
789 }
790
791 fn parse_impl_delegation(
792 &mut self,
793 span: Span,
794 attrs: &mut AttrVec,
795 defaultness: Defaultness,
796 ) -> PResult<'a, ItemKind> {
797 let mut impl_item = self.parse_item_impl(attrs, defaultness, true)?;
798 let ItemKind::Impl(Impl { items, of_trait, .. }) = &mut impl_item else { ::core::panicking::panic("internal error: entered unreachable code")unreachable!() };
799
800 let until_expr_span = span.to(self.prev_token.span);
801
802 let Some(of_trait) = of_trait else {
803 return Err(self
804 .dcx()
805 .create_err(errors::ImplReuseInherentImpl { span: until_expr_span }));
806 };
807
808 let body = self.parse_delegation_body()?;
809 let whole_reuse_span = span.to(self.prev_token.span);
810
811 items.push(Box::new(AssocItem {
812 id: DUMMY_NODE_ID,
813 attrs: Default::default(),
814 span: whole_reuse_span,
815 tokens: None,
816 vis: Visibility {
817 kind: VisibilityKind::Inherited,
818 span: whole_reuse_span,
819 tokens: None,
820 },
821 kind: AssocItemKind::DelegationMac(Box::new(DelegationMac {
822 qself: None,
823 prefix: of_trait.trait_ref.path.clone(),
824 suffixes: None,
825 body,
826 })),
827 }));
828
829 Ok(impl_item)
830 }
831
832 fn parse_path_like_delegation(&mut self) -> PResult<'a, ItemKind> {
833 let (qself, path) = if self.eat_lt() {
834 let (qself, path) = self.parse_qpath(PathStyle::Expr)?;
835 (Some(qself), path)
836 } else {
837 (None, self.parse_path(PathStyle::Expr)?)
838 };
839
840 let rename = |this: &mut Self| {
841 Ok(if this.eat_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::As,
token_type: crate::parser::token_type::TokenType::KwAs,
}exp!(As)) { Some(this.parse_ident()?) } else { None })
842 };
843
844 Ok(if self.eat_path_sep() {
845 let suffixes = if self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Star,
token_type: crate::parser::token_type::TokenType::Star,
}exp!(Star)) {
846 None
847 } else {
848 let parse_suffix = |p: &mut Self| Ok((p.parse_path_segment_ident()?, rename(p)?));
849 Some(self.parse_delim_comma_seq(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBrace,
token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace), crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::CloseBrace,
token_type: crate::parser::token_type::TokenType::CloseBrace,
}exp!(CloseBrace), parse_suffix)?.0)
850 };
851
852 ItemKind::DelegationMac(Box::new(DelegationMac {
853 qself,
854 prefix: path,
855 suffixes,
856 body: self.parse_delegation_body()?,
857 }))
858 } else {
859 let rename = rename(self)?;
860 let ident = rename.unwrap_or_else(|| path.segments.last().unwrap().ident);
861
862 ItemKind::Delegation(Box::new(Delegation {
863 id: DUMMY_NODE_ID,
864 qself,
865 path,
866 ident,
867 rename,
868 body: self.parse_delegation_body()?,
869 from_glob: false,
870 }))
871 })
872 }
873
874 fn parse_item_list<T>(
875 &mut self,
876 attrs: &mut AttrVec,
877 mut parse_item: impl FnMut(&mut Parser<'a>) -> PResult<'a, Option<Option<T>>>,
878 ) -> PResult<'a, ThinVec<T>> {
879 let open_brace_span = self.token.span;
880
881 if self.token == TokenKind::Semi {
883 self.dcx().emit_err(errors::UseEmptyBlockNotSemi { span: self.token.span });
884 self.bump();
885 return Ok(ThinVec::new());
886 }
887
888 self.expect(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBrace,
token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace))?;
889 attrs.extend(self.parse_inner_attributes()?);
890
891 let mut items = ThinVec::new();
892 while !self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::CloseBrace,
token_type: crate::parser::token_type::TokenType::CloseBrace,
}exp!(CloseBrace)) {
893 if self.recover_doc_comment_before_brace() {
894 continue;
895 }
896 self.recover_vcs_conflict_marker();
897 match parse_item(self) {
898 Ok(None) => {
899 let mut is_unnecessary_semicolon = !items.is_empty()
900 && self
918 .span_to_snippet(self.prev_token.span)
919 .is_ok_and(|snippet| snippet == "}")
920 && self.token == token::Semi;
921 let mut semicolon_span = self.token.span;
922 if !is_unnecessary_semicolon {
923 is_unnecessary_semicolon =
925 self.token == token::OpenBrace && self.prev_token == token::Semi;
926 semicolon_span = self.prev_token.span;
927 }
928 let non_item_span = self.token.span;
930 let is_let = self.token.is_keyword(kw::Let);
931
932 let mut err =
933 self.dcx().struct_span_err(non_item_span, "non-item in item list");
934 self.consume_block(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBrace,
token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace), crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::CloseBrace,
token_type: crate::parser::token_type::TokenType::CloseBrace,
}exp!(CloseBrace), ConsumeClosingDelim::Yes);
935 if is_let {
936 err.span_suggestion_verbose(
937 non_item_span,
938 "consider using `const` instead of `let` for associated const",
939 "const",
940 Applicability::MachineApplicable,
941 );
942 } else {
943 err.span_label(open_brace_span, "item list starts here")
944 .span_label(non_item_span, "non-item starts here")
945 .span_label(self.prev_token.span, "item list ends here");
946 }
947 if is_unnecessary_semicolon {
948 err.span_suggestion(
949 semicolon_span,
950 "consider removing this semicolon",
951 "",
952 Applicability::MaybeIncorrect,
953 );
954 }
955 err.emit();
956 break;
957 }
958 Ok(Some(item)) => items.extend(item),
959 Err(err) => {
960 self.consume_block(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBrace,
token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace), crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::CloseBrace,
token_type: crate::parser::token_type::TokenType::CloseBrace,
}exp!(CloseBrace), ConsumeClosingDelim::Yes);
961 err.with_span_label(
962 open_brace_span,
963 "while parsing this item list starting here",
964 )
965 .with_span_label(self.prev_token.span, "the item list ends here")
966 .emit();
967 break;
968 }
969 }
970 }
971 Ok(items)
972 }
973
974 fn recover_doc_comment_before_brace(&mut self) -> bool {
976 if let token::DocComment(..) = self.token.kind {
977 if self.look_ahead(1, |tok| tok == &token::CloseBrace) {
978 {
self.dcx().struct_span_err(self.token.span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("found a documentation comment that doesn\'t document anything"))
})).with_code(E0584)
}struct_span_code_err!(
980 self.dcx(),
981 self.token.span,
982 E0584,
983 "found a documentation comment that doesn't document anything",
984 )
985 .with_span_label(self.token.span, "this doc comment doesn't document anything")
986 .with_help(
987 "doc comments must come before what they document, if a comment was \
988 intended use `//`",
989 )
990 .emit();
991 self.bump();
992 return true;
993 }
994 }
995 false
996 }
997
998 fn parse_defaultness(&mut self) -> Defaultness {
1000 if self.check_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Default,
token_type: crate::parser::token_type::TokenType::KwDefault,
}exp!(Default))
1004 && self.look_ahead(1, |t| t.is_non_raw_ident_where(|i| i.name != kw::As))
1005 {
1006 self.bump(); Defaultness::Default(self.prev_token_uninterpolated_span())
1008 } else {
1009 Defaultness::Final
1010 }
1011 }
1012
1013 fn check_trait_front_matter(&mut self) -> bool {
1015 self.check_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Auto,
token_type: crate::parser::token_type::TokenType::KwAuto,
}exp!(Auto)) && self.is_keyword_ahead(1, &[kw::Trait])
1017 || self.check_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Unsafe,
token_type: crate::parser::token_type::TokenType::KwUnsafe,
}exp!(Unsafe)) && self.is_keyword_ahead(1, &[kw::Trait, kw::Auto])
1019 || self.check_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Const,
token_type: crate::parser::token_type::TokenType::KwConst,
}exp!(Const)) && ((self.is_keyword_ahead(1, &[kw::Trait]) || self.is_keyword_ahead(1, &[kw::Auto]) && self.is_keyword_ahead(2, &[kw::Trait]))
1020 || self.is_keyword_ahead(1, &[kw::Unsafe]) && self.is_keyword_ahead(2, &[kw::Trait, kw::Auto]))
1021 }
1022
1023 fn parse_item_trait(&mut self, attrs: &mut AttrVec, lo: Span) -> PResult<'a, ItemKind> {
1025 let constness = self.parse_constness(Case::Sensitive);
1026 if let Const::Yes(span) = constness {
1027 self.psess.gated_spans.gate(sym::const_trait_impl, span);
1028 }
1029 let safety = self.parse_safety(Case::Sensitive);
1030 let is_auto = if self.eat_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Auto,
token_type: crate::parser::token_type::TokenType::KwAuto,
}exp!(Auto)) {
1032 self.psess.gated_spans.gate(sym::auto_traits, self.prev_token.span);
1033 IsAuto::Yes
1034 } else {
1035 IsAuto::No
1036 };
1037
1038 self.expect_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Trait,
token_type: crate::parser::token_type::TokenType::KwTrait,
}exp!(Trait))?;
1039 let ident = self.parse_ident()?;
1040 let mut generics = self.parse_generics()?;
1041
1042 let had_colon = self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Colon,
token_type: crate::parser::token_type::TokenType::Colon,
}exp!(Colon));
1044 let span_at_colon = self.prev_token.span;
1045 let bounds = if had_colon { self.parse_generic_bounds()? } else { Vec::new() };
1046
1047 let span_before_eq = self.prev_token.span;
1048 if self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Eq,
token_type: crate::parser::token_type::TokenType::Eq,
}exp!(Eq)) {
1049 if had_colon {
1051 let span = span_at_colon.to(span_before_eq);
1052 self.dcx().emit_err(errors::BoundsNotAllowedOnTraitAliases { span });
1053 }
1054
1055 let bounds = self.parse_generic_bounds()?;
1056 generics.where_clause = self.parse_where_clause()?;
1057 self.expect_semi()?;
1058
1059 let whole_span = lo.to(self.prev_token.span);
1060 if is_auto == IsAuto::Yes {
1061 self.dcx().emit_err(errors::TraitAliasCannotBeAuto { span: whole_span });
1062 }
1063 if let Safety::Unsafe(_) = safety {
1064 self.dcx().emit_err(errors::TraitAliasCannotBeUnsafe { span: whole_span });
1065 }
1066
1067 self.psess.gated_spans.gate(sym::trait_alias, whole_span);
1068
1069 Ok(ItemKind::TraitAlias(Box::new(TraitAlias { constness, ident, generics, bounds })))
1070 } else {
1071 generics.where_clause = self.parse_where_clause()?;
1073 let items = self.parse_item_list(attrs, |p| p.parse_trait_item(ForceCollect::No))?;
1074 Ok(ItemKind::Trait(Box::new(Trait {
1075 constness,
1076 is_auto,
1077 safety,
1078 ident,
1079 generics,
1080 bounds,
1081 items,
1082 })))
1083 }
1084 }
1085
1086 pub fn parse_impl_item(
1087 &mut self,
1088 force_collect: ForceCollect,
1089 ) -> PResult<'a, Option<Option<Box<AssocItem>>>> {
1090 let fn_parse_mode =
1091 FnParseMode { req_name: |_, _| true, context: FnContext::Impl, req_body: true };
1092 self.parse_assoc_item(fn_parse_mode, force_collect)
1093 }
1094
1095 pub fn parse_trait_item(
1096 &mut self,
1097 force_collect: ForceCollect,
1098 ) -> PResult<'a, Option<Option<Box<AssocItem>>>> {
1099 let fn_parse_mode = FnParseMode {
1100 req_name: |edition, _| edition >= Edition::Edition2018,
1101 context: FnContext::Trait,
1102 req_body: false,
1103 };
1104 self.parse_assoc_item(fn_parse_mode, force_collect)
1105 }
1106
1107 fn parse_assoc_item(
1109 &mut self,
1110 fn_parse_mode: FnParseMode,
1111 force_collect: ForceCollect,
1112 ) -> PResult<'a, Option<Option<Box<AssocItem>>>> {
1113 Ok(self
1114 .parse_item_(
1115 fn_parse_mode,
1116 force_collect,
1117 AllowConstBlockItems::DoesNotMatter, )?
1119 .map(|Item { attrs, id, span, vis, kind, tokens }| {
1120 let kind = match AssocItemKind::try_from(kind) {
1121 Ok(kind) => kind,
1122 Err(kind) => match kind {
1123 ItemKind::Static(box StaticItem {
1124 ident,
1125 ty,
1126 safety: _,
1127 mutability: _,
1128 expr,
1129 define_opaque,
1130 }) => {
1131 self.dcx().emit_err(errors::AssociatedStaticItemNotAllowed { span });
1132 AssocItemKind::Const(Box::new(ConstItem {
1133 defaultness: Defaultness::Final,
1134 ident,
1135 generics: Generics::default(),
1136 ty,
1137 rhs_kind: ConstItemRhsKind::Body { rhs: expr },
1138 define_opaque,
1139 }))
1140 }
1141 _ => return self.error_bad_item_kind(span, &kind, "`trait`s or `impl`s"),
1142 },
1143 };
1144 Some(Box::new(Item { attrs, id, span, vis, kind, tokens }))
1145 }))
1146 }
1147
1148 fn parse_type_alias(&mut self, defaultness: Defaultness) -> PResult<'a, ItemKind> {
1154 let ident = self.parse_ident()?;
1155 let mut generics = self.parse_generics()?;
1156
1157 let bounds = if self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Colon,
token_type: crate::parser::token_type::TokenType::Colon,
}exp!(Colon)) { self.parse_generic_bounds()? } else { Vec::new() };
1159 generics.where_clause = self.parse_where_clause()?;
1160
1161 let ty = if self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Eq,
token_type: crate::parser::token_type::TokenType::Eq,
}exp!(Eq)) { Some(self.parse_ty()?) } else { None };
1162
1163 let after_where_clause = self.parse_where_clause()?;
1164
1165 self.expect_semi()?;
1166
1167 Ok(ItemKind::TyAlias(Box::new(TyAlias {
1168 defaultness,
1169 ident,
1170 generics,
1171 after_where_clause,
1172 bounds,
1173 ty,
1174 })))
1175 }
1176
1177 fn parse_use_tree(&mut self) -> PResult<'a, UseTree> {
1187 let lo = self.token.span;
1188
1189 let mut prefix =
1190 ast::Path { segments: ThinVec::new(), span: lo.shrink_to_lo(), tokens: None };
1191 let kind =
1192 if self.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBrace,
token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace)) || self.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Star,
token_type: crate::parser::token_type::TokenType::Star,
}exp!(Star)) || self.is_import_coupler() {
1193 let mod_sep_ctxt = self.token.span.ctxt();
1195 if self.eat_path_sep() {
1196 prefix
1197 .segments
1198 .push(PathSegment::path_root(lo.shrink_to_lo().with_ctxt(mod_sep_ctxt)));
1199 }
1200
1201 self.parse_use_tree_glob_or_nested()?
1202 } else {
1203 prefix = self.parse_path(PathStyle::Mod)?;
1205
1206 if self.eat_path_sep() {
1207 self.parse_use_tree_glob_or_nested()?
1208 } else {
1209 while self.eat_noexpect(&token::Colon) {
1211 self.dcx()
1212 .emit_err(errors::SingleColonImportPath { span: self.prev_token.span });
1213
1214 self.parse_path_segments(&mut prefix.segments, PathStyle::Mod, None)?;
1216 prefix.span = lo.to(self.prev_token.span);
1217 }
1218
1219 UseTreeKind::Simple(self.parse_rename()?)
1220 }
1221 };
1222
1223 Ok(UseTree { prefix, kind, span: lo.to(self.prev_token.span) })
1224 }
1225
1226 fn parse_use_tree_glob_or_nested(&mut self) -> PResult<'a, UseTreeKind> {
1228 Ok(if self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Star,
token_type: crate::parser::token_type::TokenType::Star,
}exp!(Star)) {
1229 UseTreeKind::Glob
1230 } else {
1231 let lo = self.token.span;
1232 UseTreeKind::Nested {
1233 items: self.parse_use_tree_list()?,
1234 span: lo.to(self.prev_token.span),
1235 }
1236 })
1237 }
1238
1239 fn parse_use_tree_list(&mut self) -> PResult<'a, ThinVec<(UseTree, ast::NodeId)>> {
1245 self.parse_delim_comma_seq(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBrace,
token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace), crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::CloseBrace,
token_type: crate::parser::token_type::TokenType::CloseBrace,
}exp!(CloseBrace), |p| {
1246 p.recover_vcs_conflict_marker();
1247 Ok((p.parse_use_tree()?, DUMMY_NODE_ID))
1248 })
1249 .map(|(r, _)| r)
1250 }
1251
1252 fn parse_rename(&mut self) -> PResult<'a, Option<Ident>> {
1253 if self.eat_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::As,
token_type: crate::parser::token_type::TokenType::KwAs,
}exp!(As)) {
1254 self.parse_ident_or_underscore().map(Some)
1255 } else {
1256 Ok(None)
1257 }
1258 }
1259
1260 fn parse_ident_or_underscore(&mut self) -> PResult<'a, Ident> {
1261 match self.token.ident() {
1262 Some((ident @ Ident { name: kw::Underscore, .. }, IdentIsRaw::No)) => {
1263 self.bump();
1264 Ok(ident)
1265 }
1266 _ => self.parse_ident(),
1267 }
1268 }
1269
1270 fn parse_item_extern_crate(&mut self) -> PResult<'a, ItemKind> {
1279 let orig_ident = self.parse_crate_name_with_dashes()?;
1281 let (orig_name, item_ident) = if let Some(rename) = self.parse_rename()? {
1282 (Some(orig_ident.name), rename)
1283 } else {
1284 (None, orig_ident)
1285 };
1286 self.expect_semi()?;
1287 Ok(ItemKind::ExternCrate(orig_name, item_ident))
1288 }
1289
1290 fn parse_crate_name_with_dashes(&mut self) -> PResult<'a, Ident> {
1291 let ident = if self.token.is_keyword(kw::SelfLower) {
1292 self.parse_path_segment_ident()
1293 } else {
1294 self.parse_ident()
1295 }?;
1296
1297 let dash = crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Minus,
token_type: crate::parser::token_type::TokenType::Minus,
}exp!(Minus);
1298 if self.token != dash.tok {
1299 return Ok(ident);
1300 }
1301
1302 let mut dashes = ::alloc::vec::Vec::new()vec![];
1304 let mut idents = ::alloc::vec::Vec::new()vec![];
1305 while self.eat(dash) {
1306 dashes.push(self.prev_token.span);
1307 idents.push(self.parse_ident()?);
1308 }
1309
1310 let fixed_name_sp = ident.span.to(idents.last().unwrap().span);
1311 let mut fixed_name = ident.name.to_string();
1312 for part in idents {
1313 fixed_name.write_fmt(format_args!("_{0}", part.name))write!(fixed_name, "_{}", part.name).unwrap();
1314 }
1315
1316 self.dcx().emit_err(errors::ExternCrateNameWithDashes {
1317 span: fixed_name_sp,
1318 sugg: errors::ExternCrateNameWithDashesSugg { dashes },
1319 });
1320
1321 Ok(Ident::from_str_and_span(&fixed_name, fixed_name_sp))
1322 }
1323
1324 fn parse_item_foreign_mod(
1335 &mut self,
1336 attrs: &mut AttrVec,
1337 mut safety: Safety,
1338 ) -> PResult<'a, ItemKind> {
1339 let extern_span = self.prev_token_uninterpolated_span();
1340 let abi = self.parse_abi(); if safety == Safety::Default
1343 && self.token.is_keyword(kw::Unsafe)
1344 && self.look_ahead(1, |t| *t == token::OpenBrace)
1345 {
1346 self.expect(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBrace,
token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace)).unwrap_err().emit();
1347 safety = Safety::Unsafe(self.token.span);
1348 let _ = self.eat_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Unsafe,
token_type: crate::parser::token_type::TokenType::KwUnsafe,
}exp!(Unsafe));
1349 }
1350 Ok(ItemKind::ForeignMod(ast::ForeignMod {
1351 extern_span,
1352 safety,
1353 abi,
1354 items: self.parse_item_list(attrs, |p| p.parse_foreign_item(ForceCollect::No))?,
1355 }))
1356 }
1357
1358 pub fn parse_foreign_item(
1360 &mut self,
1361 force_collect: ForceCollect,
1362 ) -> PResult<'a, Option<Option<Box<ForeignItem>>>> {
1363 let fn_parse_mode = FnParseMode {
1364 req_name: |_, is_dot_dot_dot| is_dot_dot_dot == IsDotDotDot::No,
1365 context: FnContext::Free,
1366 req_body: false,
1367 };
1368 Ok(self
1369 .parse_item_(
1370 fn_parse_mode,
1371 force_collect,
1372 AllowConstBlockItems::DoesNotMatter, )?
1374 .map(|Item { attrs, id, span, vis, kind, tokens }| {
1375 let kind = match ForeignItemKind::try_from(kind) {
1376 Ok(kind) => kind,
1377 Err(kind) => match kind {
1378 ItemKind::Const(box ConstItem { ident, ty, rhs_kind, .. }) => {
1379 let const_span = Some(span.with_hi(ident.span.lo()))
1380 .filter(|span| span.can_be_used_for_suggestions());
1381 self.dcx().emit_err(errors::ExternItemCannotBeConst {
1382 ident_span: ident.span,
1383 const_span,
1384 });
1385 ForeignItemKind::Static(Box::new(StaticItem {
1386 ident,
1387 ty,
1388 mutability: Mutability::Not,
1389 expr: match rhs_kind {
1390 ConstItemRhsKind::Body { rhs } => rhs,
1391 ConstItemRhsKind::TypeConst { rhs: Some(anon) } => {
1392 Some(anon.value)
1393 }
1394 ConstItemRhsKind::TypeConst { rhs: None } => None,
1395 },
1396 safety: Safety::Default,
1397 define_opaque: None,
1398 }))
1399 }
1400 _ => return self.error_bad_item_kind(span, &kind, "`extern` blocks"),
1401 },
1402 };
1403 Some(Box::new(Item { attrs, id, span, vis, kind, tokens }))
1404 }))
1405 }
1406
1407 fn error_bad_item_kind<T>(&self, span: Span, kind: &ItemKind, ctx: &'static str) -> Option<T> {
1408 let span = self.psess.source_map().guess_head_span(span);
1410 let descr = kind.descr();
1411 let help = match kind {
1412 ItemKind::DelegationMac(deleg) if deleg.suffixes.is_none() => false,
1413 _ => true,
1414 };
1415 self.dcx().emit_err(errors::BadItemKind { span, descr, ctx, help });
1416 None
1417 }
1418
1419 fn is_use_closure(&self) -> bool {
1420 if self.token.is_keyword(kw::Use) {
1421 self.look_ahead(1, |token| {
1423 let dist =
1425 if token.is_keyword(kw::Move) || token.is_keyword(kw::Async) { 2 } else { 1 };
1426
1427 self.look_ahead(dist, |token| #[allow(non_exhaustive_omitted_patterns)] match token.kind {
token::Or | token::OrOr => true,
_ => false,
}matches!(token.kind, token::Or | token::OrOr))
1428 })
1429 } else {
1430 false
1431 }
1432 }
1433
1434 fn is_unsafe_foreign_mod(&self) -> bool {
1435 if !self.token.is_keyword(kw::Unsafe) {
1437 return false;
1438 }
1439 if !self.is_keyword_ahead(1, &[kw::Extern]) {
1441 return false;
1442 }
1443
1444 let n = if self.look_ahead(2, |t| t.can_begin_string_literal()) { 3 } else { 2 };
1446
1447 self.tree_look_ahead(n, |t| #[allow(non_exhaustive_omitted_patterns)] match t {
TokenTree::Delimited(_, _, Delimiter::Brace, _) => true,
_ => false,
}matches!(t, TokenTree::Delimited(_, _, Delimiter::Brace, _)))
1452 == Some(true)
1453 }
1454
1455 fn parse_global_static_front_matter(&mut self, case: Case) -> Option<Safety> {
1456 let is_global_static = if self.check_keyword_case(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Static,
token_type: crate::parser::token_type::TokenType::KwStatic,
}exp!(Static), case) {
1457 !self.look_ahead(1, |token| {
1459 if token.is_keyword_case(kw::Move, case) || token.is_keyword_case(kw::Use, case) {
1460 return true;
1461 }
1462 #[allow(non_exhaustive_omitted_patterns)] match token.kind {
token::Or | token::OrOr => true,
_ => false,
}matches!(token.kind, token::Or | token::OrOr)
1463 })
1464 } else {
1465 (self.check_keyword_case(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Unsafe,
token_type: crate::parser::token_type::TokenType::KwUnsafe,
}exp!(Unsafe), case)
1467 || self.check_keyword_case(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Safe,
token_type: crate::parser::token_type::TokenType::KwSafe,
}exp!(Safe), case))
1468 && self.look_ahead(1, |t| t.is_keyword_case(kw::Static, case))
1469 };
1470
1471 if is_global_static {
1472 let safety = self.parse_safety(case);
1473 let _ = self.eat_keyword_case(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Static,
token_type: crate::parser::token_type::TokenType::KwStatic,
}exp!(Static), case);
1474 Some(safety)
1475 } else {
1476 None
1477 }
1478 }
1479
1480 fn recover_const_mut(&mut self, const_span: Span) {
1482 if self.eat_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Mut,
token_type: crate::parser::token_type::TokenType::KwMut,
}exp!(Mut)) {
1483 let span = self.prev_token.span;
1484 self.dcx()
1485 .emit_err(errors::ConstGlobalCannotBeMutable { ident_span: span, const_span });
1486 } else if self.eat_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Let,
token_type: crate::parser::token_type::TokenType::KwLet,
}exp!(Let)) {
1487 let span = self.prev_token.span;
1488 self.dcx().emit_err(errors::ConstLetMutuallyExclusive { span: const_span.to(span) });
1489 }
1490 }
1491
1492 fn parse_const_block_item(&mut self) -> PResult<'a, ConstBlockItem> {
1493 self.expect_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Const,
token_type: crate::parser::token_type::TokenType::KwConst,
}exp!(Const))?;
1494 let const_span = self.prev_token.span;
1495 self.psess.gated_spans.gate(sym::const_block_items, const_span);
1496 let block = self.parse_block()?;
1497 Ok(ConstBlockItem { id: DUMMY_NODE_ID, span: const_span.to(block.span), block })
1498 }
1499
1500 fn parse_static_item(
1507 &mut self,
1508 safety: Safety,
1509 mutability: Mutability,
1510 ) -> PResult<'a, ItemKind> {
1511 let ident = self.parse_ident()?;
1512
1513 if self.token == TokenKind::Lt && self.may_recover() {
1514 let generics = self.parse_generics()?;
1515 self.dcx().emit_err(errors::StaticWithGenerics { span: generics.span });
1516 }
1517
1518 let ty = match (self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Colon,
token_type: crate::parser::token_type::TokenType::Colon,
}exp!(Colon)), self.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Eq,
token_type: crate::parser::token_type::TokenType::Eq,
}exp!(Eq)) | self.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Semi,
token_type: crate::parser::token_type::TokenType::Semi,
}exp!(Semi))) {
1521 (true, false) => self.parse_ty()?,
1522 (colon, _) => self.recover_missing_global_item_type(colon, Some(mutability)),
1525 };
1526
1527 let expr = if self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Eq,
token_type: crate::parser::token_type::TokenType::Eq,
}exp!(Eq)) { Some(self.parse_expr()?) } else { None };
1528
1529 self.expect_semi()?;
1530
1531 let item = StaticItem { ident, ty, safety, mutability, expr, define_opaque: None };
1532 Ok(ItemKind::Static(Box::new(item)))
1533 }
1534
1535 fn parse_const_item(
1544 &mut self,
1545 const_arg: bool,
1546 ) -> PResult<'a, (Ident, Generics, Box<Ty>, ConstItemRhsKind)> {
1547 let ident = self.parse_ident_or_underscore()?;
1548
1549 let mut generics = self.parse_generics()?;
1550
1551 if !generics.span.is_empty() {
1554 self.psess.gated_spans.gate(sym::generic_const_items, generics.span);
1555 }
1556
1557 let ty = match (
1560 self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Colon,
token_type: crate::parser::token_type::TokenType::Colon,
}exp!(Colon)),
1561 self.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Eq,
token_type: crate::parser::token_type::TokenType::Eq,
}exp!(Eq)) | self.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Semi,
token_type: crate::parser::token_type::TokenType::Semi,
}exp!(Semi)) | self.check_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Where,
token_type: crate::parser::token_type::TokenType::KwWhere,
}exp!(Where)),
1562 ) {
1563 (true, false) => self.parse_ty()?,
1564 (colon, _) => self.recover_missing_global_item_type(colon, None),
1566 };
1567
1568 let before_where_clause =
1571 if self.may_recover() { self.parse_where_clause()? } else { WhereClause::default() };
1572
1573 let rhs = match (self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Eq,
token_type: crate::parser::token_type::TokenType::Eq,
}exp!(Eq)), const_arg) {
1574 (true, true) => ConstItemRhsKind::TypeConst {
1575 rhs: Some(
1576 self.parse_expr_anon_const(|this, expr| this.mgca_direct_lit_hack(expr))?,
1577 ),
1578 },
1579 (true, false) => ConstItemRhsKind::Body { rhs: Some(self.parse_expr()?) },
1580 (false, true) => ConstItemRhsKind::TypeConst { rhs: None },
1581 (false, false) => ConstItemRhsKind::Body { rhs: None },
1582 };
1583
1584 let after_where_clause = self.parse_where_clause()?;
1585
1586 if before_where_clause.has_where_token
1590 && let Some(rhs_span) = rhs.span()
1591 {
1592 self.dcx().emit_err(errors::WhereClauseBeforeConstBody {
1593 span: before_where_clause.span,
1594 name: ident.span,
1595 body: rhs_span,
1596 sugg: if !after_where_clause.has_where_token {
1597 self.psess.source_map().span_to_snippet(rhs_span).ok().map(|body_s| {
1598 errors::WhereClauseBeforeConstBodySugg {
1599 left: before_where_clause.span.shrink_to_lo(),
1600 snippet: body_s,
1601 right: before_where_clause.span.shrink_to_hi().to(rhs_span),
1602 }
1603 })
1604 } else {
1605 None
1608 },
1609 });
1610 }
1611
1612 let mut predicates = before_where_clause.predicates;
1619 predicates.extend(after_where_clause.predicates);
1620 let where_clause = WhereClause {
1621 has_where_token: before_where_clause.has_where_token
1622 || after_where_clause.has_where_token,
1623 predicates,
1624 span: if after_where_clause.has_where_token {
1625 after_where_clause.span
1626 } else {
1627 before_where_clause.span
1628 },
1629 };
1630
1631 if where_clause.has_where_token {
1632 self.psess.gated_spans.gate(sym::generic_const_items, where_clause.span);
1633 }
1634
1635 generics.where_clause = where_clause;
1636
1637 self.expect_semi()?;
1638
1639 Ok((ident, generics, ty, rhs))
1640 }
1641
1642 fn recover_missing_global_item_type(
1645 &mut self,
1646 colon_present: bool,
1647 m: Option<Mutability>,
1648 ) -> Box<Ty> {
1649 let kind = match m {
1652 Some(Mutability::Mut) => "static mut",
1653 Some(Mutability::Not) => "static",
1654 None => "const",
1655 };
1656
1657 let colon = match colon_present {
1658 true => "",
1659 false => ":",
1660 };
1661
1662 let span = self.prev_token.span.shrink_to_hi();
1663 let err = self.dcx().create_err(errors::MissingConstType { span, colon, kind });
1664 err.stash(span, StashKey::ItemNoType);
1665
1666 Box::new(Ty { kind: TyKind::Infer, span, id: ast::DUMMY_NODE_ID, tokens: None })
1669 }
1670
1671 fn parse_item_enum(&mut self) -> PResult<'a, ItemKind> {
1673 if self.token.is_keyword(kw::Struct) {
1674 let span = self.prev_token.span.to(self.token.span);
1675 let err = errors::EnumStructMutuallyExclusive { span };
1676 if self.look_ahead(1, |t| t.is_ident()) {
1677 self.bump();
1678 self.dcx().emit_err(err);
1679 } else {
1680 return Err(self.dcx().create_err(err));
1681 }
1682 }
1683
1684 let prev_span = self.prev_token.span;
1685 let ident = self.parse_ident()?;
1686 let mut generics = self.parse_generics()?;
1687 generics.where_clause = self.parse_where_clause()?;
1688
1689 let (variants, _) = if self.token == TokenKind::Semi {
1691 self.dcx().emit_err(errors::UseEmptyBlockNotSemi { span: self.token.span });
1692 self.bump();
1693 (::thin_vec::ThinVec::new()thin_vec![], Trailing::No)
1694 } else {
1695 self.parse_delim_comma_seq(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBrace,
token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace), crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::CloseBrace,
token_type: crate::parser::token_type::TokenType::CloseBrace,
}exp!(CloseBrace), |p| {
1696 p.parse_enum_variant(ident.span)
1697 })
1698 .map_err(|mut err| {
1699 err.span_label(ident.span, "while parsing this enum");
1700 if self.prev_token.is_non_reserved_ident() && self.token == token::Colon {
1702 let snapshot = self.create_snapshot_for_diagnostic();
1703 self.bump();
1704 match self.parse_ty() {
1705 Ok(_) => {
1706 err.span_suggestion_verbose(
1707 prev_span,
1708 "perhaps you meant to use `struct` here",
1709 "struct",
1710 Applicability::MaybeIncorrect,
1711 );
1712 }
1713 Err(e) => {
1714 e.cancel();
1715 }
1716 }
1717 self.restore_snapshot(snapshot);
1718 }
1719 self.eat_to_tokens(&[crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::CloseBrace,
token_type: crate::parser::token_type::TokenType::CloseBrace,
}exp!(CloseBrace)]);
1720 self.bump(); err
1722 })?
1723 };
1724
1725 let enum_definition = EnumDef { variants: variants.into_iter().flatten().collect() };
1726 Ok(ItemKind::Enum(ident, generics, enum_definition))
1727 }
1728
1729 fn parse_enum_variant(&mut self, span: Span) -> PResult<'a, Option<Variant>> {
1730 self.recover_vcs_conflict_marker();
1731 let variant_attrs = self.parse_outer_attributes()?;
1732 self.recover_vcs_conflict_marker();
1733 let help = "enum variants can be `Variant`, `Variant = <integer>`, \
1734 `Variant(Type, ..., TypeN)` or `Variant { fields: Types }`";
1735 self.collect_tokens(None, variant_attrs, ForceCollect::No, |this, variant_attrs| {
1736 let vlo = this.token.span;
1737
1738 let vis = this.parse_visibility(FollowedByType::No)?;
1739 if !this.recover_nested_adt_item(kw::Enum)? {
1740 return Ok((None, Trailing::No, UsePreAttrPos::No));
1741 }
1742 let ident = this.parse_field_ident("enum", vlo)?;
1743
1744 if this.token == token::Bang {
1745 if let Err(err) = this.unexpected() {
1746 err.with_note(rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("macros cannot expand to enum variants"))inline_fluent!("macros cannot expand to enum variants")).emit();
1747 }
1748
1749 this.bump();
1750 this.parse_delim_args()?;
1751
1752 return Ok((None, Trailing::from(this.token == token::Comma), UsePreAttrPos::No));
1753 }
1754
1755 let struct_def = if this.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBrace,
token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace)) {
1756 let (fields, recovered) =
1758 match this.parse_record_struct_body("struct", ident.span, false) {
1759 Ok((fields, recovered)) => (fields, recovered),
1760 Err(mut err) => {
1761 if this.token == token::Colon {
1762 return Err(err);
1764 }
1765 this.eat_to_tokens(&[crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::CloseBrace,
token_type: crate::parser::token_type::TokenType::CloseBrace,
}exp!(CloseBrace)]);
1766 this.bump(); err.span_label(span, "while parsing this enum");
1768 err.help(help);
1769 let guar = err.emit();
1770 (::thin_vec::ThinVec::new()thin_vec![], Recovered::Yes(guar))
1771 }
1772 };
1773 VariantData::Struct { fields, recovered }
1774 } else if this.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenParen,
token_type: crate::parser::token_type::TokenType::OpenParen,
}exp!(OpenParen)) {
1775 let body = match this.parse_tuple_struct_body() {
1776 Ok(body) => body,
1777 Err(mut err) => {
1778 if this.token == token::Colon {
1779 return Err(err);
1781 }
1782 this.eat_to_tokens(&[crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::CloseParen,
token_type: crate::parser::token_type::TokenType::CloseParen,
}exp!(CloseParen)]);
1783 this.bump(); err.span_label(span, "while parsing this enum");
1785 err.help(help);
1786 err.emit();
1787 ::thin_vec::ThinVec::new()thin_vec![]
1788 }
1789 };
1790 VariantData::Tuple(body, DUMMY_NODE_ID)
1791 } else {
1792 VariantData::Unit(DUMMY_NODE_ID)
1793 };
1794
1795 let disr_expr = if this.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Eq,
token_type: crate::parser::token_type::TokenType::Eq,
}exp!(Eq)) {
1796 Some(this.parse_expr_anon_const(|_, _| MgcaDisambiguation::AnonConst)?)
1797 } else {
1798 None
1799 };
1800
1801 let vr = ast::Variant {
1802 ident,
1803 vis,
1804 id: DUMMY_NODE_ID,
1805 attrs: variant_attrs,
1806 data: struct_def,
1807 disr_expr,
1808 span: vlo.to(this.prev_token.span),
1809 is_placeholder: false,
1810 };
1811
1812 Ok((Some(vr), Trailing::from(this.token == token::Comma), UsePreAttrPos::No))
1813 })
1814 .map_err(|mut err| {
1815 err.help(help);
1816 err
1817 })
1818 }
1819
1820 fn parse_item_struct(&mut self) -> PResult<'a, ItemKind> {
1822 let ident = self.parse_ident()?;
1823
1824 let mut generics = self.parse_generics()?;
1825
1826 let vdata = if self.token.is_keyword(kw::Where) {
1841 let tuple_struct_body;
1842 (generics.where_clause, tuple_struct_body) =
1843 self.parse_struct_where_clause(ident, generics.span)?;
1844
1845 if let Some(body) = tuple_struct_body {
1846 let body = VariantData::Tuple(body, DUMMY_NODE_ID);
1848 self.expect_semi()?;
1849 body
1850 } else if self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Semi,
token_type: crate::parser::token_type::TokenType::Semi,
}exp!(Semi)) {
1851 VariantData::Unit(DUMMY_NODE_ID)
1853 } else {
1854 let (fields, recovered) = self.parse_record_struct_body(
1856 "struct",
1857 ident.span,
1858 generics.where_clause.has_where_token,
1859 )?;
1860 VariantData::Struct { fields, recovered }
1861 }
1862 } else if self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Semi,
token_type: crate::parser::token_type::TokenType::Semi,
}exp!(Semi)) {
1864 VariantData::Unit(DUMMY_NODE_ID)
1865 } else if self.token == token::OpenBrace {
1867 let (fields, recovered) = self.parse_record_struct_body(
1868 "struct",
1869 ident.span,
1870 generics.where_clause.has_where_token,
1871 )?;
1872 VariantData::Struct { fields, recovered }
1873 } else if self.token == token::OpenParen {
1875 let body = VariantData::Tuple(self.parse_tuple_struct_body()?, DUMMY_NODE_ID);
1876 generics.where_clause = self.parse_where_clause()?;
1877 self.expect_semi()?;
1878 body
1879 } else {
1880 let err = errors::UnexpectedTokenAfterStructName::new(self.token.span, self.token);
1881 return Err(self.dcx().create_err(err));
1882 };
1883
1884 Ok(ItemKind::Struct(ident, generics, vdata))
1885 }
1886
1887 fn parse_item_union(&mut self) -> PResult<'a, ItemKind> {
1889 let ident = self.parse_ident()?;
1890
1891 let mut generics = self.parse_generics()?;
1892
1893 let vdata = if self.token.is_keyword(kw::Where) {
1894 generics.where_clause = self.parse_where_clause()?;
1895 let (fields, recovered) = self.parse_record_struct_body(
1896 "union",
1897 ident.span,
1898 generics.where_clause.has_where_token,
1899 )?;
1900 VariantData::Struct { fields, recovered }
1901 } else if self.token == token::OpenBrace {
1902 let (fields, recovered) = self.parse_record_struct_body(
1903 "union",
1904 ident.span,
1905 generics.where_clause.has_where_token,
1906 )?;
1907 VariantData::Struct { fields, recovered }
1908 } else {
1909 let token_str = super::token_descr(&self.token);
1910 let msg = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("expected `where` or `{{` after union name, found {0}",
token_str))
})format!("expected `where` or `{{` after union name, found {token_str}");
1911 let mut err = self.dcx().struct_span_err(self.token.span, msg);
1912 err.span_label(self.token.span, "expected `where` or `{` after union name");
1913 return Err(err);
1914 };
1915
1916 Ok(ItemKind::Union(ident, generics, vdata))
1917 }
1918
1919 pub(crate) fn parse_record_struct_body(
1924 &mut self,
1925 adt_ty: &str,
1926 ident_span: Span,
1927 parsed_where: bool,
1928 ) -> PResult<'a, (ThinVec<FieldDef>, Recovered)> {
1929 let mut fields = ThinVec::new();
1930 let mut recovered = Recovered::No;
1931 if self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBrace,
token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace)) {
1932 while self.token != token::CloseBrace {
1933 match self.parse_field_def(adt_ty, ident_span) {
1934 Ok(field) => {
1935 fields.push(field);
1936 }
1937 Err(mut err) => {
1938 self.consume_block(
1939 crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBrace,
token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace),
1940 crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::CloseBrace,
token_type: crate::parser::token_type::TokenType::CloseBrace,
}exp!(CloseBrace),
1941 ConsumeClosingDelim::No,
1942 );
1943 err.span_label(ident_span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("while parsing this {0}", adt_ty))
})format!("while parsing this {adt_ty}"));
1944 let guar = err.emit();
1945 recovered = Recovered::Yes(guar);
1946 break;
1947 }
1948 }
1949 }
1950 self.expect(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::CloseBrace,
token_type: crate::parser::token_type::TokenType::CloseBrace,
}exp!(CloseBrace))?;
1951 } else {
1952 let token_str = super::token_descr(&self.token);
1953 let where_str = if parsed_where { "" } else { "`where`, or " };
1954 let msg = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("expected {0}`{{` after struct name, found {1}",
where_str, token_str))
})format!("expected {where_str}`{{` after struct name, found {token_str}");
1955 let mut err = self.dcx().struct_span_err(self.token.span, msg);
1956 err.span_label(self.token.span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("expected {0}`{{` after struct name",
where_str))
})format!("expected {where_str}`{{` after struct name",));
1957 return Err(err);
1958 }
1959
1960 Ok((fields, recovered))
1961 }
1962
1963 fn parse_unsafe_field(&mut self) -> Safety {
1964 if self.eat_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Unsafe,
token_type: crate::parser::token_type::TokenType::KwUnsafe,
}exp!(Unsafe)) {
1966 let span = self.prev_token.span;
1967 self.psess.gated_spans.gate(sym::unsafe_fields, span);
1968 Safety::Unsafe(span)
1969 } else {
1970 Safety::Default
1971 }
1972 }
1973
1974 pub(super) fn parse_tuple_struct_body(&mut self) -> PResult<'a, ThinVec<FieldDef>> {
1975 self.parse_paren_comma_seq(|p| {
1978 let attrs = p.parse_outer_attributes()?;
1979 p.collect_tokens(None, attrs, ForceCollect::No, |p, attrs| {
1980 let mut snapshot = None;
1981 if p.is_vcs_conflict_marker(&TokenKind::Shl, &TokenKind::Lt) {
1982 snapshot = Some(p.create_snapshot_for_diagnostic());
1986 }
1987 let lo = p.token.span;
1988 let vis = match p.parse_visibility(FollowedByType::Yes) {
1989 Ok(vis) => vis,
1990 Err(err) => {
1991 if let Some(ref mut snapshot) = snapshot {
1992 snapshot.recover_vcs_conflict_marker();
1993 }
1994 return Err(err);
1995 }
1996 };
1997 let ty = match p.parse_ty() {
2000 Ok(ty) => ty,
2001 Err(err) => {
2002 if let Some(ref mut snapshot) = snapshot {
2003 snapshot.recover_vcs_conflict_marker();
2004 }
2005 return Err(err);
2006 }
2007 };
2008 let mut default = None;
2009 if p.token == token::Eq {
2010 let mut snapshot = p.create_snapshot_for_diagnostic();
2011 snapshot.bump();
2012 match snapshot.parse_expr_anon_const(|_, _| MgcaDisambiguation::AnonConst) {
2013 Ok(const_expr) => {
2014 let sp = ty.span.shrink_to_hi().to(const_expr.value.span);
2015 p.psess.gated_spans.gate(sym::default_field_values, sp);
2016 p.restore_snapshot(snapshot);
2017 default = Some(const_expr);
2018 }
2019 Err(err) => {
2020 err.cancel();
2021 }
2022 }
2023 }
2024
2025 Ok((
2026 FieldDef {
2027 span: lo.to(ty.span),
2028 vis,
2029 safety: Safety::Default,
2030 ident: None,
2031 id: DUMMY_NODE_ID,
2032 ty,
2033 default,
2034 attrs,
2035 is_placeholder: false,
2036 },
2037 Trailing::from(p.token == token::Comma),
2038 UsePreAttrPos::No,
2039 ))
2040 })
2041 })
2042 .map(|(r, _)| r)
2043 }
2044
2045 fn parse_field_def(&mut self, adt_ty: &str, ident_span: Span) -> PResult<'a, FieldDef> {
2047 self.recover_vcs_conflict_marker();
2048 let attrs = self.parse_outer_attributes()?;
2049 self.recover_vcs_conflict_marker();
2050 self.collect_tokens(None, attrs, ForceCollect::No, |this, attrs| {
2051 let lo = this.token.span;
2052 let vis = this.parse_visibility(FollowedByType::No)?;
2053 let safety = this.parse_unsafe_field();
2054 this.parse_single_struct_field(adt_ty, lo, vis, safety, attrs, ident_span)
2055 .map(|field| (field, Trailing::No, UsePreAttrPos::No))
2056 })
2057 }
2058
2059 fn parse_single_struct_field(
2061 &mut self,
2062 adt_ty: &str,
2063 lo: Span,
2064 vis: Visibility,
2065 safety: Safety,
2066 attrs: AttrVec,
2067 ident_span: Span,
2068 ) -> PResult<'a, FieldDef> {
2069 let a_var = self.parse_name_and_ty(adt_ty, lo, vis, safety, attrs)?;
2070 match self.token.kind {
2071 token::Comma => {
2072 self.bump();
2073 }
2074 token::Semi => {
2075 self.bump();
2076 let sp = self.prev_token.span;
2077 let mut err =
2078 self.dcx().struct_span_err(sp, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} fields are separated by `,`",
adt_ty))
})format!("{adt_ty} fields are separated by `,`"));
2079 err.span_suggestion_short(
2080 sp,
2081 "replace `;` with `,`",
2082 ",",
2083 Applicability::MachineApplicable,
2084 );
2085 err.span_label(ident_span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("while parsing this {0}", adt_ty))
})format!("while parsing this {adt_ty}"));
2086 err.emit();
2087 }
2088 token::CloseBrace => {}
2089 token::DocComment(..) => {
2090 let previous_span = self.prev_token.span;
2091 let mut err = errors::DocCommentDoesNotDocumentAnything {
2092 span: self.token.span,
2093 missing_comma: None,
2094 };
2095 self.bump(); if self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Comma,
token_type: crate::parser::token_type::TokenType::Comma,
}exp!(Comma)) || self.token == token::CloseBrace {
2097 self.dcx().emit_err(err);
2098 } else {
2099 let sp = previous_span.shrink_to_hi();
2100 err.missing_comma = Some(sp);
2101 return Err(self.dcx().create_err(err));
2102 }
2103 }
2104 _ => {
2105 let sp = self.prev_token.span.shrink_to_hi();
2106 let msg =
2107 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("expected `,`, or `}}`, found {0}",
super::token_descr(&self.token)))
})format!("expected `,`, or `}}`, found {}", super::token_descr(&self.token));
2108
2109 if let TyKind::Path(_, Path { segments, .. }) = &a_var.ty.kind
2111 && let Some(last_segment) = segments.last()
2112 {
2113 let guar = self.check_trailing_angle_brackets(
2114 last_segment,
2115 &[crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Comma,
token_type: crate::parser::token_type::TokenType::Comma,
}exp!(Comma), crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::CloseBrace,
token_type: crate::parser::token_type::TokenType::CloseBrace,
}exp!(CloseBrace)],
2116 );
2117 if let Some(_guar) = guar {
2118 let _ = self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Comma,
token_type: crate::parser::token_type::TokenType::Comma,
}exp!(Comma));
2121
2122 return Ok(a_var);
2125 }
2126 }
2127
2128 let mut err = self.dcx().struct_span_err(sp, msg);
2129
2130 if self.token.is_ident()
2131 || (self.token == TokenKind::Pound
2132 && (self.look_ahead(1, |t| t == &token::OpenBracket)))
2133 {
2134 err.span_suggestion(
2137 sp,
2138 "try adding a comma",
2139 ",",
2140 Applicability::MachineApplicable,
2141 );
2142 err.emit();
2143 } else {
2144 return Err(err);
2145 }
2146 }
2147 }
2148 Ok(a_var)
2149 }
2150
2151 fn expect_field_ty_separator(&mut self) -> PResult<'a, ()> {
2152 if let Err(err) = self.expect(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Colon,
token_type: crate::parser::token_type::TokenType::Colon,
}exp!(Colon)) {
2153 let sm = self.psess.source_map();
2154 let eq_typo = self.token == token::Eq && self.look_ahead(1, |t| t.is_path_start());
2155 let semi_typo = self.token == token::Semi
2156 && self.look_ahead(1, |t| {
2157 t.is_path_start()
2158 && match (sm.lookup_line(self.token.span.hi()), sm.lookup_line(t.span.lo())) {
2161 (Ok(l), Ok(r)) => l.line == r.line,
2162 _ => true,
2163 }
2164 });
2165 if eq_typo || semi_typo {
2166 self.bump();
2167 err.with_span_suggestion_short(
2169 self.prev_token.span,
2170 "field names and their types are separated with `:`",
2171 ":",
2172 Applicability::MachineApplicable,
2173 )
2174 .emit();
2175 } else {
2176 return Err(err);
2177 }
2178 }
2179 Ok(())
2180 }
2181
2182 fn parse_name_and_ty(
2184 &mut self,
2185 adt_ty: &str,
2186 lo: Span,
2187 vis: Visibility,
2188 safety: Safety,
2189 attrs: AttrVec,
2190 ) -> PResult<'a, FieldDef> {
2191 let name = self.parse_field_ident(adt_ty, lo)?;
2192 if self.token == token::Bang {
2193 if let Err(mut err) = self.unexpected() {
2194 err.subdiagnostic(MacroExpandsToAdtField { adt_ty });
2196 return Err(err);
2197 }
2198 }
2199 self.expect_field_ty_separator()?;
2200 let ty = self.parse_ty()?;
2201 if self.token == token::Colon && self.look_ahead(1, |&t| t != token::Colon) {
2202 self.dcx()
2203 .struct_span_err(self.token.span, "found single colon in a struct field type path")
2204 .with_span_suggestion_verbose(
2205 self.token.span,
2206 "write a path separator here",
2207 "::",
2208 Applicability::MaybeIncorrect,
2209 )
2210 .emit();
2211 }
2212 let default = if self.token == token::Eq {
2213 self.bump();
2214 let const_expr = self.parse_expr_anon_const(|_, _| MgcaDisambiguation::AnonConst)?;
2215 let sp = ty.span.shrink_to_hi().to(const_expr.value.span);
2216 self.psess.gated_spans.gate(sym::default_field_values, sp);
2217 Some(const_expr)
2218 } else {
2219 None
2220 };
2221 Ok(FieldDef {
2222 span: lo.to(self.prev_token.span),
2223 ident: Some(name),
2224 vis,
2225 safety,
2226 id: DUMMY_NODE_ID,
2227 ty,
2228 default,
2229 attrs,
2230 is_placeholder: false,
2231 })
2232 }
2233
2234 fn parse_field_ident(&mut self, adt_ty: &str, lo: Span) -> PResult<'a, Ident> {
2237 let (ident, is_raw) = self.ident_or_err(true)?;
2238 if is_raw == IdentIsRaw::No && ident.is_reserved() {
2239 let snapshot = self.create_snapshot_for_diagnostic();
2240 let err = if self.check_fn_front_matter(false, Case::Sensitive) {
2241 let inherited_vis =
2242 Visibility { span: DUMMY_SP, kind: VisibilityKind::Inherited, tokens: None };
2243 let fn_parse_mode =
2245 FnParseMode { req_name: |_, _| true, context: FnContext::Free, req_body: true };
2246 match self.parse_fn(
2247 &mut AttrVec::new(),
2248 fn_parse_mode,
2249 lo,
2250 &inherited_vis,
2251 Case::Insensitive,
2252 ) {
2253 Ok(_) => {
2254 self.dcx().struct_span_err(
2255 lo.to(self.prev_token.span),
2256 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("functions are not allowed in {0} definitions",
adt_ty))
})format!("functions are not allowed in {adt_ty} definitions"),
2257 )
2258 .with_help(
2259 "unlike in C++, Java, and C#, functions are declared in `impl` blocks",
2260 )
2261 .with_help("see https://doc.rust-lang.org/book/ch05-03-method-syntax.html for more information")
2262 }
2263 Err(err) => {
2264 err.cancel();
2265 self.restore_snapshot(snapshot);
2266 self.expected_ident_found_err()
2267 }
2268 }
2269 } else if self.eat_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Struct,
token_type: crate::parser::token_type::TokenType::KwStruct,
}exp!(Struct)) {
2270 match self.parse_item_struct() {
2271 Ok(item) => {
2272 let ItemKind::Struct(ident, ..) = item else { ::core::panicking::panic("internal error: entered unreachable code")unreachable!() };
2273 self.dcx()
2274 .struct_span_err(
2275 lo.with_hi(ident.span.hi()),
2276 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("structs are not allowed in {0} definitions",
adt_ty))
})format!("structs are not allowed in {adt_ty} definitions"),
2277 )
2278 .with_help(
2279 "consider creating a new `struct` definition instead of nesting",
2280 )
2281 }
2282 Err(err) => {
2283 err.cancel();
2284 self.restore_snapshot(snapshot);
2285 self.expected_ident_found_err()
2286 }
2287 }
2288 } else {
2289 let mut err = self.expected_ident_found_err();
2290 if self.eat_keyword_noexpect(kw::Let)
2291 && let removal_span = self.prev_token.span.until(self.token.span)
2292 && let Ok(ident) = self
2293 .parse_ident_common(false)
2294 .map_err(|err| err.cancel())
2296 && self.token == TokenKind::Colon
2297 {
2298 err.span_suggestion(
2299 removal_span,
2300 "remove this `let` keyword",
2301 String::new(),
2302 Applicability::MachineApplicable,
2303 );
2304 err.note("the `let` keyword is not allowed in `struct` fields");
2305 err.note("see <https://doc.rust-lang.org/book/ch05-01-defining-structs.html> for more information");
2306 err.emit();
2307 return Ok(ident);
2308 } else {
2309 self.restore_snapshot(snapshot);
2310 }
2311 err
2312 };
2313 return Err(err);
2314 }
2315 self.bump();
2316 Ok(ident)
2317 }
2318
2319 fn parse_item_decl_macro(&mut self, lo: Span) -> PResult<'a, ItemKind> {
2327 let ident = self.parse_ident()?;
2328 let body = if self.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBrace,
token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace)) {
2329 self.parse_delim_args()? } else if self.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenParen,
token_type: crate::parser::token_type::TokenType::OpenParen,
}exp!(OpenParen)) {
2331 let params = self.parse_token_tree(); let pspan = params.span();
2333 if !self.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBrace,
token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace)) {
2334 self.unexpected()?;
2335 }
2336 let body = self.parse_token_tree(); let bspan = body.span();
2339 let arrow = TokenTree::token_alone(token::FatArrow, pspan.between(bspan)); let tokens = TokenStream::new(<[_]>::into_vec(::alloc::boxed::box_new([params, arrow, body]))vec![params, arrow, body]);
2341 let dspan = DelimSpan::from_pair(pspan.shrink_to_lo(), bspan.shrink_to_hi());
2342 Box::new(DelimArgs { dspan, delim: Delimiter::Brace, tokens })
2343 } else {
2344 self.unexpected_any()?
2345 };
2346
2347 self.psess.gated_spans.gate(sym::decl_macro, lo.to(self.prev_token.span));
2348 Ok(ItemKind::MacroDef(
2349 ident,
2350 ast::MacroDef { body, macro_rules: false, eii_declaration: None },
2351 ))
2352 }
2353
2354 fn is_macro_rules_item(&mut self) -> IsMacroRulesItem {
2356 if self.check_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::MacroRules,
token_type: crate::parser::token_type::TokenType::KwMacroRules,
}exp!(MacroRules)) {
2357 let macro_rules_span = self.token.span;
2358
2359 if self.look_ahead(1, |t| *t == token::Bang) && self.look_ahead(2, |t| t.is_ident()) {
2360 return IsMacroRulesItem::Yes { has_bang: true };
2361 } else if self.look_ahead(1, |t| t.is_ident()) {
2362 self.dcx().emit_err(errors::MacroRulesMissingBang {
2364 span: macro_rules_span,
2365 hi: macro_rules_span.shrink_to_hi(),
2366 });
2367
2368 return IsMacroRulesItem::Yes { has_bang: false };
2369 }
2370 }
2371
2372 IsMacroRulesItem::No
2373 }
2374
2375 fn parse_item_macro_rules(
2377 &mut self,
2378 vis: &Visibility,
2379 has_bang: bool,
2380 ) -> PResult<'a, ItemKind> {
2381 self.expect_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::MacroRules,
token_type: crate::parser::token_type::TokenType::KwMacroRules,
}exp!(MacroRules))?; if has_bang {
2384 self.expect(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Bang,
token_type: crate::parser::token_type::TokenType::Bang,
}exp!(Bang))?; }
2386 let ident = self.parse_ident()?;
2387
2388 if self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Bang,
token_type: crate::parser::token_type::TokenType::Bang,
}exp!(Bang)) {
2389 let span = self.prev_token.span;
2391 self.dcx().emit_err(errors::MacroNameRemoveBang { span });
2392 }
2393
2394 let body = self.parse_delim_args()?;
2395 self.eat_semi_for_macro_if_needed(&body, None);
2396 self.complain_if_pub_macro(vis, true);
2397
2398 Ok(ItemKind::MacroDef(
2399 ident,
2400 ast::MacroDef { body, macro_rules: true, eii_declaration: None },
2401 ))
2402 }
2403
2404 fn complain_if_pub_macro(&self, vis: &Visibility, macro_rules: bool) {
2407 if let VisibilityKind::Inherited = vis.kind {
2408 return;
2409 }
2410
2411 let vstr = pprust::vis_to_string(vis);
2412 let vstr = vstr.trim_end();
2413 if macro_rules {
2414 self.dcx().emit_err(errors::MacroRulesVisibility { span: vis.span, vis: vstr });
2415 } else {
2416 self.dcx().emit_err(errors::MacroInvocationVisibility { span: vis.span, vis: vstr });
2417 }
2418 }
2419
2420 fn eat_semi_for_macro_if_needed(&mut self, args: &DelimArgs, path: Option<&Path>) {
2421 if args.need_semicolon() && !self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Semi,
token_type: crate::parser::token_type::TokenType::Semi,
}exp!(Semi)) {
2422 self.report_invalid_macro_expansion_item(args, path);
2423 }
2424 }
2425
2426 fn report_invalid_macro_expansion_item(&self, args: &DelimArgs, path: Option<&Path>) {
2427 let span = args.dspan.entire();
2428 let mut err = self.dcx().struct_span_err(
2429 span,
2430 "macros that expand to items must be delimited with braces or followed by a semicolon",
2431 );
2432 if !span.from_expansion() {
2435 let DelimSpan { open, close } = args.dspan;
2436 if let Some(path) = path
2439 && path.segments.first().is_some_and(|seg| seg.ident.name == sym::macro_rules)
2440 && args.delim == Delimiter::Parenthesis
2441 {
2442 let replace =
2443 if path.span.hi() + rustc_span::BytePos(1) < open.lo() { "" } else { " " };
2444 err.multipart_suggestion(
2445 "to define a macro, remove the parentheses around the macro name",
2446 <[_]>::into_vec(::alloc::boxed::box_new([(open, replace.to_string()),
(close, String::new())]))vec![(open, replace.to_string()), (close, String::new())],
2447 Applicability::MachineApplicable,
2448 );
2449 } else {
2450 err.multipart_suggestion(
2451 "change the delimiters to curly braces",
2452 <[_]>::into_vec(::alloc::boxed::box_new([(open, "{".to_string()),
(close, '}'.to_string())]))vec![(open, "{".to_string()), (close, '}'.to_string())],
2453 Applicability::MaybeIncorrect,
2454 );
2455 err.span_suggestion(
2456 span.with_neighbor(self.token.span).shrink_to_hi(),
2457 "add a semicolon",
2458 ';',
2459 Applicability::MaybeIncorrect,
2460 );
2461 }
2462 }
2463 err.emit();
2464 }
2465
2466 fn recover_nested_adt_item(&mut self, keyword: Symbol) -> PResult<'a, bool> {
2469 if (self.token.is_keyword(kw::Enum)
2470 || self.token.is_keyword(kw::Struct)
2471 || self.token.is_keyword(kw::Union))
2472 && self.look_ahead(1, |t| t.is_ident())
2473 {
2474 let kw_token = self.token;
2475 let kw_str = pprust::token_to_string(&kw_token);
2476 let item = self.parse_item(
2477 ForceCollect::No,
2478 AllowConstBlockItems::DoesNotMatter, )?;
2480 let mut item = item.unwrap().span;
2481 if self.token == token::Comma {
2482 item = item.to(self.token.span);
2483 }
2484 self.dcx().emit_err(errors::NestedAdt {
2485 span: kw_token.span,
2486 item,
2487 kw_str,
2488 keyword: keyword.as_str(),
2489 });
2490 return Ok(false);
2492 }
2493 Ok(true)
2494 }
2495}
2496
2497type ReqName = fn(Edition, IsDotDotDot) -> bool;
2506
2507#[derive(#[automatically_derived]
impl ::core::marker::Copy for IsDotDotDot { }Copy, #[automatically_derived]
impl ::core::clone::Clone for IsDotDotDot {
#[inline]
fn clone(&self) -> IsDotDotDot { *self }
}Clone, #[automatically_derived]
impl ::core::cmp::PartialEq for IsDotDotDot {
#[inline]
fn eq(&self, other: &IsDotDotDot) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr
}
}PartialEq)]
2508pub(crate) enum IsDotDotDot {
2509 Yes,
2510 No,
2511}
2512
2513#[derive(#[automatically_derived]
impl ::core::clone::Clone for FnParseMode {
#[inline]
fn clone(&self) -> FnParseMode {
let _: ::core::clone::AssertParamIsClone<ReqName>;
let _: ::core::clone::AssertParamIsClone<FnContext>;
let _: ::core::clone::AssertParamIsClone<bool>;
*self
}
}Clone, #[automatically_derived]
impl ::core::marker::Copy for FnParseMode { }Copy)]
2521pub(crate) struct FnParseMode {
2522 pub(super) req_name: ReqName,
2548 pub(super) context: FnContext,
2551 pub(super) req_body: bool,
2570}
2571
2572#[derive(#[automatically_derived]
impl ::core::clone::Clone for FnContext {
#[inline]
fn clone(&self) -> FnContext { *self }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for FnContext { }Copy, #[automatically_derived]
impl ::core::cmp::PartialEq for FnContext {
#[inline]
fn eq(&self, other: &FnContext) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr
}
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for FnContext {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_receiver_is_total_eq(&self) {}
}Eq)]
2575pub(crate) enum FnContext {
2576 Free,
2578 Trait,
2580 Impl,
2582}
2583
2584impl<'a> Parser<'a> {
2586 fn parse_fn(
2588 &mut self,
2589 attrs: &mut AttrVec,
2590 fn_parse_mode: FnParseMode,
2591 sig_lo: Span,
2592 vis: &Visibility,
2593 case: Case,
2594 ) -> PResult<'a, (Ident, FnSig, Generics, Option<Box<FnContract>>, Option<Box<Block>>)> {
2595 let fn_span = self.token.span;
2596 let header = self.parse_fn_front_matter(vis, case, FrontMatterParsingMode::Function)?; let ident = self.parse_ident()?; let mut generics = self.parse_generics()?; let decl = match self.parse_fn_decl(&fn_parse_mode, AllowPlus::Yes, RecoverReturnSign::Yes)
2600 {
2601 Ok(decl) => decl,
2602 Err(old_err) => {
2603 if self.token.is_keyword(kw::For) {
2605 old_err.cancel();
2606 return Err(self.dcx().create_err(errors::FnTypoWithImpl { fn_span }));
2607 } else {
2608 return Err(old_err);
2609 }
2610 }
2611 };
2612
2613 let fn_params_end = self.prev_token.span.shrink_to_hi();
2616
2617 let contract = self.parse_contract()?;
2618
2619 generics.where_clause = self.parse_where_clause()?; let fn_params_end =
2623 if generics.where_clause.has_where_token { Some(fn_params_end) } else { None };
2624
2625 let mut sig_hi = self.prev_token.span;
2626 let body =
2628 self.parse_fn_body(attrs, &ident, &mut sig_hi, fn_parse_mode.req_body, fn_params_end)?;
2629 let fn_sig_span = sig_lo.to(sig_hi);
2630 Ok((ident, FnSig { header, decl, span: fn_sig_span }, generics, contract, body))
2631 }
2632
2633 fn error_fn_body_not_found(
2635 &mut self,
2636 ident_span: Span,
2637 req_body: bool,
2638 fn_params_end: Option<Span>,
2639 ) -> PResult<'a, ErrorGuaranteed> {
2640 let expected: &[_] =
2641 if req_body { &[crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBrace,
token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace)] } else { &[crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Semi,
token_type: crate::parser::token_type::TokenType::Semi,
}exp!(Semi), crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBrace,
token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace)] };
2642 match self.expected_one_of_not_found(&[], expected) {
2643 Ok(error_guaranteed) => Ok(error_guaranteed),
2644 Err(mut err) => {
2645 if self.token == token::CloseBrace {
2646 err.span_label(ident_span, "while parsing this `fn`");
2649 Ok(err.emit())
2650 } else if self.token == token::RArrow
2651 && let Some(fn_params_end) = fn_params_end
2652 {
2653 let fn_trait_span =
2659 [sym::FnOnce, sym::FnMut, sym::Fn].into_iter().find_map(|symbol| {
2660 if self.prev_token.is_ident_named(symbol) {
2661 Some(self.prev_token.span)
2662 } else {
2663 None
2664 }
2665 });
2666
2667 let arrow_span = self.token.span;
2672 let ty_span = match self.parse_ret_ty(
2673 AllowPlus::Yes,
2674 RecoverQPath::Yes,
2675 RecoverReturnSign::Yes,
2676 ) {
2677 Ok(ty_span) => ty_span.span().shrink_to_hi(),
2678 Err(parse_error) => {
2679 parse_error.cancel();
2680 return Err(err);
2681 }
2682 };
2683 let ret_ty_span = arrow_span.to(ty_span);
2684
2685 if let Some(fn_trait_span) = fn_trait_span {
2686 err.subdiagnostic(errors::FnTraitMissingParen { span: fn_trait_span });
2689 } else if let Ok(snippet) = self.psess.source_map().span_to_snippet(ret_ty_span)
2690 {
2691 err.primary_message(
2695 "return type should be specified after the function parameters",
2696 );
2697 err.subdiagnostic(errors::MisplacedReturnType {
2698 fn_params_end,
2699 snippet,
2700 ret_ty_span,
2701 });
2702 }
2703 Err(err)
2704 } else {
2705 Err(err)
2706 }
2707 }
2708 }
2709 }
2710
2711 fn parse_fn_body(
2715 &mut self,
2716 attrs: &mut AttrVec,
2717 ident: &Ident,
2718 sig_hi: &mut Span,
2719 req_body: bool,
2720 fn_params_end: Option<Span>,
2721 ) -> PResult<'a, Option<Box<Block>>> {
2722 let has_semi = if req_body {
2723 self.token == TokenKind::Semi
2724 } else {
2725 self.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Semi,
token_type: crate::parser::token_type::TokenType::Semi,
}exp!(Semi))
2727 };
2728 let (inner_attrs, body) = if has_semi {
2729 self.expect_semi()?;
2731 *sig_hi = self.prev_token.span;
2732 (AttrVec::new(), None)
2733 } else if self.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBrace,
token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace)) || self.token.is_metavar_block() {
2734 self.parse_block_common(self.token.span, BlockCheckMode::Default, None)
2735 .map(|(attrs, body)| (attrs, Some(body)))?
2736 } else if self.token == token::Eq {
2737 self.bump(); let eq_sp = self.prev_token.span;
2740 let _ = self.parse_expr()?;
2741 self.expect_semi()?; let span = eq_sp.to(self.prev_token.span);
2743 let guar = self.dcx().emit_err(errors::FunctionBodyEqualsExpr {
2744 span,
2745 sugg: errors::FunctionBodyEqualsExprSugg { eq: eq_sp, semi: self.prev_token.span },
2746 });
2747 (AttrVec::new(), Some(self.mk_block_err(span, guar)))
2748 } else {
2749 self.error_fn_body_not_found(ident.span, req_body, fn_params_end)?;
2750 (AttrVec::new(), None)
2751 };
2752 attrs.extend(inner_attrs);
2753 Ok(body)
2754 }
2755
2756 fn check_impl_frontmatter(&mut self, look_ahead: usize) -> bool {
2757 const ALL_QUALS: &[Symbol] = &[kw::Const, kw::Unsafe];
2758 if self.check_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Impl,
token_type: crate::parser::token_type::TokenType::KwImpl,
}exp!(Impl)) {
2761 return true;
2762 }
2763 let mut i = 0;
2764 while i < ALL_QUALS.len() {
2765 let action = self.look_ahead(i + look_ahead, |token| {
2766 if token.is_keyword(kw::Impl) {
2767 return Some(true);
2768 }
2769 if ALL_QUALS.iter().any(|&qual| token.is_keyword(qual)) {
2770 return None;
2772 }
2773 Some(false)
2774 });
2775 if let Some(ret) = action {
2776 return ret;
2777 }
2778 i += 1;
2779 }
2780
2781 self.is_keyword_ahead(i, &[kw::Impl])
2782 }
2783
2784 pub(super) fn check_fn_front_matter(&mut self, check_pub: bool, case: Case) -> bool {
2789 const ALL_QUALS: &[ExpKeywordPair] = &[
2790 crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Pub,
token_type: crate::parser::token_type::TokenType::KwPub,
}exp!(Pub),
2791 crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Gen,
token_type: crate::parser::token_type::TokenType::KwGen,
}exp!(Gen),
2792 crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Const,
token_type: crate::parser::token_type::TokenType::KwConst,
}exp!(Const),
2793 crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Async,
token_type: crate::parser::token_type::TokenType::KwAsync,
}exp!(Async),
2794 crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Unsafe,
token_type: crate::parser::token_type::TokenType::KwUnsafe,
}exp!(Unsafe),
2795 crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Safe,
token_type: crate::parser::token_type::TokenType::KwSafe,
}exp!(Safe),
2796 crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Extern,
token_type: crate::parser::token_type::TokenType::KwExtern,
}exp!(Extern),
2797 ];
2798
2799 let quals: &[_] = if check_pub {
2804 ALL_QUALS
2805 } else {
2806 &[crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Gen,
token_type: crate::parser::token_type::TokenType::KwGen,
}exp!(Gen), crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Const,
token_type: crate::parser::token_type::TokenType::KwConst,
}exp!(Const), crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Async,
token_type: crate::parser::token_type::TokenType::KwAsync,
}exp!(Async), crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Unsafe,
token_type: crate::parser::token_type::TokenType::KwUnsafe,
}exp!(Unsafe), crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Safe,
token_type: crate::parser::token_type::TokenType::KwSafe,
}exp!(Safe), crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Extern,
token_type: crate::parser::token_type::TokenType::KwExtern,
}exp!(Extern)]
2807 };
2808 self.check_keyword_case(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Fn,
token_type: crate::parser::token_type::TokenType::KwFn,
}exp!(Fn), case) || quals.iter().any(|&exp| self.check_keyword_case(exp, case))
2811 && self.look_ahead(1, |t| {
2812 t.is_keyword_case(kw::Fn, case)
2814 || (
2816 (
2817 t.is_non_raw_ident_where(|i|
2818 quals.iter().any(|exp| exp.kw == i.name)
2819 && i.is_reserved()
2821 )
2822 || case == Case::Insensitive
2823 && t.is_non_raw_ident_where(|i| quals.iter().any(|exp| {
2824 exp.kw.as_str() == i.name.as_str().to_lowercase()
2825 }))
2826 )
2827 && !self.is_unsafe_foreign_mod()
2829 && !self.is_async_gen_block()
2831 && !self.is_keyword_ahead(2, &[kw::Auto, kw::Trait])
2833 )
2834 })
2835 || self.check_keyword_case(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Extern,
token_type: crate::parser::token_type::TokenType::KwExtern,
}exp!(Extern), case)
2837 && self.look_ahead(1, |t| t.can_begin_string_literal())
2841 && (self.tree_look_ahead(2, |tt| {
2842 match tt {
2843 TokenTree::Token(t, _) => t.is_keyword_case(kw::Fn, case),
2844 TokenTree::Delimited(..) => false,
2845 }
2846 }) == Some(true) ||
2847 (self.may_recover()
2850 && self.tree_look_ahead(2, |tt| {
2851 match tt {
2852 TokenTree::Token(t, _) =>
2853 ALL_QUALS.iter().any(|exp| {
2854 t.is_keyword(exp.kw)
2855 }),
2856 TokenTree::Delimited(..) => false,
2857 }
2858 }) == Some(true)
2859 && self.tree_look_ahead(3, |tt| {
2860 match tt {
2861 TokenTree::Token(t, _) => t.is_keyword_case(kw::Fn, case),
2862 TokenTree::Delimited(..) => false,
2863 }
2864 }) == Some(true)
2865 )
2866 )
2867 }
2868
2869 pub(super) fn parse_fn_front_matter(
2884 &mut self,
2885 orig_vis: &Visibility,
2886 case: Case,
2887 parsing_mode: FrontMatterParsingMode,
2888 ) -> PResult<'a, FnHeader> {
2889 let sp_start = self.token.span;
2890 let constness = self.parse_constness(case);
2891 if parsing_mode == FrontMatterParsingMode::FunctionPtrType
2892 && let Const::Yes(const_span) = constness
2893 {
2894 self.dcx().emit_err(FnPointerCannotBeConst {
2895 span: const_span,
2896 suggestion: const_span.until(self.token.span),
2897 });
2898 }
2899
2900 let async_start_sp = self.token.span;
2901 let coroutine_kind = self.parse_coroutine_kind(case);
2902 if parsing_mode == FrontMatterParsingMode::FunctionPtrType
2903 && let Some(ast::CoroutineKind::Async { span: async_span, .. }) = coroutine_kind
2904 {
2905 self.dcx().emit_err(FnPointerCannotBeAsync {
2906 span: async_span,
2907 suggestion: async_span.until(self.token.span),
2908 });
2909 }
2910 let unsafe_start_sp = self.token.span;
2913 let safety = self.parse_safety(case);
2914
2915 let ext_start_sp = self.token.span;
2916 let ext = self.parse_extern(case);
2917
2918 if let Some(CoroutineKind::Async { span, .. }) = coroutine_kind {
2919 if span.is_rust_2015() {
2920 self.dcx().emit_err(errors::AsyncFnIn2015 {
2921 span,
2922 help: errors::HelpUseLatestEdition::new(),
2923 });
2924 }
2925 }
2926
2927 match coroutine_kind {
2928 Some(CoroutineKind::Gen { span, .. }) | Some(CoroutineKind::AsyncGen { span, .. }) => {
2929 self.psess.gated_spans.gate(sym::gen_blocks, span);
2930 }
2931 Some(CoroutineKind::Async { .. }) | None => {}
2932 }
2933
2934 if !self.eat_keyword_case(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Fn,
token_type: crate::parser::token_type::TokenType::KwFn,
}exp!(Fn), case) {
2935 match self.expect_one_of(&[], &[]) {
2939 Ok(Recovered::Yes(_)) => {}
2940 Ok(Recovered::No) => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
2941 Err(mut err) => {
2942 enum WrongKw {
2944 Duplicated(Span),
2945 Misplaced(Span),
2946 MisplacedDisallowedQualifier,
2951 }
2952
2953 let mut recover_constness = constness;
2955 let mut recover_coroutine_kind = coroutine_kind;
2956 let mut recover_safety = safety;
2957 let wrong_kw = if self.check_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Const,
token_type: crate::parser::token_type::TokenType::KwConst,
}exp!(Const)) {
2960 match constness {
2961 Const::Yes(sp) => Some(WrongKw::Duplicated(sp)),
2962 Const::No => {
2963 recover_constness = Const::Yes(self.token.span);
2964 match parsing_mode {
2965 FrontMatterParsingMode::Function => {
2966 Some(WrongKw::Misplaced(async_start_sp))
2967 }
2968 FrontMatterParsingMode::FunctionPtrType => {
2969 self.dcx().emit_err(FnPointerCannotBeConst {
2970 span: self.token.span,
2971 suggestion: self
2972 .token
2973 .span
2974 .with_lo(self.prev_token.span.hi()),
2975 });
2976 Some(WrongKw::MisplacedDisallowedQualifier)
2977 }
2978 }
2979 }
2980 }
2981 } else if self.check_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Async,
token_type: crate::parser::token_type::TokenType::KwAsync,
}exp!(Async)) {
2982 match coroutine_kind {
2983 Some(CoroutineKind::Async { span, .. }) => {
2984 Some(WrongKw::Duplicated(span))
2985 }
2986 Some(CoroutineKind::AsyncGen { span, .. }) => {
2987 Some(WrongKw::Duplicated(span))
2988 }
2989 Some(CoroutineKind::Gen { .. }) => {
2990 recover_coroutine_kind = Some(CoroutineKind::AsyncGen {
2991 span: self.token.span,
2992 closure_id: DUMMY_NODE_ID,
2993 return_impl_trait_id: DUMMY_NODE_ID,
2994 });
2995 Some(WrongKw::Misplaced(unsafe_start_sp))
2997 }
2998 None => {
2999 recover_coroutine_kind = Some(CoroutineKind::Async {
3000 span: self.token.span,
3001 closure_id: DUMMY_NODE_ID,
3002 return_impl_trait_id: DUMMY_NODE_ID,
3003 });
3004 match parsing_mode {
3005 FrontMatterParsingMode::Function => {
3006 Some(WrongKw::Misplaced(async_start_sp))
3007 }
3008 FrontMatterParsingMode::FunctionPtrType => {
3009 self.dcx().emit_err(FnPointerCannotBeAsync {
3010 span: self.token.span,
3011 suggestion: self
3012 .token
3013 .span
3014 .with_lo(self.prev_token.span.hi()),
3015 });
3016 Some(WrongKw::MisplacedDisallowedQualifier)
3017 }
3018 }
3019 }
3020 }
3021 } else if self.check_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Unsafe,
token_type: crate::parser::token_type::TokenType::KwUnsafe,
}exp!(Unsafe)) {
3022 match safety {
3023 Safety::Unsafe(sp) => Some(WrongKw::Duplicated(sp)),
3024 Safety::Safe(sp) => {
3025 recover_safety = Safety::Unsafe(self.token.span);
3026 Some(WrongKw::Misplaced(sp))
3027 }
3028 Safety::Default => {
3029 recover_safety = Safety::Unsafe(self.token.span);
3030 Some(WrongKw::Misplaced(ext_start_sp))
3031 }
3032 }
3033 } else if self.check_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Safe,
token_type: crate::parser::token_type::TokenType::KwSafe,
}exp!(Safe)) {
3034 match safety {
3035 Safety::Safe(sp) => Some(WrongKw::Duplicated(sp)),
3036 Safety::Unsafe(sp) => {
3037 recover_safety = Safety::Safe(self.token.span);
3038 Some(WrongKw::Misplaced(sp))
3039 }
3040 Safety::Default => {
3041 recover_safety = Safety::Safe(self.token.span);
3042 Some(WrongKw::Misplaced(ext_start_sp))
3043 }
3044 }
3045 } else {
3046 None
3047 };
3048
3049 if let Some(WrongKw::Duplicated(original_sp)) = wrong_kw {
3051 let original_kw = self
3052 .span_to_snippet(original_sp)
3053 .expect("Span extracted directly from keyword should always work");
3054
3055 err.span_suggestion(
3056 self.token_uninterpolated_span(),
3057 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}` already used earlier, remove this one",
original_kw))
})format!("`{original_kw}` already used earlier, remove this one"),
3058 "",
3059 Applicability::MachineApplicable,
3060 )
3061 .span_note(original_sp, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}` first seen here",
original_kw))
})format!("`{original_kw}` first seen here"));
3062 }
3063 else if let Some(WrongKw::Misplaced(correct_pos_sp)) = wrong_kw {
3065 let correct_pos_sp = correct_pos_sp.to(self.prev_token.span);
3066 if let Ok(current_qual) = self.span_to_snippet(correct_pos_sp) {
3067 let misplaced_qual_sp = self.token_uninterpolated_span();
3068 let misplaced_qual = self.span_to_snippet(misplaced_qual_sp).unwrap();
3069
3070 err.span_suggestion(
3071 correct_pos_sp.to(misplaced_qual_sp),
3072 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}` must come before `{1}`",
misplaced_qual, current_qual))
})format!("`{misplaced_qual}` must come before `{current_qual}`"),
3073 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} {1}", misplaced_qual,
current_qual))
})format!("{misplaced_qual} {current_qual}"),
3074 Applicability::MachineApplicable,
3075 ).note("keyword order for functions declaration is `pub`, `default`, `const`, `async`, `unsafe`, `extern`");
3076 }
3077 }
3078 else if self.check_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Pub,
token_type: crate::parser::token_type::TokenType::KwPub,
}exp!(Pub)) {
3080 let sp = sp_start.to(self.prev_token.span);
3081 if let Ok(snippet) = self.span_to_snippet(sp) {
3082 let current_vis = match self.parse_visibility(FollowedByType::No) {
3083 Ok(v) => v,
3084 Err(d) => {
3085 d.cancel();
3086 return Err(err);
3087 }
3088 };
3089 let vs = pprust::vis_to_string(¤t_vis);
3090 let vs = vs.trim_end();
3091
3092 if #[allow(non_exhaustive_omitted_patterns)] match orig_vis.kind {
VisibilityKind::Inherited => true,
_ => false,
}matches!(orig_vis.kind, VisibilityKind::Inherited) {
3094 err.span_suggestion(
3095 sp_start.to(self.prev_token.span),
3096 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("visibility `{0}` must come before `{1}`",
vs, snippet))
})format!("visibility `{vs}` must come before `{snippet}`"),
3097 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} {1}", vs, snippet))
})format!("{vs} {snippet}"),
3098 Applicability::MachineApplicable,
3099 );
3100 }
3101 else {
3103 err.span_suggestion(
3104 current_vis.span,
3105 "there is already a visibility modifier, remove one",
3106 "",
3107 Applicability::MachineApplicable,
3108 )
3109 .span_note(orig_vis.span, "explicit visibility first seen here");
3110 }
3111 }
3112 }
3113
3114 if let Some(wrong_kw) = wrong_kw
3117 && self.may_recover()
3118 && self.look_ahead(1, |tok| tok.is_keyword_case(kw::Fn, case))
3119 {
3120 self.bump();
3122 self.bump();
3123 if #[allow(non_exhaustive_omitted_patterns)] match wrong_kw {
WrongKw::MisplacedDisallowedQualifier => true,
_ => false,
}matches!(wrong_kw, WrongKw::MisplacedDisallowedQualifier) {
3126 err.cancel();
3127 } else {
3128 err.emit();
3129 }
3130 return Ok(FnHeader {
3131 constness: recover_constness,
3132 safety: recover_safety,
3133 coroutine_kind: recover_coroutine_kind,
3134 ext,
3135 });
3136 }
3137
3138 return Err(err);
3139 }
3140 }
3141 }
3142
3143 Ok(FnHeader { constness, safety, coroutine_kind, ext })
3144 }
3145
3146 pub(super) fn parse_fn_decl(
3148 &mut self,
3149 fn_parse_mode: &FnParseMode,
3150 ret_allow_plus: AllowPlus,
3151 recover_return_sign: RecoverReturnSign,
3152 ) -> PResult<'a, Box<FnDecl>> {
3153 Ok(Box::new(FnDecl {
3154 inputs: self.parse_fn_params(fn_parse_mode)?,
3155 output: self.parse_ret_ty(ret_allow_plus, RecoverQPath::Yes, recover_return_sign)?,
3156 }))
3157 }
3158
3159 pub(super) fn parse_fn_params(
3161 &mut self,
3162 fn_parse_mode: &FnParseMode,
3163 ) -> PResult<'a, ThinVec<Param>> {
3164 let mut first_param = true;
3165 if self.token != TokenKind::OpenParen
3167 && !self.token.is_keyword(kw::For)
3169 {
3170 self.dcx()
3172 .emit_err(errors::MissingFnParams { span: self.prev_token.span.shrink_to_hi() });
3173 return Ok(ThinVec::new());
3174 }
3175
3176 let (mut params, _) = self.parse_paren_comma_seq(|p| {
3177 p.recover_vcs_conflict_marker();
3178 let snapshot = p.create_snapshot_for_diagnostic();
3179 let param = p.parse_param_general(fn_parse_mode, first_param, true).or_else(|e| {
3180 let guar = e.emit();
3181 let lo = if let TokenKind::OpenParen = p.prev_token.kind {
3185 p.prev_token.span.shrink_to_hi()
3186 } else {
3187 p.prev_token.span
3188 };
3189 p.restore_snapshot(snapshot);
3190 p.eat_to_tokens(&[crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Comma,
token_type: crate::parser::token_type::TokenType::Comma,
}exp!(Comma), crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::CloseParen,
token_type: crate::parser::token_type::TokenType::CloseParen,
}exp!(CloseParen)]);
3192 Ok(dummy_arg(Ident::new(sym::dummy, lo.to(p.prev_token.span)), guar))
3194 });
3195 first_param = false;
3197 param
3198 })?;
3199 self.deduplicate_recovered_params_names(&mut params);
3201 Ok(params)
3202 }
3203
3204 pub(super) fn parse_param_general(
3209 &mut self,
3210 fn_parse_mode: &FnParseMode,
3211 first_param: bool,
3212 recover_arg_parse: bool,
3213 ) -> PResult<'a, Param> {
3214 let lo = self.token.span;
3215 let attrs = self.parse_outer_attributes()?;
3216 self.collect_tokens(None, attrs, ForceCollect::No, |this, attrs| {
3217 if let Some(mut param) = this.parse_self_param()? {
3219 param.attrs = attrs;
3220 let res = if first_param { Ok(param) } else { this.recover_bad_self_param(param) };
3221 return Ok((res?, Trailing::No, UsePreAttrPos::No));
3222 }
3223
3224 let is_dot_dot_dot = if this.token.kind == token::DotDotDot {
3225 IsDotDotDot::Yes
3226 } else {
3227 IsDotDotDot::No
3228 };
3229 let is_name_required = (fn_parse_mode.req_name)(
3230 this.token.span.with_neighbor(this.prev_token.span).edition(),
3231 is_dot_dot_dot,
3232 );
3233 let is_name_required = if is_name_required && is_dot_dot_dot == IsDotDotDot::Yes {
3234 this.psess.buffer_lint(
3235 VARARGS_WITHOUT_PATTERN,
3236 this.token.span,
3237 ast::CRATE_NODE_ID,
3238 errors::VarargsWithoutPattern { span: this.token.span },
3239 );
3240 false
3241 } else {
3242 is_name_required
3243 };
3244 let (pat, ty) = if is_name_required || this.is_named_param() {
3245 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_parse/src/parser/item.rs:3245",
"rustc_parse::parser::item", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_parse/src/parser/item.rs"),
::tracing_core::__macro_support::Option::Some(3245u32),
::tracing_core::__macro_support::Option::Some("rustc_parse::parser::item"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("parse_param_general parse_pat (is_name_required:{0})",
is_name_required) as &dyn Value))])
});
} else { ; }
};debug!("parse_param_general parse_pat (is_name_required:{})", is_name_required);
3246 let (pat, colon) = this.parse_fn_param_pat_colon()?;
3247 if !colon {
3248 let mut err = this.unexpected().unwrap_err();
3249 return if let Some(ident) = this.parameter_without_type(
3250 &mut err,
3251 pat,
3252 is_name_required,
3253 first_param,
3254 fn_parse_mode,
3255 ) {
3256 let guar = err.emit();
3257 Ok((dummy_arg(ident, guar), Trailing::No, UsePreAttrPos::No))
3258 } else {
3259 Err(err)
3260 };
3261 }
3262
3263 this.eat_incorrect_doc_comment_for_param_type();
3264 (pat, this.parse_ty_for_param()?)
3265 } else {
3266 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_parse/src/parser/item.rs:3266",
"rustc_parse::parser::item", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_parse/src/parser/item.rs"),
::tracing_core::__macro_support::Option::Some(3266u32),
::tracing_core::__macro_support::Option::Some("rustc_parse::parser::item"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("parse_param_general ident_to_pat")
as &dyn Value))])
});
} else { ; }
};debug!("parse_param_general ident_to_pat");
3267 let parser_snapshot_before_ty = this.create_snapshot_for_diagnostic();
3268 this.eat_incorrect_doc_comment_for_param_type();
3269 let mut ty = this.parse_ty_for_param();
3270
3271 if let Ok(t) = &ty {
3272 if let TyKind::Path(_, Path { segments, .. }) = &t.kind
3274 && let Some(segment) = segments.last()
3275 && let Some(guar) =
3276 this.check_trailing_angle_brackets(segment, &[crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::CloseParen,
token_type: crate::parser::token_type::TokenType::CloseParen,
}exp!(CloseParen)])
3277 {
3278 return Ok((
3279 dummy_arg(segment.ident, guar),
3280 Trailing::No,
3281 UsePreAttrPos::No,
3282 ));
3283 }
3284
3285 if this.token != token::Comma && this.token != token::CloseParen {
3286 ty = this.unexpected_any();
3289 }
3290 }
3291 match ty {
3292 Ok(ty) => {
3293 let pat = this.mk_pat(ty.span, PatKind::Missing);
3294 (Box::new(pat), ty)
3295 }
3296 Err(err) if this.token == token::DotDotDot => return Err(err),
3298 Err(err) if this.unmatched_angle_bracket_count > 0 => return Err(err),
3299 Err(err) if recover_arg_parse => {
3300 err.cancel();
3302 this.restore_snapshot(parser_snapshot_before_ty);
3303 this.recover_arg_parse()?
3304 }
3305 Err(err) => return Err(err),
3306 }
3307 };
3308
3309 let span = lo.to(this.prev_token.span);
3310
3311 Ok((
3312 Param { attrs, id: ast::DUMMY_NODE_ID, is_placeholder: false, pat, span, ty },
3313 Trailing::No,
3314 UsePreAttrPos::No,
3315 ))
3316 })
3317 }
3318
3319 fn parse_self_param(&mut self) -> PResult<'a, Option<Param>> {
3321 let expect_self_ident = |this: &mut Self| match this.token.ident() {
3323 Some((ident, IdentIsRaw::No)) => {
3324 this.bump();
3325 ident
3326 }
3327 _ => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
3328 };
3329 let is_lifetime = |this: &Self, n| this.look_ahead(n, |t| t.is_lifetime());
3331 let is_isolated_self = |this: &Self, n| {
3333 this.is_keyword_ahead(n, &[kw::SelfLower])
3334 && this.look_ahead(n + 1, |t| t != &token::PathSep)
3335 };
3336 let is_isolated_pin_const_self = |this: &Self, n| {
3338 this.look_ahead(n, |token| token.is_ident_named(sym::pin))
3339 && this.is_keyword_ahead(n + 1, &[kw::Const])
3340 && is_isolated_self(this, n + 2)
3341 };
3342 let is_isolated_mut_self =
3344 |this: &Self, n| this.is_keyword_ahead(n, &[kw::Mut]) && is_isolated_self(this, n + 1);
3345 let is_isolated_pin_mut_self = |this: &Self, n| {
3347 this.look_ahead(n, |token| token.is_ident_named(sym::pin))
3348 && is_isolated_mut_self(this, n + 1)
3349 };
3350 let parse_self_possibly_typed = |this: &mut Self, m| {
3352 let eself_ident = expect_self_ident(this);
3353 let eself_hi = this.prev_token.span;
3354 let eself = if this.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Colon,
token_type: crate::parser::token_type::TokenType::Colon,
}exp!(Colon)) {
3355 SelfKind::Explicit(this.parse_ty()?, m)
3356 } else {
3357 SelfKind::Value(m)
3358 };
3359 Ok((eself, eself_ident, eself_hi))
3360 };
3361 let expect_self_ident_not_typed =
3362 |this: &mut Self, modifier: &SelfKind, modifier_span: Span| {
3363 let eself_ident = expect_self_ident(this);
3364
3365 if this.may_recover() && this.eat_noexpect(&token::Colon) {
3367 let snap = this.create_snapshot_for_diagnostic();
3368 match this.parse_ty() {
3369 Ok(ty) => {
3370 this.dcx().emit_err(errors::IncorrectTypeOnSelf {
3371 span: ty.span,
3372 move_self_modifier: errors::MoveSelfModifier {
3373 removal_span: modifier_span,
3374 insertion_span: ty.span.shrink_to_lo(),
3375 modifier: modifier.to_ref_suggestion(),
3376 },
3377 });
3378 }
3379 Err(diag) => {
3380 diag.cancel();
3381 this.restore_snapshot(snap);
3382 }
3383 }
3384 }
3385 eself_ident
3386 };
3387 let recover_self_ptr = |this: &mut Self| {
3389 this.dcx().emit_err(errors::SelfArgumentPointer { span: this.token.span });
3390
3391 Ok((SelfKind::Value(Mutability::Not), expect_self_ident(this), this.prev_token.span))
3392 };
3393
3394 let eself_lo = self.token.span;
3398 let (eself, eself_ident, eself_hi) = match self.token.uninterpolate().kind {
3399 token::And => {
3400 let has_lifetime = is_lifetime(self, 1);
3401 let skip_lifetime_count = has_lifetime as usize;
3402 let eself = if is_isolated_self(self, skip_lifetime_count + 1) {
3403 self.bump(); let lifetime = has_lifetime.then(|| self.expect_lifetime());
3406 SelfKind::Region(lifetime, Mutability::Not)
3407 } else if is_isolated_mut_self(self, skip_lifetime_count + 1) {
3408 self.bump(); let lifetime = has_lifetime.then(|| self.expect_lifetime());
3411 self.bump(); SelfKind::Region(lifetime, Mutability::Mut)
3413 } else if is_isolated_pin_const_self(self, skip_lifetime_count + 1) {
3414 self.bump(); let lifetime = has_lifetime.then(|| self.expect_lifetime());
3417 self.psess.gated_spans.gate(sym::pin_ergonomics, self.token.span);
3418 self.bump(); self.bump(); SelfKind::Pinned(lifetime, Mutability::Not)
3421 } else if is_isolated_pin_mut_self(self, skip_lifetime_count + 1) {
3422 self.bump(); let lifetime = has_lifetime.then(|| self.expect_lifetime());
3425 self.psess.gated_spans.gate(sym::pin_ergonomics, self.token.span);
3426 self.bump(); self.bump(); SelfKind::Pinned(lifetime, Mutability::Mut)
3429 } else {
3430 return Ok(None);
3432 };
3433 let hi = self.token.span;
3434 let self_ident = expect_self_ident_not_typed(self, &eself, eself_lo.until(hi));
3435 (eself, self_ident, hi)
3436 }
3437 token::Star if is_isolated_self(self, 1) => {
3439 self.bump();
3440 recover_self_ptr(self)?
3441 }
3442 token::Star
3444 if self.look_ahead(1, |t| t.is_mutability()) && is_isolated_self(self, 2) =>
3445 {
3446 self.bump();
3447 self.bump();
3448 recover_self_ptr(self)?
3449 }
3450 token::Ident(..) if is_isolated_self(self, 0) => {
3452 parse_self_possibly_typed(self, Mutability::Not)?
3453 }
3454 token::Ident(..) if is_isolated_mut_self(self, 0) => {
3456 self.bump();
3457 parse_self_possibly_typed(self, Mutability::Mut)?
3458 }
3459 _ => return Ok(None),
3460 };
3461
3462 let eself = source_map::respan(eself_lo.to(eself_hi), eself);
3463 Ok(Some(Param::from_self(AttrVec::default(), eself, eself_ident)))
3464 }
3465
3466 fn is_named_param(&self) -> bool {
3467 let offset = match &self.token.kind {
3468 token::OpenInvisible(origin) => match origin {
3469 InvisibleOrigin::MetaVar(MetaVarKind::Pat(_)) => {
3470 return self.check_noexpect_past_close_delim(&token::Colon);
3471 }
3472 _ => 0,
3473 },
3474 token::And | token::AndAnd => 1,
3475 _ if self.token.is_keyword(kw::Mut) => 1,
3476 _ => 0,
3477 };
3478
3479 self.look_ahead(offset, |t| t.is_ident())
3480 && self.look_ahead(offset + 1, |t| t == &token::Colon)
3481 }
3482
3483 fn recover_self_param(&mut self) -> bool {
3484 #[allow(non_exhaustive_omitted_patterns)] match self.parse_outer_attributes().and_then(|_|
self.parse_self_param()).map_err(|e| e.cancel()) {
Ok(Some(_)) => true,
_ => false,
}matches!(
3485 self.parse_outer_attributes()
3486 .and_then(|_| self.parse_self_param())
3487 .map_err(|e| e.cancel()),
3488 Ok(Some(_))
3489 )
3490 }
3491}
3492
3493enum IsMacroRulesItem {
3494 Yes { has_bang: bool },
3495 No,
3496}
3497
3498#[derive(#[automatically_derived]
impl ::core::marker::Copy for FrontMatterParsingMode { }Copy, #[automatically_derived]
impl ::core::clone::Clone for FrontMatterParsingMode {
#[inline]
fn clone(&self) -> FrontMatterParsingMode { *self }
}Clone, #[automatically_derived]
impl ::core::cmp::PartialEq for FrontMatterParsingMode {
#[inline]
fn eq(&self, other: &FrontMatterParsingMode) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr
}
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for FrontMatterParsingMode {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_receiver_is_total_eq(&self) {}
}Eq)]
3499pub(super) enum FrontMatterParsingMode {
3500 Function,
3502 FunctionPtrType,
3505}