Skip to content

Commit 8300f51

Browse files
committed
Deny async fn in 2015 edition
Fix style issues and update diagnostic messages Update src/librustc_passes/diagnostics.rs Co-Authored-By: doctorn <me@nathancorbyn.com> Deny nested `async fn` in Rust 2015 edition Deny nested `async fn` in Rust 2015 edition Deny nested `async fn` in Rust 2015 edition
1 parent 7f19f16 commit 8300f51

22 files changed

+215
-62
lines changed

src/librustc/hir/lowering.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2906,7 +2906,7 @@ impl<'a> LoweringContext<'a> {
29062906
// `impl Future<Output = T>` here because lower_body
29072907
// only cares about the input argument patterns in the function
29082908
// declaration (decl), not the return types.
2909-
let body_id = this.lower_async_body(decl, header.asyncness, body);
2909+
let body_id = this.lower_async_body(decl, header.asyncness.node, body);
29102910

29112911
let (generics, fn_decl) = this.add_in_band_defs(
29122912
generics,
@@ -2916,7 +2916,7 @@ impl<'a> LoweringContext<'a> {
29162916
decl,
29172917
Some((fn_def_id, idty)),
29182918
true,
2919-
header.asyncness.opt_return_id()
2919+
header.asyncness.node.opt_return_id()
29202920
),
29212921
);
29222922

@@ -3410,14 +3410,14 @@ impl<'a> LoweringContext<'a> {
34103410
)
34113411
}
34123412
ImplItemKind::Method(ref sig, ref body) => {
3413-
let body_id = self.lower_async_body(&sig.decl, sig.header.asyncness, body);
3413+
let body_id = self.lower_async_body(&sig.decl, sig.header.asyncness.node, body);
34143414
let impl_trait_return_allow = !self.is_in_trait_impl;
34153415
let (generics, sig) = self.lower_method_sig(
34163416
&i.generics,
34173417
sig,
34183418
impl_item_def_id,
34193419
impl_trait_return_allow,
3420-
sig.header.asyncness.opt_return_id(),
3420+
sig.header.asyncness.node.opt_return_id(),
34213421
);
34223422
(generics, hir::ImplItemKind::Method(sig, body_id))
34233423
}
@@ -3637,7 +3637,7 @@ impl<'a> LoweringContext<'a> {
36373637
fn lower_fn_header(&mut self, h: FnHeader) -> hir::FnHeader {
36383638
hir::FnHeader {
36393639
unsafety: self.lower_unsafety(h.unsafety),
3640-
asyncness: self.lower_asyncness(h.asyncness),
3640+
asyncness: self.lower_asyncness(h.asyncness.node),
36413641
constness: self.lower_constness(h.constness),
36423642
abi: h.abi,
36433643
}

src/librustc/hir/map/def_collector.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ impl<'a> DefCollector<'a> {
7373
decl: &'a FnDecl,
7474
body: &'a Block,
7575
) {
76-
let (closure_id, return_impl_trait_id) = match header.asyncness {
76+
let (closure_id, return_impl_trait_id) = match header.asyncness.node {
7777
IsAsync::Async {
7878
closure_id,
7979
return_impl_trait_id,
@@ -129,10 +129,10 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
129129
}
130130
ItemKind::Fn(
131131
ref decl,
132-
ref header @ FnHeader { asyncness: IsAsync::Async { .. }, .. },
132+
ref header,
133133
ref generics,
134134
ref body,
135-
) => {
135+
) if header.asyncness.node.is_async() => {
136136
return self.visit_async_fn(
137137
i.id,
138138
i.ident.name,
@@ -242,9 +242,9 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
242242
fn visit_impl_item(&mut self, ii: &'a ImplItem) {
243243
let def_data = match ii.node {
244244
ImplItemKind::Method(MethodSig {
245-
header: ref header @ FnHeader { asyncness: IsAsync::Async { .. }, .. },
245+
ref header,
246246
ref decl,
247-
}, ref body) => {
247+
}, ref body) if header.asyncness.node.is_async() => {
248248
return self.visit_async_fn(
249249
ii.id,
250250
ii.ident.name,

src/librustc_passes/ast_validation.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
463463
self.invalid_visibility(&impl_item.vis, None);
464464
if let ImplItemKind::Method(ref sig, _) = impl_item.node {
465465
self.check_trait_fn_not_const(sig.header.constness);
466-
self.check_trait_fn_not_async(impl_item.span, sig.header.asyncness);
466+
self.check_trait_fn_not_async(impl_item.span, sig.header.asyncness.node);
467467
}
468468
}
469469
}
@@ -482,9 +482,10 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
482482
.note("only trait implementations may be annotated with default").emit();
483483
}
484484
}
485-
ItemKind::Fn(_, header, ref generics, _) => {
485+
ItemKind::Fn(_, ref header, ref generics, _) => {
486486
// We currently do not permit const generics in `const fn`, as
487487
// this is tantamount to allowing compile-time dependent typing.
488+
self.visit_fn_header(header);
488489
if header.constness.node == Constness::Const {
489490
// Look for const generics and error if we find any.
490491
for param in &generics.params {
@@ -535,7 +536,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
535536
self.no_questions_in_bounds(bounds, "supertraits", true);
536537
for trait_item in trait_items {
537538
if let TraitItemKind::Method(ref sig, ref block) = trait_item.node {
538-
self.check_trait_fn_not_async(trait_item.span, sig.header.asyncness);
539+
self.check_trait_fn_not_async(trait_item.span, sig.header.asyncness.node);
539540
self.check_trait_fn_not_const(sig.header.constness);
540541
if block.is_none() {
541542
self.check_decl_no_pat(&sig.decl, |span, mut_ident| {
@@ -702,6 +703,13 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
702703
.span_bug(mac.span, "macro invocation missed in expansion; did you forget to override \
703704
the relevant `fold_*()` method in `PlaceholderExpander`?");
704705
}
706+
707+
fn visit_fn_header(&mut self, header: &'a FnHeader) {
708+
if header.asyncness.node.is_async() && self.session.rust_2015() {
709+
struct_span_err!(self.session, header.asyncness.span, E0670,
710+
"`async fn` is not permitted in the 2015 edition").emit();
711+
}
712+
}
705713
}
706714

707715
pub fn check_crate(session: &Session, krate: &Crate) -> (bool, bool) {

src/librustc_passes/diagnostics.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,18 @@ loop {
310310
break;
311311
}
312312
```
313+
"##,
314+
315+
E0670: r##"
316+
Rust 2015 does not permit the use of `async fn`.
317+
318+
Example of erroneous code:
319+
320+
```compile_fail,E0670
321+
async fn foo() {}
322+
```
323+
324+
Switch to the Rust 2018 edition to use `async fn`.
313325
"##
314326
}
315327

src/librustc_resolve/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -806,9 +806,9 @@ impl<'a, 'tcx> Visitor<'tcx> for Resolver<'a> {
806806
debug!("(resolving function) entering function");
807807
let (rib_kind, asyncness) = match function_kind {
808808
FnKind::ItemFn(_, ref header, ..) =>
809-
(ItemRibKind, header.asyncness),
809+
(ItemRibKind, header.asyncness.node),
810810
FnKind::Method(_, ref sig, _, _) =>
811-
(TraitOrImplItemRibKind, sig.header.asyncness),
811+
(TraitOrImplItemRibKind, sig.header.asyncness.node),
812812
FnKind::Closure(_) =>
813813
// Async closures aren't resolved through `visit_fn`-- they're
814814
// processed separately

src/librustc_save_analysis/sig.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ impl Sig for ast::Item {
378378
if header.constness.node == ast::Constness::Const {
379379
text.push_str("const ");
380380
}
381-
if header.asyncness.is_async() {
381+
if header.asyncness.node.is_async() {
382382
text.push_str("async ");
383383
}
384384
if header.unsafety == ast::Unsafety::Unsafe {
@@ -936,7 +936,7 @@ fn make_method_signature(
936936
if m.header.constness.node == ast::Constness::Const {
937937
text.push_str("const ");
938938
}
939-
if m.header.asyncness.is_async() {
939+
if m.header.asyncness.node.is_async() {
940940
text.push_str("async ");
941941
}
942942
if m.header.unsafety == ast::Unsafety::Unsafe {

src/libsyntax/ast.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2216,7 +2216,7 @@ impl Item {
22162216
#[derive(Clone, Copy, RustcEncodable, RustcDecodable, Debug)]
22172217
pub struct FnHeader {
22182218
pub unsafety: Unsafety,
2219-
pub asyncness: IsAsync,
2219+
pub asyncness: Spanned<IsAsync>,
22202220
pub constness: Spanned<Constness>,
22212221
pub abi: Abi,
22222222
}
@@ -2225,7 +2225,7 @@ impl Default for FnHeader {
22252225
fn default() -> FnHeader {
22262226
FnHeader {
22272227
unsafety: Unsafety::Normal,
2228-
asyncness: IsAsync::NotAsync,
2228+
asyncness: dummy_spanned(IsAsync::NotAsync),
22292229
constness: dummy_spanned(Constness::NotConst),
22302230
abi: Abi::Rust,
22312231
}

src/libsyntax/ext/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1017,7 +1017,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
10171017
ast::ItemKind::Fn(self.fn_decl(inputs, ast::FunctionRetTy::Ty(output)),
10181018
ast::FnHeader {
10191019
unsafety: ast::Unsafety::Normal,
1020-
asyncness: ast::IsAsync::NotAsync,
1020+
asyncness: dummy_spanned(ast::IsAsync::NotAsync),
10211021
constness: dummy_spanned(ast::Constness::NotConst),
10221022
abi: Abi::Rust,
10231023
},

src/libsyntax/feature_gate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1894,7 +1894,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
18941894
match fn_kind {
18951895
FnKind::ItemFn(_, header, _, _) => {
18961896
// Check for const fn and async fn declarations.
1897-
if header.asyncness.is_async() {
1897+
if header.asyncness.node.is_async() {
18981898
gate_feature_post!(&self, async_await, span, "async fn is unstable");
18991899
}
19001900
// Stability of const fn methods are covered in

src/libsyntax/mut_visit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -926,7 +926,7 @@ pub fn noop_flat_map_impl_item<T: MutVisitor>(mut item: ImplItem, visitor: &mut
926926

927927
pub fn noop_visit_fn_header<T: MutVisitor>(header: &mut FnHeader, vis: &mut T) {
928928
let FnHeader { unsafety: _, asyncness, constness: _, abi: _ } = header;
929-
vis.visit_asyncness(asyncness);
929+
vis.visit_asyncness(&mut asyncness.node);
930930
}
931931

932932
pub fn noop_visit_mod<T: MutVisitor>(Mod { inner, items, inline: _ }: &mut Mod, vis: &mut T) {

0 commit comments

Comments
 (0)