diff options
| author | bors <bors@rust-lang.org> | 2019-04-23 19:52:05 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2019-04-23 19:52:05 +0000 |
| commit | e938c2b9aae7e0c37c382e4e22bdc360e9a4f0b6 (patch) | |
| tree | 4c16f95720b55ef5c1e387e81cae2c010a3427b3 /src/libsyntax | |
| parent | 4eff8526a789e0dfa8b97f7dec91b7b5c18e8544 (diff) | |
| parent | 0353fa5b90d74520052158d27578ed41fcc229e4 (diff) | |
| download | rust-e938c2b9aae7e0c37c382e4e22bdc360e9a4f0b6.tar.gz rust-e938c2b9aae7e0c37c382e4e22bdc360e9a4f0b6.zip | |
Auto merge of #60211 - Centril:rollup-akw4r85, r=Centril
Rollup of 6 pull requests Successful merges: - #59823 ([wg-async-await] Drop `async fn` arguments in async block ) - #59839 (Warn on unused results for operation methods on nums) - #60146 (Update fonts used by rustdoc) - #60169 (Warn when ignore-tidy-linelength is present, but no lines are too long) - #60177 (Promote rust comments to rustdoc) - #60191 (Add f16c target_feature) Failed merges: r? @ghost
Diffstat (limited to 'src/libsyntax')
| -rw-r--r-- | src/libsyntax/ast.rs | 51 | ||||
| -rw-r--r-- | src/libsyntax/ext/build.rs | 6 | ||||
| -rw-r--r-- | src/libsyntax/ext/placeholders.rs | 20 | ||||
| -rw-r--r-- | src/libsyntax/mut_visit.rs | 34 | ||||
| -rw-r--r-- | src/libsyntax/parse/parser.rs | 87 | ||||
| -rw-r--r-- | src/libsyntax/print/pprust.rs | 23 | ||||
| -rw-r--r-- | src/libsyntax/visit.rs | 3 |
7 files changed, 190 insertions, 34 deletions
diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index a5472c622e6..334fcfd74f3 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -888,6 +888,17 @@ pub struct Local { pub id: NodeId, pub span: Span, pub attrs: ThinVec<Attribute>, + /// Origin of this local variable. + pub source: LocalSource, +} + +#[derive(Clone, Copy, RustcEncodable, RustcDecodable, Debug)] +pub enum LocalSource { + /// Local was parsed from source. + Normal, + /// Within `ast::IsAsync::Async`, a local is generated that will contain the moved arguments + /// of an `async fn`. + AsyncFn, } /// An arm of a 'match'. @@ -1725,6 +1736,16 @@ pub struct Arg { pub ty: P<Ty>, pub pat: P<Pat>, pub id: NodeId, + pub source: ArgSource, +} + +/// The source of an argument in a function header. +#[derive(Clone, RustcEncodable, RustcDecodable, Debug)] +pub enum ArgSource { + /// Argument as written by the user. + Normal, + /// Argument from `async fn` lowering, contains the original binding pattern. + AsyncFn(P<Pat>), } /// Alternative representation for `Arg`s describing `self` parameter of methods. @@ -1784,6 +1805,7 @@ impl Arg { }), ty, id: DUMMY_NODE_ID, + source: ArgSource::Normal, }; match eself.node { SelfKind::Explicit(ty, mutbl) => arg(mutbl, ty), @@ -1838,18 +1860,35 @@ pub enum Unsafety { Normal, } -#[derive(Copy, Clone, RustcEncodable, RustcDecodable, Debug)] +#[derive(Clone, RustcEncodable, RustcDecodable, Debug)] +pub struct AsyncArgument { + /// `__arg0` + pub ident: Ident, + /// `__arg0: <ty>` argument to replace existing function argument `<pat>: <ty>`. + pub arg: Arg, + /// `let <pat>: <ty> = __arg0;` statement to be inserted at the start of the block. + pub stmt: Stmt, +} + +#[derive(Clone, RustcEncodable, RustcDecodable, Debug)] pub enum IsAsync { Async { closure_id: NodeId, return_impl_trait_id: NodeId, + /// This field stores the arguments and statements that are used in HIR lowering to + /// ensure that `async fn` arguments are dropped at the correct time. + /// + /// The argument and statements here are generated at parse time as they are required in + /// both the hir lowering, def collection and name resolution and this stops them needing + /// to be created in each place. + arguments: Vec<AsyncArgument>, }, NotAsync, } impl IsAsync { - pub fn is_async(self) -> bool { - if let IsAsync::Async { .. } = self { + pub fn is_async(&self) -> bool { + if let IsAsync::Async { .. } = *self { true } else { false @@ -1857,12 +1896,12 @@ impl IsAsync { } /// In ths case this is an `async` return, the `NodeId` for the generated `impl Trait` item. - pub fn opt_return_id(self) -> Option<NodeId> { + pub fn opt_return_id(&self) -> Option<NodeId> { match self { IsAsync::Async { return_impl_trait_id, .. - } => Some(return_impl_trait_id), + } => Some(*return_impl_trait_id), IsAsync::NotAsync => None, } } @@ -2202,7 +2241,7 @@ impl Item { /// /// All the information between the visibility and the name of the function is /// included in this struct (e.g., `async unsafe fn` or `const extern "C" fn`). -#[derive(Clone, Copy, RustcEncodable, RustcDecodable, Debug)] +#[derive(Clone, RustcEncodable, RustcDecodable, Debug)] pub struct FnHeader { pub unsafety: Unsafety, pub asyncness: Spanned<IsAsync>, diff --git a/src/libsyntax/ext/build.rs b/src/libsyntax/ext/build.rs index 614967bdeb4..40dd187ed28 100644 --- a/src/libsyntax/ext/build.rs +++ b/src/libsyntax/ext/build.rs @@ -526,6 +526,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> { id: ast::DUMMY_NODE_ID, span: sp, attrs: ThinVec::new(), + source: ast::LocalSource::Normal, }); ast::Stmt { id: ast::DUMMY_NODE_ID, @@ -554,6 +555,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> { id: ast::DUMMY_NODE_ID, span: sp, attrs: ThinVec::new(), + source: ast::LocalSource::Normal, }); ast::Stmt { id: ast::DUMMY_NODE_ID, @@ -571,6 +573,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> { id: ast::DUMMY_NODE_ID, span, attrs: ThinVec::new(), + source: ast::LocalSource::Normal, }); ast::Stmt { id: ast::DUMMY_NODE_ID, @@ -976,7 +979,8 @@ impl<'a> AstBuilder for ExtCtxt<'a> { ast::Arg { ty, pat: arg_pat, - id: ast::DUMMY_NODE_ID + id: ast::DUMMY_NODE_ID, + source: ast::ArgSource::Normal, } } diff --git a/src/libsyntax/ext/placeholders.rs b/src/libsyntax/ext/placeholders.rs index 3e60dd81a3b..68cd3c28676 100644 --- a/src/libsyntax/ext/placeholders.rs +++ b/src/libsyntax/ext/placeholders.rs @@ -102,6 +102,13 @@ impl<'a, 'b> PlaceholderExpander<'a, 'b> { fn remove(&mut self, id: ast::NodeId) -> AstFragment { self.expanded_fragments.remove(&id).unwrap() } + + fn next_id(&mut self, id: &mut ast::NodeId) { + if self.monotonic { + assert_eq!(*id, ast::DUMMY_NODE_ID); + *id = self.cx.resolver.next_node_id() + } + } } impl<'a, 'b> MutVisitor for PlaceholderExpander<'a, 'b> { @@ -183,9 +190,16 @@ impl<'a, 'b> MutVisitor for PlaceholderExpander<'a, 'b> { noop_visit_block(block, self); for stmt in block.stmts.iter_mut() { - if self.monotonic { - assert_eq!(stmt.id, ast::DUMMY_NODE_ID); - stmt.id = self.cx.resolver.next_node_id(); + self.next_id(&mut stmt.id); + } + } + + fn visit_asyncness(&mut self, a: &mut ast::IsAsync) { + noop_visit_asyncness(a, self); + + if let ast::IsAsync::Async { ref mut arguments, .. } = a { + for argument in arguments.iter_mut() { + self.next_id(&mut argument.stmt.id); } } } diff --git a/src/libsyntax/mut_visit.rs b/src/libsyntax/mut_visit.rs index 784d0049ac5..d3441a2039b 100644 --- a/src/libsyntax/mut_visit.rs +++ b/src/libsyntax/mut_visit.rs @@ -208,6 +208,10 @@ pub trait MutVisitor: Sized { noop_visit_local(l, self); } + fn visit_local_source(&mut self, l: &mut LocalSource) { + noop_visit_local_source(l, self); + } + fn visit_mac(&mut self, _mac: &mut Mac) { panic!("visit_mac disabled by default"); // N.B., see note about macros above. If you really want a visitor that @@ -231,6 +235,10 @@ pub trait MutVisitor: Sized { noop_visit_arg(a, self); } + fn visit_arg_source(&mut self, a: &mut ArgSource) { + noop_visit_arg_source(a, self); + } + fn visit_generics(&mut self, generics: &mut Generics) { noop_visit_generics(generics, self); } @@ -511,13 +519,17 @@ pub fn noop_visit_parenthesized_parameter_data<T: MutVisitor>(args: &mut Parenth } pub fn noop_visit_local<T: MutVisitor>(local: &mut P<Local>, vis: &mut T) { - let Local { id, pat, ty, init, span, attrs } = local.deref_mut(); + let Local { id, pat, ty, init, span, attrs, source } = local.deref_mut(); vis.visit_id(id); vis.visit_pat(pat); visit_opt(ty, |ty| vis.visit_ty(ty)); visit_opt(init, |init| vis.visit_expr(init)); vis.visit_span(span); visit_thin_attrs(attrs, vis); + vis.visit_local_source(source); +} + +pub fn noop_visit_local_source<T: MutVisitor>(_local_source: &mut LocalSource, _vis: &mut T) { } pub fn noop_visit_attribute<T: MutVisitor>(attr: &mut Attribute, vis: &mut T) { @@ -556,10 +568,18 @@ pub fn noop_visit_meta_item<T: MutVisitor>(mi: &mut MetaItem, vis: &mut T) { vis.visit_span(span); } -pub fn noop_visit_arg<T: MutVisitor>(Arg { id, pat, ty }: &mut Arg, vis: &mut T) { +pub fn noop_visit_arg<T: MutVisitor>(Arg { id, pat, ty, source }: &mut Arg, vis: &mut T) { vis.visit_id(id); vis.visit_pat(pat); vis.visit_ty(ty); + vis.visit_arg_source(source); +} + +pub fn noop_visit_arg_source<T: MutVisitor>(source: &mut ArgSource, vis: &mut T) { + match source { + ArgSource::Normal => {}, + ArgSource::AsyncFn(pat) => vis.visit_pat(pat), + } } pub fn noop_visit_tt<T: MutVisitor>(tt: &mut TokenTree, vis: &mut T) { @@ -671,9 +691,17 @@ pub fn noop_visit_interpolated<T: MutVisitor>(nt: &mut token::Nonterminal, vis: pub fn noop_visit_asyncness<T: MutVisitor>(asyncness: &mut IsAsync, vis: &mut T) { match asyncness { - IsAsync::Async { closure_id, return_impl_trait_id } => { + IsAsync::Async { closure_id, return_impl_trait_id, ref mut arguments } => { vis.visit_id(closure_id); vis.visit_id(return_impl_trait_id); + for AsyncArgument { ident, arg, stmt } in arguments.iter_mut() { + vis.visit_ident(ident); + vis.visit_arg(arg); + visit_clobber(stmt, |stmt| { + vis.flat_map_stmt(stmt) + .expect_one("expected visitor to produce exactly one item") + }); + } } IsAsync::NotAsync => {} } diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 824192f0739..53dab510ac3 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -1,7 +1,7 @@ -use crate::ast::{AngleBracketedArgs, ParenthesizedArgs, AttrStyle, BareFnTy}; +use crate::ast::{AngleBracketedArgs, AsyncArgument, ParenthesizedArgs, AttrStyle, BareFnTy}; use crate::ast::{GenericBound, TraitBoundModifier}; use crate::ast::Unsafety; -use crate::ast::{Mod, AnonConst, Arg, Arm, Guard, Attribute, BindingMode, TraitItemKind}; +use crate::ast::{Mod, AnonConst, Arg, ArgSource, Arm, Guard, Attribute, BindingMode, TraitItemKind}; use crate::ast::Block; use crate::ast::{BlockCheckMode, CaptureBy, Movability}; use crate::ast::{Constness, Crate}; @@ -14,7 +14,7 @@ use crate::ast::{GenericParam, GenericParamKind}; use crate::ast::GenericArg; use crate::ast::{Ident, ImplItem, IsAsync, IsAuto, Item, ItemKind}; use crate::ast::{Label, Lifetime, Lit, LitKind}; -use crate::ast::Local; +use crate::ast::{Local, LocalSource}; use crate::ast::MacStmtStyle; use crate::ast::{Mac, Mac_, MacDelimiter}; use crate::ast::{MutTy, Mutability}; @@ -550,7 +550,7 @@ fn dummy_arg(span: Span) -> Arg { span, id: ast::DUMMY_NODE_ID }; - Arg { ty: P(ty), pat: pat, id: ast::DUMMY_NODE_ID } + Arg { ty: P(ty), pat: pat, id: ast::DUMMY_NODE_ID, source: ast::ArgSource::Normal } } #[derive(Copy, Clone, Debug)] @@ -1517,6 +1517,7 @@ impl<'a> Parser<'a> { IsAsync::Async { closure_id: ast::DUMMY_NODE_ID, return_impl_trait_id: ast::DUMMY_NODE_ID, + arguments: Vec::new(), } } else { IsAsync::NotAsync @@ -1575,7 +1576,7 @@ impl<'a> Parser<'a> { // trait item macro. (keywords::Invalid.ident(), ast::TraitItemKind::Macro(mac), ast::Generics::default()) } else { - let (constness, unsafety, asyncness, abi) = self.parse_fn_front_matter()?; + let (constness, unsafety, mut asyncness, abi) = self.parse_fn_front_matter()?; let ident = self.parse_ident()?; let mut generics = self.parse_generics()?; @@ -1589,6 +1590,7 @@ impl<'a> Parser<'a> { p.parse_arg_general(p.span.rust_2018(), true, false) })?; generics.where_clause = self.parse_where_clause()?; + self.construct_async_arguments(&mut asyncness, &d); let sig = ast::MethodSig { header: FnHeader { @@ -2124,7 +2126,7 @@ impl<'a> Parser<'a> { } }; - Ok(Arg { ty, pat, id: ast::DUMMY_NODE_ID }) + Ok(Arg { ty, pat, id: ast::DUMMY_NODE_ID, source: ast::ArgSource::Normal }) } /// Parses a single function argument. @@ -2147,7 +2149,8 @@ impl<'a> Parser<'a> { Ok(Arg { ty: t, pat, - id: ast::DUMMY_NODE_ID + id: ast::DUMMY_NODE_ID, + source: ast::ArgSource::Normal, }) } @@ -5029,6 +5032,7 @@ impl<'a> Parser<'a> { id: ast::DUMMY_NODE_ID, span: lo.to(hi), attrs, + source: LocalSource::Normal, })) } @@ -6566,7 +6570,7 @@ impl<'a> Parser<'a> { /// Parses an item-position function declaration. fn parse_item_fn(&mut self, unsafety: Unsafety, - asyncness: Spanned<IsAsync>, + mut asyncness: Spanned<IsAsync>, constness: Spanned<Constness>, abi: Abi) -> PResult<'a, ItemInfo> { @@ -6575,6 +6579,7 @@ impl<'a> Parser<'a> { let decl = self.parse_fn_decl(allow_c_variadic)?; generics.where_clause = self.parse_where_clause()?; let (inner_attrs, body) = self.parse_inner_attrs_and_block()?; + self.construct_async_arguments(&mut asyncness, &decl); let header = FnHeader { unsafety, asyncness, constness, abi }; Ok((ident, ItemKind::Fn(decl, header, generics, body), Some(inner_attrs))) } @@ -6755,11 +6760,12 @@ impl<'a> Parser<'a> { Ok((keywords::Invalid.ident(), vec![], ast::Generics::default(), ast::ImplItemKind::Macro(mac))) } else { - let (constness, unsafety, asyncness, abi) = self.parse_fn_front_matter()?; + let (constness, unsafety, mut asyncness, abi) = self.parse_fn_front_matter()?; let ident = self.parse_ident()?; let mut generics = self.parse_generics()?; let decl = self.parse_fn_decl_with_self(|p| p.parse_arg())?; generics.where_clause = self.parse_where_clause()?; + self.construct_async_arguments(&mut asyncness, &decl); *at_end = true; let (inner_attrs, body) = self.parse_inner_attrs_and_block()?; let header = ast::FnHeader { abi, unsafety, constness, asyncness }; @@ -8181,6 +8187,7 @@ impl<'a> Parser<'a> { respan(async_span, IsAsync::Async { closure_id: ast::DUMMY_NODE_ID, return_impl_trait_id: ast::DUMMY_NODE_ID, + arguments: Vec::new(), }), respan(fn_span, Constness::NotConst), Abi::Rust)?; @@ -8826,6 +8833,68 @@ impl<'a> Parser<'a> { } } } + + /// When lowering a `async fn` to the HIR, we need to move all of the arguments of the function + /// into the generated closure so that they are dropped when the future is polled and not when + /// it is created. + /// + /// The arguments of the function are replaced in HIR lowering with the arguments created by + /// this function and the statements created here are inserted at the top of the closure body. + fn construct_async_arguments(&mut self, asyncness: &mut Spanned<IsAsync>, decl: &FnDecl) { + if let IsAsync::Async { ref mut arguments, .. } = asyncness.node { + for (index, input) in decl.inputs.iter().enumerate() { + let id = ast::DUMMY_NODE_ID; + let span = input.pat.span; + + // Construct a name for our temporary argument. + let name = format!("__arg{}", index); + let ident = Ident::from_str(&name); + + // Construct an argument representing `__argN: <ty>` to replace the argument of the + // async function. + let arg = Arg { + ty: input.ty.clone(), + id, + pat: P(Pat { + id, + node: PatKind::Ident( + BindingMode::ByValue(Mutability::Immutable), ident, None, + ), + span, + }), + source: ArgSource::AsyncFn(input.pat.clone()), + }; + + // Construct a `let <pat> = __argN;` statement to insert at the top of the + // async closure. + let local = P(Local { + pat: input.pat.clone(), + // We explicitly do not specify the type for this statement. When the user's + // argument type is `impl Trait` then this would require the + // `impl_trait_in_bindings` feature to also be present for that same type to + // be valid in this binding. At the time of writing (13 Mar 19), + // `impl_trait_in_bindings` is not stable. + ty: None, + init: Some(P(Expr { + id, + node: ExprKind::Path(None, ast::Path { + span, + segments: vec![PathSegment { ident, id, args: None }], + }), + span, + attrs: ThinVec::new(), + })), + id, + span, + attrs: ThinVec::new(), + source: LocalSource::AsyncFn, + }); + let stmt = Stmt { id, node: StmtKind::Local(local), span, }; + + arguments.push(AsyncArgument { ident, arg, stmt }); + } + } + } } pub fn emit_unclosed_delims(unclosed_delims: &mut Vec<UnmatchedBrace>, handler: &errors::Handler) { diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index d94e4762e67..7ce3951f13e 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -372,7 +372,7 @@ pub fn vis_to_string(v: &ast::Visibility) -> String { } pub fn fun_to_string(decl: &ast::FnDecl, - header: ast::FnHeader, + header: &ast::FnHeader, name: ast::Ident, generics: &ast::Generics) -> String { @@ -1133,7 +1133,7 @@ impl<'a> State<'a> { match item.node { ast::ForeignItemKind::Fn(ref decl, ref generics) => { self.head("")?; - self.print_fn(decl, ast::FnHeader::default(), + self.print_fn(decl, &ast::FnHeader::default(), Some(item.ident), generics, &item.vis)?; self.end()?; // end head-ibox @@ -1263,7 +1263,7 @@ impl<'a> State<'a> { self.s.word(";")?; self.end()?; // end the outer cbox } - ast::ItemKind::Fn(ref decl, header, ref param_names, ref body) => { + ast::ItemKind::Fn(ref decl, ref header, ref param_names, ref body) => { self.head("")?; self.print_fn( decl, @@ -1615,7 +1615,7 @@ impl<'a> State<'a> { vis: &ast::Visibility) -> io::Result<()> { self.print_fn(&m.decl, - m.header, + &m.header, Some(ident), &generics, vis) @@ -2213,7 +2213,7 @@ impl<'a> State<'a> { self.bclose_(expr.span, INDENT_UNIT)?; } ast::ExprKind::Closure( - capture_clause, asyncness, movability, ref decl, ref body, _) => { + capture_clause, ref asyncness, movability, ref decl, ref body, _) => { self.print_movability(movability)?; self.print_asyncness(asyncness)?; self.print_capture_clause(capture_clause)?; @@ -2798,7 +2798,7 @@ impl<'a> State<'a> { pub fn print_fn(&mut self, decl: &ast::FnDecl, - header: ast::FnHeader, + header: &ast::FnHeader, name: Option<ast::Ident>, generics: &ast::Generics, vis: &ast::Visibility) -> io::Result<()> { @@ -2853,8 +2853,7 @@ impl<'a> State<'a> { } } - pub fn print_asyncness(&mut self, asyncness: ast::IsAsync) - -> io::Result<()> { + pub fn print_asyncness(&mut self, asyncness: &ast::IsAsync) -> io::Result<()> { if asyncness.is_async() { self.word_nbsp("async")?; } @@ -3126,7 +3125,7 @@ impl<'a> State<'a> { span: syntax_pos::DUMMY_SP, }; self.print_fn(decl, - ast::FnHeader { unsafety, abi, ..ast::FnHeader::default() }, + &ast::FnHeader { unsafety, abi, ..ast::FnHeader::default() }, name, &generics, &source_map::dummy_spanned(ast::VisibilityKind::Inherited))?; @@ -3189,7 +3188,7 @@ impl<'a> State<'a> { } pub fn print_fn_header_info(&mut self, - header: ast::FnHeader, + header: &ast::FnHeader, vis: &ast::Visibility) -> io::Result<()> { self.s.word(visibility_qualified(vis, ""))?; @@ -3198,7 +3197,7 @@ impl<'a> State<'a> { ast::Constness::Const => self.word_nbsp("const")? } - self.print_asyncness(header.asyncness.node)?; + self.print_asyncness(&header.asyncness.node)?; self.print_unsafety(header.unsafety)?; if header.abi != Abi::Rust { @@ -3247,7 +3246,7 @@ mod tests { assert_eq!( fun_to_string( &decl, - ast::FnHeader { + &ast::FnHeader { unsafety: ast::Unsafety::Normal, constness: source_map::dummy_spanned(ast::Constness::NotConst), asyncness: source_map::dummy_spanned(ast::IsAsync::NotAsync), diff --git a/src/libsyntax/visit.rs b/src/libsyntax/visit.rs index fe74cbd6496..fc99d10b0b6 100644 --- a/src/libsyntax/visit.rs +++ b/src/libsyntax/visit.rs @@ -544,6 +544,9 @@ pub fn walk_fn_ret_ty<'a, V: Visitor<'a>>(visitor: &mut V, ret_ty: &'a FunctionR pub fn walk_fn_decl<'a, V: Visitor<'a>>(visitor: &mut V, function_declaration: &'a FnDecl) { for argument in &function_declaration.inputs { visitor.visit_pat(&argument.pat); + if let ArgSource::AsyncFn(pat) = &argument.source { + visitor.visit_pat(pat); + } visitor.visit_ty(&argument.ty) } visitor.visit_fn_ret_ty(&function_declaration.output) |
