Skip to content

Commit 087a571

Browse files
Record asyncness span in HIR
1 parent 0fd7ce9 commit 087a571

File tree

22 files changed

+78
-54
lines changed

22 files changed

+78
-54
lines changed

Diff for: compiler/rustc_ast_lowering/src/item.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1308,7 +1308,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
13081308

13091309
fn lower_asyncness(&mut self, a: Async) -> hir::IsAsync {
13101310
match a {
1311-
Async::Yes { .. } => hir::IsAsync::Async,
1311+
Async::Yes { span, .. } => hir::IsAsync::Async(span),
13121312
Async::No => hir::IsAsync::NotAsync,
13131313
}
13141314
}

Diff for: compiler/rustc_borrowck/src/diagnostics/region_name.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
302302
if free_region.bound_region.is_named() {
303303
// A named region that is actually named.
304304
Some(RegionName { name, source: RegionNameSource::NamedFreeRegion(span) })
305-
} else if let hir::IsAsync::Async = tcx.asyncness(self.mir_hir_id().owner) {
305+
} else if tcx.asyncness(self.mir_hir_id().owner).is_async() {
306306
// If we spuriously thought that the region is named, we should let the
307307
// system generate a true name for error messages. Currently this can
308308
// happen if we have an elided name in an async fn for example: the

Diff for: compiler/rustc_hir/src/hir.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -2853,13 +2853,13 @@ impl ImplicitSelfKind {
28532853
#[derive(Copy, Clone, PartialEq, Eq, Encodable, Decodable, Debug)]
28542854
#[derive(HashStable_Generic)]
28552855
pub enum IsAsync {
2856-
Async,
2856+
Async(Span),
28572857
NotAsync,
28582858
}
28592859

28602860
impl IsAsync {
28612861
pub fn is_async(self) -> bool {
2862-
self == IsAsync::Async
2862+
matches!(self, IsAsync::Async(_))
28632863
}
28642864
}
28652865

@@ -3296,7 +3296,7 @@ pub struct FnHeader {
32963296

32973297
impl FnHeader {
32983298
pub fn is_async(&self) -> bool {
3299-
matches!(&self.asyncness, IsAsync::Async)
3299+
matches!(&self.asyncness, IsAsync::Async(_))
33003300
}
33013301

33023302
pub fn is_const(&self) -> bool {
@@ -4091,10 +4091,10 @@ mod size_asserts {
40914091
static_assert_size!(GenericBound<'_>, 48);
40924092
static_assert_size!(Generics<'_>, 56);
40934093
static_assert_size!(Impl<'_>, 80);
4094-
static_assert_size!(ImplItem<'_>, 80);
4095-
static_assert_size!(ImplItemKind<'_>, 32);
4096-
static_assert_size!(Item<'_>, 80);
4097-
static_assert_size!(ItemKind<'_>, 48);
4094+
static_assert_size!(ImplItem<'_>, 88);
4095+
static_assert_size!(ImplItemKind<'_>, 40);
4096+
static_assert_size!(Item<'_>, 88);
4097+
static_assert_size!(ItemKind<'_>, 56);
40984098
static_assert_size!(Local<'_>, 64);
40994099
static_assert_size!(Param<'_>, 32);
41004100
static_assert_size!(Pat<'_>, 72);
@@ -4105,8 +4105,8 @@ mod size_asserts {
41054105
static_assert_size!(Res, 12);
41064106
static_assert_size!(Stmt<'_>, 32);
41074107
static_assert_size!(StmtKind<'_>, 16);
4108-
static_assert_size!(TraitItem<'_>, 80);
4109-
static_assert_size!(TraitItemKind<'_>, 40);
4108+
static_assert_size!(TraitItem<'_>, 88);
4109+
static_assert_size!(TraitItemKind<'_>, 48);
41104110
static_assert_size!(Ty<'_>, 48);
41114111
static_assert_size!(TyKind<'_>, 32);
41124112
// tidy-alphabetical-end

Diff for: compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ fn compare_asyncness<'tcx>(
595595
trait_m: ty::AssocItem,
596596
delay: bool,
597597
) -> Result<(), ErrorGuaranteed> {
598-
if tcx.asyncness(trait_m.def_id) == hir::IsAsync::Async {
598+
if tcx.asyncness(trait_m.def_id).is_async() {
599599
match tcx.fn_sig(impl_m.def_id).skip_binder().skip_binder().output().kind() {
600600
ty::Alias(ty::Opaque, ..) => {
601601
// allow both `async fn foo()` and `fn foo() -> impl Future`

Diff for: compiler/rustc_hir_analysis/src/check/entry.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ fn check_main_fn_ty(tcx: TyCtxt<'_>, main_def_id: DefId) {
112112
}
113113

114114
let main_asyncness = tcx.asyncness(main_def_id);
115-
if let hir::IsAsync::Async = main_asyncness {
115+
if main_asyncness.is_async() {
116116
let asyncness_span = main_fn_asyncness_span(tcx, main_def_id);
117117
tcx.sess.emit_err(errors::MainFunctionAsync { span: main_span, asyncness: asyncness_span });
118118
error = true;
@@ -212,7 +212,7 @@ fn check_start_fn_ty(tcx: TyCtxt<'_>, start_def_id: DefId) {
212212
});
213213
error = true;
214214
}
215-
if let hir::IsAsync::Async = sig.header.asyncness {
215+
if sig.header.asyncness.is_async() {
216216
let span = tcx.def_span(it.owner_id);
217217
tcx.sess.emit_err(errors::StartAsync { span: span });
218218
error = true;

Diff for: compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1213,7 +1213,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
12131213
&& let Some(generics) = self.tcx.hir().get_generics(self.tcx.local_parent(param_id))
12141214
&& let Some(param) = generics.params.iter().find(|p| p.def_id == param_id)
12151215
&& param.is_elided_lifetime()
1216-
&& let hir::IsAsync::NotAsync = self.tcx.asyncness(lifetime_ref.hir_id.owner.def_id)
1216+
&& !self.tcx.asyncness(lifetime_ref.hir_id.owner.def_id).is_async()
12171217
&& !self.tcx.features().anonymous_lifetime_in_impl_trait
12181218
{
12191219
let mut diag = rustc_session::parse::feature_err(

Diff for: compiler/rustc_hir_pretty/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2304,7 +2304,7 @@ impl<'a> State<'a> {
23042304

23052305
match header.asyncness {
23062306
hir::IsAsync::NotAsync => {}
2307-
hir::IsAsync::Async => self.word_nbsp("async"),
2307+
hir::IsAsync::Async(_) => self.word_nbsp("async"),
23082308
}
23092309

23102310
self.print_unsafety(header.unsafety);

Diff for: compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -987,10 +987,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
987987
let bound_vars = self.tcx.late_bound_vars(fn_id);
988988
let ty = self.tcx.erase_late_bound_regions(Binder::bind_with_vars(ty, bound_vars));
989989
let ty = match self.tcx.asyncness(fn_id.owner) {
990-
hir::IsAsync::Async => self.get_impl_future_output_ty(ty).unwrap_or_else(|| {
990+
ty::Asyncness::Yes => self.get_impl_future_output_ty(ty).unwrap_or_else(|| {
991991
span_bug!(fn_decl.output.span(), "failed to get output type of async function")
992992
}),
993-
hir::IsAsync::NotAsync => ty,
993+
ty::Asyncness::No => ty,
994994
};
995995
let ty = self.normalize(expr.span, ty);
996996
if self.can_coerce(found, ty) {

Diff for: compiler/rustc_lint/src/builtin.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ use crate::{
4141
},
4242
EarlyContext, EarlyLintPass, LateContext, LateLintPass, Level, LintContext,
4343
};
44-
use hir::IsAsync;
4544
use rustc_ast::attr;
4645
use rustc_ast::tokenstream::{TokenStream, TokenTree};
4746
use rustc_ast::visit::{FnCtxt, FnKind};
@@ -1294,7 +1293,7 @@ impl<'tcx> LateLintPass<'tcx> for UngatedAsyncFnTrackCaller {
12941293
span: Span,
12951294
def_id: LocalDefId,
12961295
) {
1297-
if fn_kind.asyncness() == IsAsync::Async
1296+
if fn_kind.asyncness().is_async()
12981297
&& !cx.tcx.features().async_fn_track_caller
12991298
// Now, check if the function has the `#[track_caller]` attribute
13001299
&& let Some(attr) = cx.tcx.get_attr(def_id, sym::track_caller)

Diff for: compiler/rustc_metadata/src/rmeta/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ define_tables! {
439439
coerce_unsized_info: Table<DefIndex, LazyValue<ty::adjustment::CoerceUnsizedInfo>>,
440440
mir_const_qualif: Table<DefIndex, LazyValue<mir::ConstQualifs>>,
441441
rendered_const: Table<DefIndex, LazyValue<String>>,
442-
asyncness: Table<DefIndex, hir::IsAsync>,
442+
asyncness: Table<DefIndex, ty::Asyncness>,
443443
fn_arg_names: Table<DefIndex, LazyArray<Ident>>,
444444
generator_kind: Table<DefIndex, LazyValue<hir::GeneratorKind>>,
445445
trait_def: Table<DefIndex, LazyValue<ty::TraitDef>>,

Diff for: compiler/rustc_metadata/src/rmeta/table.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,9 @@ fixed_size_enum! {
205205
}
206206

207207
fixed_size_enum! {
208-
hir::IsAsync {
209-
( NotAsync )
210-
( Async )
208+
ty::Asyncness {
209+
( Yes )
210+
( No )
211211
}
212212
}
213213

Diff for: compiler/rustc_middle/src/query/erase.rs

+1
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ trivial! {
265265
rustc_middle::ty::adjustment::CoerceUnsizedInfo,
266266
rustc_middle::ty::AssocItem,
267267
rustc_middle::ty::AssocItemContainer,
268+
rustc_middle::ty::Asyncness,
268269
rustc_middle::ty::BoundVariableKind,
269270
rustc_middle::ty::DeducedParamAttrs,
270271
rustc_middle::ty::Destructor,

Diff for: compiler/rustc_middle/src/query/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,7 @@ rustc_queries! {
731731
separate_provide_extern
732732
}
733733

734-
query asyncness(key: DefId) -> hir::IsAsync {
734+
query asyncness(key: DefId) -> ty::Asyncness {
735735
desc { |tcx| "checking if the function is async: `{}`", tcx.def_path_str(key) }
736736
separate_provide_extern
737737
}

Diff for: compiler/rustc_middle/src/ty/mod.rs

+13
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,19 @@ impl fmt::Display for ImplPolarity {
280280
}
281281
}
282282

283+
#[derive(Copy, Clone, PartialEq, Eq, Hash, TyEncodable, TyDecodable, HashStable, Debug)]
284+
#[derive(TypeFoldable, TypeVisitable)]
285+
pub enum Asyncness {
286+
Yes,
287+
No,
288+
}
289+
290+
impl Asyncness {
291+
pub fn is_async(self) -> bool {
292+
matches!(self, Asyncness::Yes)
293+
}
294+
}
295+
283296
#[derive(Clone, Debug, PartialEq, Eq, Copy, Hash, Encodable, Decodable, HashStable)]
284297
pub enum Visibility<Id = LocalDefId> {
285298
/// Visible everywhere (including in other crates).

Diff for: compiler/rustc_middle/src/ty/parameterized.rs

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ trivially_parameterized_over_tcx! {
6262
crate::middle::resolve_bound_vars::ObjectLifetimeDefault,
6363
crate::mir::ConstQualifs,
6464
ty::AssocItemContainer,
65+
ty::Asyncness,
6566
ty::DeducedParamAttrs,
6667
ty::Generics,
6768
ty::ImplPolarity,

Diff for: compiler/rustc_trait_selection/src/traits/error_reporting/on_unimplemented.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,9 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
104104
hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn(sig, _, body_id), .. }) => {
105105
self.describe_generator(*body_id).or_else(|| {
106106
Some(match sig.header {
107-
hir::FnHeader { asyncness: hir::IsAsync::Async, .. } => "an async function",
107+
hir::FnHeader { asyncness: hir::IsAsync::Async(_), .. } => {
108+
"an async function"
109+
}
108110
_ => "a function",
109111
})
110112
})
@@ -118,7 +120,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
118120
..
119121
}) => self.describe_generator(*body_id).or_else(|| {
120122
Some(match sig.header {
121-
hir::FnHeader { asyncness: hir::IsAsync::Async, .. } => "an async method",
123+
hir::FnHeader { asyncness: hir::IsAsync::Async(_), .. } => "an async method",
122124
_ => "a method",
123125
})
124126
}),

Diff for: compiler/rustc_ty_utils/src/ty.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -296,9 +296,12 @@ fn issue33140_self_ty(tcx: TyCtxt<'_>, def_id: DefId) -> Option<EarlyBinder<Ty<'
296296
}
297297

298298
/// Check if a function is async.
299-
fn asyncness(tcx: TyCtxt<'_>, def_id: LocalDefId) -> hir::IsAsync {
299+
fn asyncness(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Asyncness {
300300
let node = tcx.hir().get_by_def_id(def_id);
301-
node.fn_sig().map_or(hir::IsAsync::NotAsync, |sig| sig.header.asyncness)
301+
node.fn_sig().map_or(ty::Asyncness::No, |sig| match sig.header.asyncness {
302+
hir::IsAsync::Async(_) => ty::Asyncness::Yes,
303+
hir::IsAsync::NotAsync => ty::Asyncness::No,
304+
})
302305
}
303306

304307
fn unsizing_params_for_adt<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> BitSet<u32> {

Diff for: src/librustdoc/clean/types.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use rustc_resolve::rustdoc::{
3131
use rustc_session::Session;
3232
use rustc_span::hygiene::MacroKind;
3333
use rustc_span::symbol::{kw, sym, Ident, Symbol};
34-
use rustc_span::{self, FileName, Loc};
34+
use rustc_span::{self, FileName, Loc, DUMMY_SP};
3535
use rustc_target::abi::VariantIdx;
3636
use rustc_target::spec::abi::Abi;
3737

@@ -622,7 +622,7 @@ impl Item {
622622
fn build_fn_header(
623623
def_id: DefId,
624624
tcx: TyCtxt<'_>,
625-
asyncness: hir::IsAsync,
625+
asyncness: ty::Asyncness,
626626
) -> hir::FnHeader {
627627
let sig = tcx.fn_sig(def_id).skip_binder();
628628
let constness =
@@ -631,6 +631,10 @@ impl Item {
631631
} else {
632632
hir::Constness::NotConst
633633
};
634+
let asyncness = match asyncness {
635+
ty::Asyncness::Yes => hir::IsAsync::Async(DUMMY_SP),
636+
ty::Asyncness::No => hir::IsAsync::NotAsync,
637+
};
634638
hir::FnHeader { unsafety: sig.unsafety(), abi: sig.abi(), constness, asyncness }
635639
}
636640
let header = match *self.kind {

Diff for: src/librustdoc/html/format.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1599,7 +1599,7 @@ impl PrintWithSpace for hir::Unsafety {
15991599
impl PrintWithSpace for hir::IsAsync {
16001600
fn print_with_space(&self) -> &str {
16011601
match self {
1602-
hir::IsAsync::Async => "async ",
1602+
hir::IsAsync::Async(_) => "async ",
16031603
hir::IsAsync::NotAsync => "",
16041604
}
16051605
}

Diff for: src/tools/clippy/clippy_lints/src/redundant_closure_call.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use rustc_hir::intravisit::{Visitor as HirVisitor, Visitor};
1010
use rustc_lint::{LateContext, LateLintPass};
1111
use rustc_middle::hir::nested_filter;
1212
use rustc_middle::lint::in_external_macro;
13+
use rustc_middle::ty;
1314
use rustc_session::{declare_lint_pass, declare_tool_lint};
1415

1516
declare_clippy_lint! {
@@ -84,7 +85,7 @@ fn find_innermost_closure<'tcx>(
8485
cx: &LateContext<'tcx>,
8586
mut expr: &'tcx hir::Expr<'tcx>,
8687
mut steps: usize,
87-
) -> Option<(&'tcx hir::Expr<'tcx>, &'tcx hir::FnDecl<'tcx>, hir::IsAsync)> {
88+
) -> Option<(&'tcx hir::Expr<'tcx>, &'tcx hir::FnDecl<'tcx>, ty::Asyncness)> {
8889
let mut data = None;
8990

9091
while let hir::ExprKind::Closure(closure) = expr.kind
@@ -98,9 +99,9 @@ fn find_innermost_closure<'tcx>(
9899
{
99100
expr = body.value;
100101
data = Some((body.value, closure.fn_decl, if is_async_closure(body) {
101-
hir::IsAsync::Async
102+
ty::Asyncness::Yes
102103
} else {
103-
hir::IsAsync::NotAsync
104+
ty::Asyncness::No
104105
}));
105106
steps -= 1;
106107
}

Diff for: src/tools/clippy/clippy_utils/src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ use rustc_hir::intravisit::{walk_expr, FnKind, Visitor};
9090
use rustc_hir::LangItem::{OptionNone, OptionSome, ResultErr, ResultOk};
9191
use rustc_hir::{
9292
self as hir, def, Arm, ArrayLen, BindingAnnotation, Block, BlockCheckMode, Body, Closure, Destination, Expr,
93-
ExprField, ExprKind, FnDecl, FnRetTy, GenericArgs, HirId, Impl, ImplItem, ImplItemKind, ImplItemRef, IsAsync, Item,
93+
ExprField, ExprKind, FnDecl, FnRetTy, GenericArgs, HirId, Impl, ImplItem, ImplItemKind, ImplItemRef, Item,
9494
ItemKind, LangItem, Local, MatchSource, Mutability, Node, OwnerId, Param, Pat, PatKind, Path, PathSegment, PrimTy,
9595
QPath, Stmt, StmtKind, TraitItem, TraitItemKind, TraitItemRef, TraitRef, TyKind, UnOp,
9696
};
@@ -1958,8 +1958,8 @@ pub fn if_sequence<'tcx>(mut expr: &'tcx Expr<'tcx>) -> (Vec<&'tcx Expr<'tcx>>,
19581958
/// Checks if the given function kind is an async function.
19591959
pub fn is_async_fn(kind: FnKind<'_>) -> bool {
19601960
match kind {
1961-
FnKind::ItemFn(_, _, header) => header.asyncness == IsAsync::Async,
1962-
FnKind::Method(_, sig) => sig.header.asyncness == IsAsync::Async,
1961+
FnKind::ItemFn(_, _, header) => header.asyncness .is_async(),
1962+
FnKind::Method(_, sig) => sig.header.asyncness.is_async(),
19631963
FnKind::Closure => false,
19641964
}
19651965
}

Diff for: tests/ui/stats/hir-stats.stderr

+16-16
Original file line numberDiff line numberDiff line change
@@ -146,33 +146,33 @@ hir-stats - Trait 192 ( 2.1%) 4
146146
hir-stats WherePredicate 192 ( 2.1%) 3 64
147147
hir-stats - BoundPredicate 192 ( 2.1%) 3
148148
hir-stats Block 288 ( 3.2%) 6 48
149-
hir-stats Pat 360 ( 4.0%) 5 72
149+
hir-stats Pat 360 ( 3.9%) 5 72
150150
hir-stats - Wild 72 ( 0.8%) 1
151151
hir-stats - Struct 72 ( 0.8%) 1
152152
hir-stats - Binding 216 ( 2.4%) 3
153153
hir-stats GenericParam 400 ( 4.4%) 5 80
154-
hir-stats Generics 560 ( 6.2%) 10 56
155-
hir-stats Ty 720 ( 8.0%) 15 48
154+
hir-stats Generics 560 ( 6.1%) 10 56
155+
hir-stats Ty 720 ( 7.9%) 15 48
156156
hir-stats - Ptr 48 ( 0.5%) 1
157157
hir-stats - Ref 48 ( 0.5%) 1
158-
hir-stats - Path 624 ( 6.9%) 13
159-
hir-stats Expr 768 ( 8.5%) 12 64
158+
hir-stats - Path 624 ( 6.8%) 13
159+
hir-stats Expr 768 ( 8.4%) 12 64
160160
hir-stats - Path 64 ( 0.7%) 1
161161
hir-stats - Struct 64 ( 0.7%) 1
162162
hir-stats - Match 64 ( 0.7%) 1
163163
hir-stats - InlineAsm 64 ( 0.7%) 1
164164
hir-stats - Lit 128 ( 1.4%) 2
165165
hir-stats - Block 384 ( 4.2%) 6
166-
hir-stats Item 880 ( 9.7%) 11 80
167-
hir-stats - Trait 80 ( 0.9%) 1
168-
hir-stats - Enum 80 ( 0.9%) 1
169-
hir-stats - ExternCrate 80 ( 0.9%) 1
170-
hir-stats - ForeignMod 80 ( 0.9%) 1
171-
hir-stats - Impl 80 ( 0.9%) 1
172-
hir-stats - Fn 160 ( 1.8%) 2
173-
hir-stats - Use 320 ( 3.5%) 4
174-
hir-stats Path 1_240 (13.7%) 31 40
175-
hir-stats PathSegment 1_920 (21.2%) 40 48
166+
hir-stats Item 968 (10.6%) 11 88
167+
hir-stats - Trait 88 ( 1.0%) 1
168+
hir-stats - Enum 88 ( 1.0%) 1
169+
hir-stats - ExternCrate 88 ( 1.0%) 1
170+
hir-stats - ForeignMod 88 ( 1.0%) 1
171+
hir-stats - Impl 88 ( 1.0%) 1
172+
hir-stats - Fn 176 ( 1.9%) 2
173+
hir-stats - Use 352 ( 3.9%) 4
174+
hir-stats Path 1_240 (13.6%) 31 40
175+
hir-stats PathSegment 1_920 (21.0%) 40 48
176176
hir-stats ----------------------------------------------------------------
177-
hir-stats Total 9_048
177+
hir-stats Total 9_136
178178
hir-stats

0 commit comments

Comments
 (0)