]> git.lizzy.rs Git - rust.git/blob - src/librustc_parse/parser/ty.rs
ast: Keep `extern` qualifiers in functions more precisely
[rust.git] / src / librustc_parse / parser / ty.rs
1 use super::{Parser, PathStyle, PrevTokenKind, TokenType};
2 use super::item::ParamCfg;
3
4 use crate::{maybe_whole, maybe_recover_from_interpolated_ty_qpath};
5
6 use syntax::ptr::P;
7 use syntax::ast::{self, Ty, TyKind, MutTy, BareFnTy, FunctionRetTy, GenericParam, Lifetime, Ident};
8 use syntax::ast::{TraitBoundModifier, TraitObjectSyntax, GenericBound, GenericBounds, PolyTraitRef};
9 use syntax::ast::{Mutability, AnonConst, Mac};
10 use syntax::token::{self, Token};
11 use syntax::source_map::Span;
12 use syntax::struct_span_fatal;
13 use syntax_pos::symbol::kw;
14
15 use errors::{PResult, Applicability, pluralize};
16
17 use rustc_error_codes::*;
18
19 /// Returns `true` if `IDENT t` can start a type -- `IDENT::a::b`, `IDENT<u8, u8>`,
20 /// `IDENT<<u8 as Trait>::AssocTy>`.
21 ///
22 /// Types can also be of the form `IDENT(u8, u8) -> u8`, however this assumes
23 /// that `IDENT` is not the ident of a fn trait.
24 fn can_continue_type_after_non_fn_ident(t: &Token) -> bool {
25     t == &token::ModSep || t == &token::Lt ||
26     t == &token::BinOp(token::Shl)
27 }
28
29 impl<'a> Parser<'a> {
30     /// Parses a type.
31     pub fn parse_ty(&mut self) -> PResult<'a, P<Ty>> {
32         self.parse_ty_common(true, true, false)
33     }
34
35     /// Parses a type in restricted contexts where `+` is not permitted.
36     ///
37     /// Example 1: `&'a TYPE`
38     ///     `+` is prohibited to maintain operator priority (P(+) < P(&)).
39     /// Example 2: `value1 as TYPE + value2`
40     ///     `+` is prohibited to avoid interactions with expression grammar.
41     pub(super) fn parse_ty_no_plus(&mut self) -> PResult<'a, P<Ty>> {
42         self.parse_ty_common(false, true, false)
43     }
44
45     /// Parses an optional return type `[ -> TY ]` in a function declaration.
46     pub(super) fn parse_ret_ty(&mut self, allow_plus: bool) -> PResult<'a, FunctionRetTy> {
47         if self.eat(&token::RArrow) {
48             Ok(FunctionRetTy::Ty(self.parse_ty_common(allow_plus, true, false)?))
49         } else {
50             Ok(FunctionRetTy::Default(self.token.span.shrink_to_lo()))
51         }
52     }
53
54     pub(super) fn parse_ty_common(&mut self, allow_plus: bool, allow_qpath_recovery: bool,
55                        allow_c_variadic: bool) -> PResult<'a, P<Ty>> {
56         maybe_recover_from_interpolated_ty_qpath!(self, allow_qpath_recovery);
57         maybe_whole!(self, NtTy, |x| x);
58
59         let lo = self.token.span;
60         let mut impl_dyn_multi = false;
61         let kind = if self.eat(&token::OpenDelim(token::Paren)) {
62             // `(TYPE)` is a parenthesized type.
63             // `(TYPE,)` is a tuple with a single field of type TYPE.
64             let mut ts = vec![];
65             let mut last_comma = false;
66             while self.token != token::CloseDelim(token::Paren) {
67                 ts.push(self.parse_ty()?);
68                 if self.eat(&token::Comma) {
69                     last_comma = true;
70                 } else {
71                     last_comma = false;
72                     break;
73                 }
74             }
75             let trailing_plus = self.prev_token_kind == PrevTokenKind::Plus;
76             self.expect(&token::CloseDelim(token::Paren))?;
77
78             if ts.len() == 1 && !last_comma {
79                 let ty = ts.into_iter().nth(0).unwrap().into_inner();
80                 let maybe_bounds = allow_plus && self.token.is_like_plus();
81                 match ty.kind {
82                     // `(TY_BOUND_NOPAREN) + BOUND + ...`.
83                     TyKind::Path(None, ref path) if maybe_bounds => {
84                         self.parse_remaining_bounds(Vec::new(), path.clone(), lo, true)?
85                     }
86                     TyKind::TraitObject(ref bounds, TraitObjectSyntax::None)
87                             if maybe_bounds && bounds.len() == 1 && !trailing_plus => {
88                         let path = match bounds[0] {
89                             GenericBound::Trait(ref pt, ..) => pt.trait_ref.path.clone(),
90                             GenericBound::Outlives(..) => self.bug("unexpected lifetime bound"),
91                         };
92                         self.parse_remaining_bounds(Vec::new(), path, lo, true)?
93                     }
94                     // `(TYPE)`
95                     _ => TyKind::Paren(P(ty))
96                 }
97             } else {
98                 TyKind::Tup(ts)
99             }
100         } else if self.eat(&token::Not) {
101             // Never type `!`
102             TyKind::Never
103         } else if self.eat(&token::BinOp(token::Star)) {
104             // Raw pointer
105             TyKind::Ptr(self.parse_ptr()?)
106         } else if self.eat(&token::OpenDelim(token::Bracket)) {
107             // Array or slice
108             let t = self.parse_ty()?;
109             // Parse optional `; EXPR` in `[TYPE; EXPR]`
110             let t = match self.maybe_parse_fixed_length_of_vec()? {
111                 None => TyKind::Slice(t),
112                 Some(length) => TyKind::Array(t, AnonConst {
113                     id: ast::DUMMY_NODE_ID,
114                     value: length,
115                 }),
116             };
117             self.expect(&token::CloseDelim(token::Bracket))?;
118             t
119         } else if self.check(&token::BinOp(token::And)) || self.check(&token::AndAnd) {
120             // Reference
121             self.expect_and()?;
122             self.parse_borrowed_pointee()?
123         } else if self.eat_keyword_noexpect(kw::Typeof) {
124             // `typeof(EXPR)`
125             // In order to not be ambiguous, the type must be surrounded by parens.
126             self.expect(&token::OpenDelim(token::Paren))?;
127             let e = AnonConst {
128                 id: ast::DUMMY_NODE_ID,
129                 value: self.parse_expr()?,
130             };
131             self.expect(&token::CloseDelim(token::Paren))?;
132             TyKind::Typeof(e)
133         } else if self.eat_keyword(kw::Underscore) {
134             // A type to be inferred `_`
135             TyKind::Infer
136         } else if self.token_is_bare_fn_keyword() {
137             // Function pointer type
138             self.parse_ty_bare_fn(Vec::new())?
139         } else if self.check_keyword(kw::For) {
140             // Function pointer type or bound list (trait object type) starting with a poly-trait.
141             //   `for<'lt> [unsafe] [extern "ABI"] fn (&'lt S) -> T`
142             //   `for<'lt> Trait1<'lt> + Trait2 + 'a`
143             let lo = self.token.span;
144             let lifetime_defs = self.parse_late_bound_lifetime_defs()?;
145             if self.token_is_bare_fn_keyword() {
146                 self.parse_ty_bare_fn(lifetime_defs)?
147             } else {
148                 let path = self.parse_path(PathStyle::Type)?;
149                 let parse_plus = allow_plus && self.check_plus();
150                 self.parse_remaining_bounds(lifetime_defs, path, lo, parse_plus)?
151             }
152         } else if self.eat_keyword(kw::Impl) {
153             // Always parse bounds greedily for better error recovery.
154             let bounds = self.parse_generic_bounds(None)?;
155             impl_dyn_multi = bounds.len() > 1 || self.prev_token_kind == PrevTokenKind::Plus;
156             TyKind::ImplTrait(ast::DUMMY_NODE_ID, bounds)
157         } else if self.check_keyword(kw::Dyn) &&
158                   (self.token.span.rust_2018() ||
159                    self.look_ahead(1, |t| t.can_begin_bound() &&
160                                           !can_continue_type_after_non_fn_ident(t))) {
161             self.bump(); // `dyn`
162             // Always parse bounds greedily for better error recovery.
163             let bounds = self.parse_generic_bounds(None)?;
164             impl_dyn_multi = bounds.len() > 1 || self.prev_token_kind == PrevTokenKind::Plus;
165             TyKind::TraitObject(bounds, TraitObjectSyntax::Dyn)
166         } else if self.check(&token::Question) ||
167                   self.check_lifetime() && self.look_ahead(1, |t| t.is_like_plus()) {
168             // Bound list (trait object type)
169             TyKind::TraitObject(self.parse_generic_bounds_common(allow_plus, None)?,
170                                 TraitObjectSyntax::None)
171         } else if self.eat_lt() {
172             // Qualified path
173             let (qself, path) = self.parse_qpath(PathStyle::Type)?;
174             TyKind::Path(Some(qself), path)
175         } else if self.token.is_path_start() {
176             // Simple path
177             let path = self.parse_path(PathStyle::Type)?;
178             if self.eat(&token::Not) {
179                 // Macro invocation in type position
180                 let (delim, tts) = self.expect_delimited_token_tree()?;
181                 let mac = Mac {
182                     path,
183                     tts,
184                     delim,
185                     span: lo.to(self.prev_span),
186                     prior_type_ascription: self.last_type_ascription,
187                 };
188                 TyKind::Mac(mac)
189             } else {
190                 // Just a type path or bound list (trait object type) starting with a trait.
191                 //   `Type`
192                 //   `Trait1 + Trait2 + 'a`
193                 if allow_plus && self.check_plus() {
194                     self.parse_remaining_bounds(Vec::new(), path, lo, true)?
195                 } else {
196                     TyKind::Path(None, path)
197                 }
198             }
199         } else if self.check(&token::DotDotDot) {
200             if allow_c_variadic {
201                 self.eat(&token::DotDotDot);
202                 TyKind::CVarArgs
203             } else {
204                 return Err(struct_span_fatal!(
205                     self.sess.span_diagnostic,
206                     self.token.span,
207                     E0743,
208                     "only foreign functions are allowed to be C-variadic",
209                 ));
210             }
211         } else {
212             let msg = format!("expected type, found {}", self.this_token_descr());
213             let mut err = self.fatal(&msg);
214             err.span_label(self.token.span, "expected type");
215             self.maybe_annotate_with_ascription(&mut err, true);
216             return Err(err);
217         };
218
219         let span = lo.to(self.prev_span);
220         let ty = self.mk_ty(span, kind);
221
222         // Try to recover from use of `+` with incorrect priority.
223         self.maybe_report_ambiguous_plus(allow_plus, impl_dyn_multi, &ty);
224         self.maybe_recover_from_bad_type_plus(allow_plus, &ty)?;
225         self.maybe_recover_from_bad_qpath(ty, allow_qpath_recovery)
226     }
227
228     fn parse_remaining_bounds(&mut self, generic_params: Vec<GenericParam>, path: ast::Path,
229                               lo: Span, parse_plus: bool) -> PResult<'a, TyKind> {
230         let poly_trait_ref = PolyTraitRef::new(generic_params, path, lo.to(self.prev_span));
231         let mut bounds = vec![GenericBound::Trait(poly_trait_ref, TraitBoundModifier::None)];
232         if parse_plus {
233             self.eat_plus(); // `+`, or `+=` gets split and `+` is discarded
234             bounds.append(&mut self.parse_generic_bounds(Some(self.prev_span))?);
235         }
236         Ok(TyKind::TraitObject(bounds, TraitObjectSyntax::None))
237     }
238
239     fn parse_ptr(&mut self) -> PResult<'a, MutTy> {
240         let mutbl = self.parse_const_or_mut().unwrap_or_else(|| {
241             let span = self.prev_span;
242             let msg = "expected mut or const in raw pointer type";
243             self.struct_span_err(span, msg)
244                 .span_label(span, msg)
245                 .help("use `*mut T` or `*const T` as appropriate")
246                 .emit();
247             Mutability::Immutable
248         });
249         let t = self.parse_ty_no_plus()?;
250         Ok(MutTy { ty: t, mutbl })
251     }
252
253     fn maybe_parse_fixed_length_of_vec(&mut self) -> PResult<'a, Option<P<ast::Expr>>> {
254         if self.eat(&token::Semi) {
255             Ok(Some(self.parse_expr()?))
256         } else {
257             Ok(None)
258         }
259     }
260
261     fn parse_borrowed_pointee(&mut self) -> PResult<'a, TyKind> {
262         let opt_lifetime = if self.check_lifetime() { Some(self.expect_lifetime()) } else { None };
263         let mutbl = self.parse_mutability();
264         let ty = self.parse_ty_no_plus()?;
265         return Ok(TyKind::Rptr(opt_lifetime, MutTy { ty, mutbl }));
266     }
267
268     /// Is the current token one of the keywords that signals a bare function type?
269     fn token_is_bare_fn_keyword(&mut self) -> bool {
270         self.check_keyword(kw::Fn) ||
271             self.check_keyword(kw::Unsafe) ||
272             self.check_keyword(kw::Extern)
273     }
274
275     /// Parses a `TyKind::BareFn` type.
276     fn parse_ty_bare_fn(&mut self, generic_params: Vec<GenericParam>) -> PResult<'a, TyKind> {
277         /*
278
279         [unsafe] [extern "ABI"] fn (S) -> T
280          ^~~~^           ^~~~^     ^~^    ^
281            |               |        |     |
282            |               |        |   Return type
283            |               |      Argument types
284            |               |
285            |              ABI
286         Function Style
287         */
288
289         let unsafety = self.parse_unsafety();
290         let ext = self.parse_extern()?;
291         self.expect_keyword(kw::Fn)?;
292         let cfg = ParamCfg {
293             is_self_allowed: false,
294             allow_c_variadic: true,
295             is_name_required: |_| false,
296         };
297         let decl = self.parse_fn_decl(cfg, false)?;
298         Ok(TyKind::BareFn(P(BareFnTy {
299             ext,
300             unsafety,
301             generic_params,
302             decl,
303         })))
304     }
305
306     pub(super) fn parse_generic_bounds(&mut self,
307                                   colon_span: Option<Span>) -> PResult<'a, GenericBounds> {
308         self.parse_generic_bounds_common(true, colon_span)
309     }
310
311     /// Parses bounds of a type parameter `BOUND + BOUND + ...`, possibly with trailing `+`.
312     ///
313     /// ```
314     /// BOUND = TY_BOUND | LT_BOUND
315     /// LT_BOUND = LIFETIME (e.g., `'a`)
316     /// TY_BOUND = TY_BOUND_NOPAREN | (TY_BOUND_NOPAREN)
317     /// TY_BOUND_NOPAREN = [?] [for<LT_PARAM_DEFS>] SIMPLE_PATH (e.g., `?for<'a: 'b> m::Trait<'a>`)
318     /// ```
319     fn parse_generic_bounds_common(&mut self,
320                                    allow_plus: bool,
321                                    colon_span: Option<Span>) -> PResult<'a, GenericBounds> {
322         let mut bounds = Vec::new();
323         let mut negative_bounds = Vec::new();
324         let mut last_plus_span = None;
325         let mut was_negative = false;
326         loop {
327             // This needs to be synchronized with `TokenKind::can_begin_bound`.
328             let is_bound_start = self.check_path() || self.check_lifetime() ||
329                                  self.check(&token::Not) || // used for error reporting only
330                                  self.check(&token::Question) ||
331                                  self.check_keyword(kw::For) ||
332                                  self.check(&token::OpenDelim(token::Paren));
333             if is_bound_start {
334                 let lo = self.token.span;
335                 let has_parens = self.eat(&token::OpenDelim(token::Paren));
336                 let inner_lo = self.token.span;
337                 let is_negative = self.eat(&token::Not);
338                 let question = if self.eat(&token::Question) { Some(self.prev_span) } else { None };
339                 if self.token.is_lifetime() {
340                     if let Some(question_span) = question {
341                         self.span_err(question_span,
342                                       "`?` may only modify trait bounds, not lifetime bounds");
343                     }
344                     bounds.push(GenericBound::Outlives(self.expect_lifetime()));
345                     if has_parens {
346                         let inner_span = inner_lo.to(self.prev_span);
347                         self.expect(&token::CloseDelim(token::Paren))?;
348                         let mut err = self.struct_span_err(
349                             lo.to(self.prev_span),
350                             "parenthesized lifetime bounds are not supported"
351                         );
352                         if let Ok(snippet) = self.span_to_snippet(inner_span) {
353                             err.span_suggestion_short(
354                                 lo.to(self.prev_span),
355                                 "remove the parentheses",
356                                 snippet.to_owned(),
357                                 Applicability::MachineApplicable
358                             );
359                         }
360                         err.emit();
361                     }
362                 } else {
363                     let lifetime_defs = self.parse_late_bound_lifetime_defs()?;
364                     let path = self.parse_path(PathStyle::Type)?;
365                     if has_parens {
366                         self.expect(&token::CloseDelim(token::Paren))?;
367                     }
368                     let poly_span = lo.to(self.prev_span);
369                     if is_negative {
370                         was_negative = true;
371                         if let Some(sp) = last_plus_span.or(colon_span) {
372                             negative_bounds.push(sp.to(poly_span));
373                         }
374                     } else {
375                         let poly_trait = PolyTraitRef::new(lifetime_defs, path, poly_span);
376                         let modifier = if question.is_some() {
377                             TraitBoundModifier::Maybe
378                         } else {
379                             TraitBoundModifier::None
380                         };
381                         bounds.push(GenericBound::Trait(poly_trait, modifier));
382                     }
383                 }
384             } else {
385                 break
386             }
387
388             if !allow_plus || !self.eat_plus() {
389                 break
390             } else {
391                 last_plus_span = Some(self.prev_span);
392             }
393         }
394
395         if !negative_bounds.is_empty() || was_negative {
396             let negative_bounds_len = negative_bounds.len();
397             let last_span = negative_bounds.last().map(|sp| *sp);
398             let mut err = self.struct_span_err(
399                 negative_bounds,
400                 "negative trait bounds are not supported",
401             );
402             if let Some(sp) = last_span {
403                 err.span_label(sp, "negative trait bounds are not supported");
404             }
405             if let Some(bound_list) = colon_span {
406                 let bound_list = bound_list.to(self.prev_span);
407                 let mut new_bound_list = String::new();
408                 if !bounds.is_empty() {
409                     let mut snippets = bounds.iter().map(|bound| bound.span())
410                         .map(|span| self.span_to_snippet(span));
411                     while let Some(Ok(snippet)) = snippets.next() {
412                         new_bound_list.push_str(" + ");
413                         new_bound_list.push_str(&snippet);
414                     }
415                     new_bound_list = new_bound_list.replacen(" +", ":", 1);
416                 }
417                 err.span_suggestion_hidden(
418                     bound_list,
419                     &format!("remove the trait bound{}", pluralize!(negative_bounds_len)),
420                     new_bound_list,
421                     Applicability::MachineApplicable,
422                 );
423             }
424             err.emit();
425         }
426
427         return Ok(bounds);
428     }
429
430     pub(super) fn parse_late_bound_lifetime_defs(&mut self) -> PResult<'a, Vec<GenericParam>> {
431         if self.eat_keyword(kw::For) {
432             self.expect_lt()?;
433             let params = self.parse_generic_params()?;
434             self.expect_gt()?;
435             // We rely on AST validation to rule out invalid cases: There must not be type
436             // parameters, and the lifetime parameters must not have bounds.
437             Ok(params)
438         } else {
439             Ok(Vec::new())
440         }
441     }
442
443     pub fn check_lifetime(&mut self) -> bool {
444         self.expected_tokens.push(TokenType::Lifetime);
445         self.token.is_lifetime()
446     }
447
448     /// Parses a single lifetime `'a` or panics.
449     pub fn expect_lifetime(&mut self) -> Lifetime {
450         if let Some(ident) = self.token.lifetime() {
451             let span = self.token.span;
452             self.bump();
453             Lifetime { ident: Ident::new(ident.name, span), id: ast::DUMMY_NODE_ID }
454         } else {
455             self.span_bug(self.token.span, "not a lifetime")
456         }
457     }
458
459     pub(super) fn mk_ty(&self, span: Span, kind: TyKind) -> P<Ty> {
460         P(Ty { kind, span, id: ast::DUMMY_NODE_ID })
461     }
462 }