Skip to content

Commit 15cb9eb

Browse files
authored
Rollup merge of rust-lang#94493 - oribenshir:feature/ISSUE-78543_async_fn_in_foreign_crate_diag_2, r=davidtwco
Improved diagnostic on failure to meet send bound on future in a foreign crate Provide a better diagnostic on failure to meet send bound on futures in a foreign crate. fixes rust-lang#78543
2 parents e2661ba + ebe3c56 commit 15cb9eb

File tree

11 files changed

+247
-53
lines changed

11 files changed

+247
-53
lines changed

compiler/rustc_metadata/src/rmeta/decoder.rs

+19
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use rustc_middle::mir::interpret::{AllocDecodingSession, AllocDecodingState};
2828
use rustc_middle::thir;
2929
use rustc_middle::ty::codec::TyDecoder;
3030
use rustc_middle::ty::fast_reject::SimplifiedType;
31+
use rustc_middle::ty::GeneratorDiagnosticData;
3132
use rustc_middle::ty::{self, Ty, TyCtxt, Visibility};
3233
use rustc_serialize::{opaque, Decodable, Decoder};
3334
use rustc_session::cstore::{
@@ -1725,6 +1726,24 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
17251726
.collect()
17261727
})
17271728
}
1729+
1730+
fn get_generator_diagnostic_data(
1731+
self,
1732+
tcx: TyCtxt<'tcx>,
1733+
id: DefIndex,
1734+
) -> Option<GeneratorDiagnosticData<'tcx>> {
1735+
self.root
1736+
.tables
1737+
.generator_diagnostic_data
1738+
.get(self, id)
1739+
.map(|param| param.decode((self, tcx)))
1740+
.map(|generator_data| GeneratorDiagnosticData {
1741+
generator_interior_types: generator_data.generator_interior_types,
1742+
hir_owner: generator_data.hir_owner,
1743+
nodes_types: generator_data.nodes_types,
1744+
adjustments: generator_data.adjustments,
1745+
})
1746+
}
17281747
}
17291748

17301749
impl CrateMetadata {

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

+1
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ provide! { <'tcx> tcx, def_id, other, cdata,
246246

247247
crate_extern_paths => { cdata.source().paths().cloned().collect() }
248248
expn_that_defined => { cdata.get_expn_that_defined(def_id.index, tcx.sess) }
249+
generator_diagnostic_data => { cdata.get_generator_diagnostic_data(tcx, def_id.index) }
249250
}
250251

251252
pub(in crate::rmeta) fn provide(providers: &mut Providers) {

compiler/rustc_metadata/src/rmeta/encoder.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1556,16 +1556,17 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
15561556
fn encode_info_for_closure(&mut self, hir_id: hir::HirId) {
15571557
let def_id = self.tcx.hir().local_def_id(hir_id);
15581558
debug!("EncodeContext::encode_info_for_closure({:?})", def_id);
1559-
15601559
// NOTE(eddyb) `tcx.type_of(def_id)` isn't used because it's fully generic,
15611560
// including on the signature, which is inferred in `typeck.
1562-
let ty = self.tcx.typeck(def_id).node_type(hir_id);
1563-
1561+
let typeck_result: &'tcx ty::TypeckResults<'tcx> = self.tcx.typeck(def_id);
1562+
let ty = typeck_result.node_type(hir_id);
15641563
match ty.kind() {
15651564
ty::Generator(..) => {
15661565
let data = self.tcx.generator_kind(def_id).unwrap();
1566+
let generator_diagnostic_data = typeck_result.get_generator_diagnostic_data();
15671567
record!(self.tables.kind[def_id.to_def_id()] <- EntryKind::Generator);
15681568
record!(self.tables.generator_kind[def_id.to_def_id()] <- data);
1569+
record!(self.tables.generator_diagnostic_data[def_id.to_def_id()] <- generator_diagnostic_data);
15691570
}
15701571

15711572
ty::Closure(..) => {

compiler/rustc_metadata/src/rmeta/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use rustc_middle::mir;
1919
use rustc_middle::thir;
2020
use rustc_middle::ty::fast_reject::SimplifiedType;
2121
use rustc_middle::ty::query::Providers;
22+
use rustc_middle::ty::GeneratorDiagnosticData;
2223
use rustc_middle::ty::{self, ReprOptions, Ty};
2324
use rustc_serialize::opaque::Encoder;
2425
use rustc_session::config::SymbolManglingVersion;
@@ -358,6 +359,7 @@ define_tables! {
358359
def_keys: Table<DefIndex, Lazy<DefKey>>,
359360
def_path_hashes: Table<DefIndex, DefPathHash>,
360361
proc_macro_quoted_spans: Table<usize, Lazy<Span>>,
362+
generator_diagnostic_data: Table<DefIndex, Lazy<GeneratorDiagnosticData<'tcx>>>,
361363
}
362364

363365
#[derive(Copy, Clone, MetadataEncodable, MetadataDecodable)]

compiler/rustc_middle/src/query/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1969,4 +1969,10 @@ rustc_queries! {
19691969
eval_always
19701970
desc { "computing the backend features for CLI flags" }
19711971
}
1972+
1973+
query generator_diagnostic_data(key: DefId) -> Option<GeneratorDiagnosticData<'tcx>> {
1974+
storage(ArenaCacheSelector<'tcx>)
1975+
desc { |tcx| "looking up generator diagnostic data of `{}`", tcx.def_path_str(key) }
1976+
separate_provide_extern
1977+
}
19721978
}

compiler/rustc_middle/src/ty/context.rs

+32
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,16 @@ pub struct GeneratorInteriorTypeCause<'tcx> {
367367
pub expr: Option<hir::HirId>,
368368
}
369369

370+
// This type holds diagnostic information on generators and async functions across crate boundaries
371+
// and is used to provide better error messages
372+
#[derive(TyEncodable, TyDecodable, Clone, Debug, HashStable)]
373+
pub struct GeneratorDiagnosticData<'tcx> {
374+
pub generator_interior_types: ty::Binder<'tcx, Vec<GeneratorInteriorTypeCause<'tcx>>>,
375+
pub hir_owner: DefId,
376+
pub nodes_types: ItemLocalMap<Ty<'tcx>>,
377+
pub adjustments: ItemLocalMap<Vec<ty::adjustment::Adjustment<'tcx>>>,
378+
}
379+
370380
#[derive(TyEncodable, TyDecodable, Debug, HashStable)]
371381
pub struct TypeckResults<'tcx> {
372382
/// The `HirId::owner` all `ItemLocalId`s in this table are relative to.
@@ -623,6 +633,28 @@ impl<'tcx> TypeckResults<'tcx> {
623633
LocalTableInContextMut { hir_owner: self.hir_owner, data: &mut self.node_types }
624634
}
625635

636+
pub fn get_generator_diagnostic_data(&self) -> GeneratorDiagnosticData<'tcx> {
637+
let generator_interior_type = self.generator_interior_types.map_bound_ref(|vec| {
638+
vec.iter()
639+
.map(|item| {
640+
GeneratorInteriorTypeCause {
641+
ty: item.ty,
642+
span: item.span,
643+
scope_span: item.scope_span,
644+
yield_span: item.yield_span,
645+
expr: None, //FIXME: Passing expression over crate boundaries is impossible at the moment
646+
}
647+
})
648+
.collect::<Vec<_>>()
649+
});
650+
GeneratorDiagnosticData {
651+
generator_interior_types: generator_interior_type,
652+
hir_owner: self.hir_owner.to_def_id(),
653+
nodes_types: self.node_types.clone(),
654+
adjustments: self.adjustments.clone(),
655+
}
656+
}
657+
626658
pub fn node_type(&self, id: hir::HirId) -> Ty<'tcx> {
627659
self.node_type_opt(id).unwrap_or_else(|| {
628660
bug!("node_type: no type for node `{}`", tls::with(|tcx| tcx.hir().node_to_string(id)))

compiler/rustc_middle/src/ty/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,9 @@ pub use self::consts::{
6767
};
6868
pub use self::context::{
6969
tls, CanonicalUserType, CanonicalUserTypeAnnotation, CanonicalUserTypeAnnotations,
70-
CtxtInterners, DelaySpanBugEmitted, FreeRegionInfo, GeneratorInteriorTypeCause, GlobalCtxt,
71-
Lift, OnDiskCache, TyCtxt, TypeckResults, UserType, UserTypeAnnotationIndex,
70+
CtxtInterners, DelaySpanBugEmitted, FreeRegionInfo, GeneratorDiagnosticData,
71+
GeneratorInteriorTypeCause, GlobalCtxt, Lift, OnDiskCache, TyCtxt, TypeckResults, UserType,
72+
UserTypeAnnotationIndex,
7273
};
7374
pub use self::instance::{Instance, InstanceDef};
7475
pub use self::list::List;

compiler/rustc_middle/src/ty/query.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,11 @@ use crate::traits::{self, ImplSource};
3131
use crate::ty::fast_reject::SimplifiedType;
3232
use crate::ty::subst::{GenericArg, SubstsRef};
3333
use crate::ty::util::AlwaysRequiresDrop;
34+
use crate::ty::GeneratorDiagnosticData;
3435
use crate::ty::{self, AdtSizedConstraint, CrateInherentImpls, ParamEnvAnd, Ty, TyCtxt};
36+
use rustc_ast as ast;
3537
use rustc_ast::expand::allocator::AllocatorKind;
38+
use rustc_attr as attr;
3639
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap};
3740
use rustc_data_structures::steal::Steal;
3841
use rustc_data_structures::svh::Svh;
@@ -49,13 +52,10 @@ use rustc_session::cstore::{CrateDepKind, CrateSource};
4952
use rustc_session::cstore::{ExternCrate, ForeignModule, LinkagePreference, NativeLib};
5053
use rustc_session::utils::NativeLibKind;
5154
use rustc_session::Limits;
52-
use rustc_target::abi;
53-
use rustc_target::spec::PanicStrategy;
54-
55-
use rustc_ast as ast;
56-
use rustc_attr as attr;
5755
use rustc_span::symbol::Symbol;
5856
use rustc_span::{Span, DUMMY_SP};
57+
use rustc_target::abi;
58+
use rustc_target::spec::PanicStrategy;
5959
use std::ops::Deref;
6060
use std::path::PathBuf;
6161
use std::sync::Arc;

0 commit comments

Comments
 (0)