Skip to content

Commit c30f068

Browse files
committed
IsAsync -> enum Async { Yes { span: Span, .. }, No }
use new span for better diagnostics.
1 parent e839b2e commit c30f068

File tree

19 files changed

+96
-108
lines changed

19 files changed

+96
-108
lines changed

src/librustc_ast_lowering/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
106106
ref body,
107107
fn_decl_span,
108108
) => {
109-
if let IsAsync::Async { closure_id, .. } = asyncness {
109+
if let Async::Yes { closure_id, .. } = asyncness {
110110
self.lower_expr_async_closure(
111111
capture_clause,
112112
closure_id,

src/librustc_ast_lowering/item.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
299299
// `impl Future<Output = T>` here because lower_body
300300
// only cares about the input argument patterns in the function
301301
// declaration (decl), not the return types.
302-
let asyncness = header.asyncness.node;
302+
let asyncness = header.asyncness;
303303
let body_id =
304304
this.lower_maybe_async_body(span, &decl, asyncness, body.as_deref());
305305

@@ -836,19 +836,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
836836
}
837837
AssocItemKind::Fn(ref sig, ref body) => {
838838
self.current_item = Some(i.span);
839-
let body_id = self.lower_maybe_async_body(
840-
i.span,
841-
&sig.decl,
842-
sig.header.asyncness.node,
843-
body.as_deref(),
844-
);
839+
let asyncness = sig.header.asyncness;
840+
let body_id =
841+
self.lower_maybe_async_body(i.span, &sig.decl, asyncness, body.as_deref());
845842
let impl_trait_return_allow = !self.is_in_trait_impl;
846843
let (generics, sig) = self.lower_method_sig(
847844
&i.generics,
848845
sig,
849846
impl_item_def_id,
850847
impl_trait_return_allow,
851-
sig.header.asyncness.node.opt_return_id(),
848+
asyncness.opt_return_id(),
852849
);
853850

854851
(generics, hir::ImplItemKind::Method(sig, body_id))
@@ -1033,12 +1030,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
10331030
&mut self,
10341031
span: Span,
10351032
decl: &FnDecl,
1036-
asyncness: IsAsync,
1033+
asyncness: Async,
10371034
body: Option<&Block>,
10381035
) -> hir::BodyId {
10391036
let closure_id = match asyncness {
1040-
IsAsync::Async { closure_id, .. } => closure_id,
1041-
IsAsync::NotAsync => return self.lower_fn_body_block(span, decl, body),
1037+
Async::Yes { closure_id, .. } => closure_id,
1038+
Async::No => return self.lower_fn_body_block(span, decl, body),
10421039
};
10431040

10441041
self.lower_body(|this| {
@@ -1248,7 +1245,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
12481245
fn lower_fn_header(&mut self, h: FnHeader) -> hir::FnHeader {
12491246
hir::FnHeader {
12501247
unsafety: self.lower_unsafety(h.unsafety),
1251-
asyncness: self.lower_asyncness(h.asyncness.node),
1248+
asyncness: self.lower_asyncness(h.asyncness),
12521249
constness: self.lower_constness(h.constness),
12531250
abi: self.lower_extern(h.ext),
12541251
}
@@ -1276,10 +1273,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
12761273
.emit();
12771274
}
12781275

1279-
fn lower_asyncness(&mut self, a: IsAsync) -> hir::IsAsync {
1276+
fn lower_asyncness(&mut self, a: Async) -> hir::IsAsync {
12801277
match a {
1281-
IsAsync::Async { .. } => hir::IsAsync::Async,
1282-
IsAsync::NotAsync => hir::IsAsync::NotAsync,
1278+
Async::Yes { .. } => hir::IsAsync::Async,
1279+
Async::No => hir::IsAsync::NotAsync,
12831280
}
12841281
}
12851282

src/librustc_ast_passes/ast_validation.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -221,13 +221,13 @@ impl<'a> AstValidator<'a> {
221221
}
222222
}
223223

224-
fn check_trait_fn_not_async(&self, span: Span, asyncness: IsAsync) {
225-
if asyncness.is_async() {
226-
struct_span_err!(self.session, span, E0706, "trait fns cannot be declared `async`")
224+
fn check_trait_fn_not_async(&self, fn_span: Span, asyncness: Async) {
225+
if let Async::Yes { span, .. } = asyncness {
226+
struct_span_err!(self.session, fn_span, E0706, "trait fns cannot be declared `async`")
227+
.span_label(span, "`async` because of this")
227228
.note("`async` trait functions are not currently supported")
228229
.note(
229-
"consider using the `async-trait` crate: \
230-
https://crates.io/crates/async-trait",
230+
"consider using the `async-trait` crate: https://crates.io/crates/async-trait",
231231
)
232232
.emit();
233233
}
@@ -1144,7 +1144,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
11441144
self.invalid_visibility(&item.vis, None);
11451145
if let AssocItemKind::Fn(sig, _) = &item.kind {
11461146
self.check_trait_fn_not_const(sig.header.constness);
1147-
self.check_trait_fn_not_async(item.span, sig.header.asyncness.node);
1147+
self.check_trait_fn_not_async(item.span, sig.header.asyncness);
11481148
}
11491149
}
11501150

src/librustc_ast_pretty/pprust.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2449,7 +2449,7 @@ impl<'a> State<'a> {
24492449
}
24502450
}
24512451

2452-
crate fn print_asyncness(&mut self, asyncness: ast::IsAsync) {
2452+
crate fn print_asyncness(&mut self, asyncness: ast::Async) {
24532453
if asyncness.is_async() {
24542454
self.word_nbsp("async");
24552455
}
@@ -2734,7 +2734,7 @@ impl<'a> State<'a> {
27342734
self.s.word(visibility_qualified(vis, ""));
27352735

27362736
self.print_constness(header.constness);
2737-
self.print_asyncness(header.asyncness.node);
2737+
self.print_asyncness(header.asyncness);
27382738
self.print_unsafety(header.unsafety);
27392739

27402740
match header.ext {

src/librustc_builtin_macros/test.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,8 +381,10 @@ fn has_test_signature(cx: &ExtCtxt<'_>, i: &ast::Item) -> bool {
381381
.emit();
382382
return false;
383383
}
384-
if sig.header.asyncness.node.is_async() {
385-
sd.span_err(i.span, "async functions cannot be used for tests");
384+
if let ast::Async::Yes { span, .. } = sig.header.asyncness {
385+
sd.struct_span_err(i.span, "async functions cannot be used for tests")
386+
.span_label(span, "async because of this")
387+
.emit();
386388
return false;
387389
}
388390

src/librustc_expand/build.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ impl<'a> ExtCtxt<'a> {
507507
span,
508508
ast::ExprKind::Closure(
509509
ast::CaptureBy::Ref,
510-
ast::IsAsync::NotAsync,
510+
ast::Async::No,
511511
ast::Movability::Movable,
512512
fn_decl,
513513
body,
@@ -530,7 +530,7 @@ impl<'a> ExtCtxt<'a> {
530530
span,
531531
ast::ExprKind::Closure(
532532
ast::CaptureBy::Ref,
533-
ast::IsAsync::NotAsync,
533+
ast::Async::No,
534534
ast::Movability::Movable,
535535
fn_decl,
536536
body,

src/librustc_parse/parser/expr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use syntax::ast::{self, AttrStyle, AttrVec, CaptureBy, Field, Ident, Lit, DUMMY_
1313
use syntax::ast::{
1414
AnonConst, BinOp, BinOpKind, FnDecl, FunctionRetTy, Mac, Param, Ty, TyKind, UnOp,
1515
};
16-
use syntax::ast::{Arm, BlockCheckMode, Expr, ExprKind, IsAsync, Label, Movability, RangeLimits};
16+
use syntax::ast::{Arm, Async, BlockCheckMode, Expr, ExprKind, Label, Movability, RangeLimits};
1717
use syntax::ptr::P;
1818
use syntax::token::{self, Token, TokenKind};
1919
use syntax::util::classify;
@@ -1348,7 +1348,7 @@ impl<'a> Parser<'a> {
13481348
if self.eat_keyword(kw::Static) { Movability::Static } else { Movability::Movable };
13491349

13501350
let asyncness =
1351-
if self.token.span.rust_2018() { self.parse_asyncness() } else { IsAsync::NotAsync };
1351+
if self.token.span.rust_2018() { self.parse_asyncness() } else { Async::No };
13521352
if asyncness.is_async() {
13531353
// Feature-gate `async ||` closures.
13541354
self.sess.gated_spans.gate(sym::async_closure, self.prev_span);

src/librustc_parse/parser/item.rs

Lines changed: 9 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ use crate::maybe_whole;
66

77
use rustc_ast_pretty::pprust;
88
use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder, PResult, StashKey};
9-
use rustc_span::source_map::{self, respan, Span};
9+
use rustc_span::source_map::{self, Span};
1010
use rustc_span::symbol::{kw, sym, Symbol};
1111
use rustc_span::BytePos;
1212
use syntax::ast::{self, AttrKind, AttrStyle, AttrVec, Attribute, Ident, DUMMY_NODE_ID};
1313
use syntax::ast::{AssocItem, AssocItemKind, Item, ItemKind, UseTree, UseTreeKind};
14+
use syntax::ast::{Async, Const, Defaultness, Extern, IsAuto, PathSegment, StrLit, Unsafe};
1415
use syntax::ast::{BindingMode, Block, FnDecl, FnSig, Mac, MacArgs, MacDelimiter, Param, SelfKind};
15-
use syntax::ast::{Const, Defaultness, Extern, IsAsync, IsAuto, PathSegment, StrLit, Unsafe};
1616
use syntax::ast::{EnumDef, Generics, StructField, TraitRef, Ty, TyKind, Variant, VariantData};
1717
use syntax::ast::{FnHeader, ForeignItem, ForeignItemKind, Mutability, Visibility, VisibilityKind};
1818
use syntax::ptr::P;
@@ -105,10 +105,9 @@ impl<'a> Parser<'a> {
105105

106106
if self.eat_keyword(kw::Fn) {
107107
// EXTERN FUNCTION ITEM
108-
let fn_span = self.prev_span;
109108
let header = FnHeader {
110109
unsafety: Unsafe::No,
111-
asyncness: respan(fn_span, IsAsync::NotAsync),
110+
asyncness: Async::No,
112111
constness: Const::No,
113112
ext: Extern::from_abi(abi),
114113
};
@@ -140,12 +139,7 @@ impl<'a> Parser<'a> {
140139
let ext = self.parse_extern()?;
141140
self.expect_keyword(kw::Fn)?;
142141

143-
let header = FnHeader {
144-
unsafety,
145-
asyncness: respan(const_span, IsAsync::NotAsync),
146-
constness,
147-
ext,
148-
};
142+
let header = FnHeader { unsafety, asyncness: Async::No, constness, ext };
149143
return self.parse_item_fn(lo, vis, attrs, header);
150144
}
151145

@@ -172,16 +166,9 @@ impl<'a> Parser<'a> {
172166
let async_span = self.token.span;
173167
if self.is_keyword_ahead(1, &[kw::Fn]) || self.is_keyword_ahead(2, &[kw::Fn]) {
174168
// ASYNC FUNCTION ITEM
175-
self.bump(); // `async`
169+
let asyncness = self.parse_asyncness(); // `async`
176170
let unsafety = self.parse_unsafety(); // `unsafe`?
177171
self.expect_keyword(kw::Fn)?; // `fn`
178-
let asyncness = respan(
179-
async_span,
180-
IsAsync::Async {
181-
closure_id: DUMMY_NODE_ID,
182-
return_impl_trait_id: DUMMY_NODE_ID,
183-
},
184-
);
185172
self.ban_async_in_2015(async_span);
186173
let header =
187174
FnHeader { unsafety, asyncness, constness: Const::No, ext: Extern::None };
@@ -211,13 +198,7 @@ impl<'a> Parser<'a> {
211198
if self.check_keyword(kw::Fn) {
212199
// FUNCTION ITEM
213200
self.bump();
214-
let fn_span = self.prev_span;
215-
let header = FnHeader {
216-
unsafety: Unsafe::No,
217-
asyncness: respan(fn_span, IsAsync::NotAsync),
218-
constness: Const::No,
219-
ext: Extern::None,
220-
};
201+
let header = FnHeader::default();
221202
return self.parse_item_fn(lo, vis, attrs, header);
222203
}
223204

@@ -230,13 +211,7 @@ impl<'a> Parser<'a> {
230211
self.check(&token::OpenDelim(token::Brace));
231212
let ext = self.parse_extern()?;
232213
self.expect_keyword(kw::Fn)?;
233-
let fn_span = self.prev_span;
234-
let header = FnHeader {
235-
unsafety,
236-
asyncness: respan(fn_span, IsAsync::NotAsync),
237-
constness: Const::No,
238-
ext,
239-
};
214+
let header = FnHeader { unsafety, asyncness: Async::No, constness: Const::No, ext };
240215
return self.parse_item_fn(lo, vis, attrs, header);
241216
}
242217

@@ -1788,10 +1763,9 @@ impl<'a> Parser<'a> {
17881763
fn parse_fn_front_matter(&mut self) -> PResult<'a, FnHeader> {
17891764
let constness = self.parse_constness();
17901765
let asyncness = self.parse_asyncness();
1791-
if let IsAsync::Async { .. } = asyncness {
1792-
self.ban_async_in_2015(self.prev_span);
1766+
if let Async::Yes { span, .. } = asyncness {
1767+
self.ban_async_in_2015(span);
17931768
}
1794-
let asyncness = respan(self.prev_span, asyncness);
17951769
let unsafety = self.parse_unsafety();
17961770
let (constness, unsafety, ext) = if let Const::Yes(_) = constness {
17971771
(constness, unsafety, Extern::None)

src/librustc_parse/parser/mod.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use rustc_span::symbol::{kw, sym, Symbol};
2424
use rustc_span::{FileName, Span, DUMMY_SP};
2525
use syntax::ast::DUMMY_NODE_ID;
2626
use syntax::ast::{self, AttrStyle, AttrVec, Const, CrateSugar, Extern, Ident, Unsafe};
27-
use syntax::ast::{IsAsync, MacArgs, MacDelimiter, Mutability, StrLit, Visibility, VisibilityKind};
27+
use syntax::ast::{Async, MacArgs, MacDelimiter, Mutability, StrLit, Visibility, VisibilityKind};
2828
use syntax::ptr::P;
2929
use syntax::token::{self, DelimToken, Token, TokenKind};
3030
use syntax::tokenstream::{self, DelimSpan, TokenStream, TokenTree, TreeAndJoint};
@@ -954,11 +954,12 @@ impl<'a> Parser<'a> {
954954
}
955955

956956
/// Parses asyncness: `async` or nothing.
957-
fn parse_asyncness(&mut self) -> IsAsync {
957+
fn parse_asyncness(&mut self) -> Async {
958958
if self.eat_keyword(kw::Async) {
959-
IsAsync::Async { closure_id: DUMMY_NODE_ID, return_impl_trait_id: DUMMY_NODE_ID }
959+
let span = self.prev_span;
960+
Async::Yes { span, closure_id: DUMMY_NODE_ID, return_impl_trait_id: DUMMY_NODE_ID }
960961
} else {
961-
IsAsync::NotAsync
962+
Async::No
962963
}
963964
}
964965

src/librustc_resolve/def_collector.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ impl<'a> DefCollector<'a> {
4848
decl: &'a FnDecl,
4949
body: Option<&'a Block>,
5050
) {
51-
let (closure_id, return_impl_trait_id) = match header.asyncness.node {
52-
IsAsync::Async { closure_id, return_impl_trait_id } => {
51+
let (closure_id, return_impl_trait_id) = match header.asyncness {
52+
Async::Yes { span: _, closure_id, return_impl_trait_id } => {
5353
(closure_id, return_impl_trait_id)
5454
}
5555
_ => unreachable!(),
@@ -117,7 +117,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
117117
| ItemKind::ExternCrate(..)
118118
| ItemKind::ForeignMod(..)
119119
| ItemKind::TyAlias(..) => DefPathData::TypeNs(i.ident.name),
120-
ItemKind::Fn(sig, generics, body) if sig.header.asyncness.node.is_async() => {
120+
ItemKind::Fn(sig, generics, body) if sig.header.asyncness.is_async() => {
121121
return self.visit_async_fn(
122122
i.id,
123123
i.ident.name,
@@ -215,7 +215,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
215215

216216
fn visit_assoc_item(&mut self, i: &'a AssocItem, ctxt: visit::AssocCtxt) {
217217
let def_data = match &i.kind {
218-
AssocItemKind::Fn(FnSig { header, decl }, body) if header.asyncness.node.is_async() => {
218+
AssocItemKind::Fn(FnSig { header, decl }, body) if header.asyncness.is_async() => {
219219
return self.visit_async_fn(
220220
i.id,
221221
i.ident.name,
@@ -255,10 +255,10 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
255255
// we must create two defs.
256256
let closure_def = self.create_def(expr.id, DefPathData::ClosureExpr, expr.span);
257257
match asyncness {
258-
IsAsync::Async { closure_id, .. } => {
258+
Async::Yes { closure_id, .. } => {
259259
self.create_def(closure_id, DefPathData::ClosureExpr, expr.span)
260260
}
261-
IsAsync::NotAsync => closure_def,
261+
Async::No => closure_def,
262262
}
263263
}
264264
ExprKind::Async(_, async_id, _) => {

src/librustc_resolve/late.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2030,7 +2030,7 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
20302030
// `async |x| ...` gets desugared to `|x| future_from_generator(|| ...)`, so we need to
20312031
// resolve the arguments within the proper scopes so that usages of them inside the
20322032
// closure are detected as upvars rather than normal closure arg usages.
2033-
ExprKind::Closure(_, IsAsync::Async { .. }, _, ref fn_decl, ref body, _span) => {
2033+
ExprKind::Closure(_, Async::Yes { .. }, _, ref fn_decl, ref body, _span) => {
20342034
self.with_rib(ValueNS, NormalRibKind, |this| {
20352035
// Resolve arguments:
20362036
this.resolve_params(&fn_decl.inputs);

src/librustc_save_analysis/dump_visitor.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,8 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> {
290290
// as an `impl Trait` existential type. Because of this, to match
291291
// the definition paths when resolving nested types we need to
292292
// start walking from the newly-created definition.
293-
match sig.header.asyncness.node {
294-
ast::IsAsync::Async { return_impl_trait_id, .. } => {
293+
match sig.header.asyncness {
294+
ast::Async::Yes { return_impl_trait_id, .. } => {
295295
v.nest_tables(return_impl_trait_id, |v| v.visit_ty(ret_ty))
296296
}
297297
_ => v.visit_ty(ret_ty),
@@ -383,8 +383,8 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> {
383383
// as an `impl Trait` existential type. Because of this, to match
384384
// the definition paths when resolving nested types we need to
385385
// start walking from the newly-created definition.
386-
match header.asyncness.node {
387-
ast::IsAsync::Async { return_impl_trait_id, .. } => {
386+
match header.asyncness {
387+
ast::Async::Yes { return_impl_trait_id, .. } => {
388388
v.nest_tables(return_impl_trait_id, |v| v.visit_ty(ret_ty))
389389
}
390390
_ => v.visit_ty(ret_ty),

src/librustc_save_analysis/sig.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ impl Sig for ast::Item {
368368
if let ast::Const::Yes(_) = header.constness {
369369
text.push_str("const ");
370370
}
371-
if header.asyncness.node.is_async() {
371+
if header.asyncness.is_async() {
372372
text.push_str("async ");
373373
}
374374
if let ast::Unsafe::Yes(_) = header.unsafety {
@@ -887,7 +887,7 @@ fn make_method_signature(
887887
if let ast::Const::Yes(_) = m.header.constness {
888888
text.push_str("const ");
889889
}
890-
if m.header.asyncness.node.is_async() {
890+
if m.header.asyncness.is_async() {
891891
text.push_str("async ");
892892
}
893893
if let ast::Unsafe::Yes(_) = m.header.unsafety {

0 commit comments

Comments
 (0)