Parse async fn header.

This is gated on edition 2018 & the `async_await` feature gate.

The parser will accept `async fn` and `async unsafe fn` as fn
items. Along the same lines as `const fn`, only `async unsafe fn`
is permitted, not `unsafe async fn`.The parser will not accept
`async` functions as trait methods.

To do a little code clean up, four fields of the function type
struct have been merged into the new `FnHeader` struct: constness,
asyncness, unsafety, and ABI.

Also, a small bug in HIR printing is fixed: it previously printed
`const unsafe fn` as `unsafe const fn`, which is grammatically
incorrect.
This commit is contained in:
Without Boats
2018-05-16 22:55:18 -07:00
committed by Taylor Cramer
parent 4b17d31f11
commit 18ff7d091a
33 changed files with 367 additions and 288 deletions

View File

@@ -17,7 +17,7 @@ pub use util::ThinVec;
pub use util::parser::ExprPrecedence;
use syntax_pos::{Span, DUMMY_SP};
use codemap::{respan, Spanned};
use codemap::{dummy_spanned, respan, Spanned};
use rustc_target::spec::abi::Abi;
use ext::hygiene::{Mark, SyntaxContext};
use print::pprust;
@@ -1325,9 +1325,7 @@ pub struct MutTy {
/// or in an implementation.
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
pub struct MethodSig {
pub unsafety: Unsafety,
pub constness: Spanned<Constness>,
pub abi: Abi,
pub header: FnHeader,
pub decl: P<FnDecl>,
}
@@ -1708,6 +1706,12 @@ pub enum Unsafety {
Normal,
}
#[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
pub enum IsAsync {
Async,
NotAsync,
}
#[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
pub enum Constness {
Const,
@@ -2009,6 +2013,29 @@ pub struct Item {
pub tokens: Option<TokenStream>,
}
/// A function header
///
/// All the information between the visibility & the name of the function is
/// included in this struct (e.g. `async unsafe fn` or `const extern "C" fn`)
#[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
pub struct FnHeader {
pub unsafety: Unsafety,
pub asyncness: IsAsync,
pub constness: Spanned<Constness>,
pub abi: Abi,
}
impl Default for FnHeader {
fn default() -> FnHeader {
FnHeader {
unsafety: Unsafety::Normal,
asyncness: IsAsync::NotAsync,
constness: dummy_spanned(Constness::NotConst),
abi: Abi::Rust,
}
}
}
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
pub enum ItemKind {
/// An `extern crate` item, with optional *original* crate name if the crate was renamed.
@@ -2030,7 +2057,7 @@ pub enum ItemKind {
/// A function declaration (`fn` or `pub fn`).
///
/// E.g. `fn foo(bar: usize) -> usize { .. }`
Fn(P<FnDecl>, Unsafety, Spanned<Constness>, Abi, Generics, P<Block>),
Fn(P<FnDecl>, FnHeader, Generics, P<Block>),
/// A module declaration (`mod` or `pub mod`).
///
/// E.g. `mod foo;` or `mod foo { .. }`