Skip to content

Commit ebe3c56

Browse files
committed
Provide a better diagnostic on failure to meet send bound on futures in a foreign crate
Adding diagnostic data on generators to the crate metadata and using it to provide a better diagnostic on failure to meet send bound on futures originated from a foreign crate
1 parent 07bb916 commit ebe3c56

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::{
@@ -1732,6 +1733,24 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
17321733
.collect()
17331734
})
17341735
}
1736+
1737+
fn get_generator_diagnostic_data(
1738+
self,
1739+
tcx: TyCtxt<'tcx>,
1740+
id: DefIndex,
1741+
) -> Option<GeneratorDiagnosticData<'tcx>> {
1742+
self.root
1743+
.tables
1744+
.generator_diagnostic_data
1745+
.get(self, id)
1746+
.map(|param| param.decode((self, tcx)))
1747+
.map(|generator_data| GeneratorDiagnosticData {
1748+
generator_interior_types: generator_data.generator_interior_types,
1749+
hir_owner: generator_data.hir_owner,
1750+
nodes_types: generator_data.nodes_types,
1751+
adjustments: generator_data.adjustments,
1752+
})
1753+
}
17351754
}
17361755

17371756
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
@@ -1550,16 +1550,17 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
15501550
fn encode_info_for_closure(&mut self, hir_id: hir::HirId) {
15511551
let def_id = self.tcx.hir().local_def_id(hir_id);
15521552
debug!("EncodeContext::encode_info_for_closure({:?})", def_id);
1553-
15541553
// NOTE(eddyb) `tcx.type_of(def_id)` isn't used because it's fully generic,
15551554
// including on the signature, which is inferred in `typeck.
1556-
let ty = self.tcx.typeck(def_id).node_type(hir_id);
1557-
1555+
let typeck_result: &'tcx ty::TypeckResults<'tcx> = self.tcx.typeck(def_id);
1556+
let ty = typeck_result.node_type(hir_id);
15581557
match ty.kind() {
15591558
ty::Generator(..) => {
15601559
let data = self.tcx.generator_kind(def_id).unwrap();
1560+
let generator_diagnostic_data = typeck_result.get_generator_diagnostic_data();
15611561
record!(self.tables.kind[def_id.to_def_id()] <- EntryKind::Generator);
15621562
record!(self.tables.generator_kind[def_id.to_def_id()] <- data);
1563+
record!(self.tables.generator_diagnostic_data[def_id.to_def_id()] <- generator_diagnostic_data);
15631564
}
15641565

15651566
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
@@ -1962,4 +1962,10 @@ rustc_queries! {
19621962
eval_always
19631963
desc { "computing the backend features for CLI flags" }
19641964
}
1965+
1966+
query generator_diagnostic_data(key: DefId) -> Option<GeneratorDiagnosticData<'tcx>> {
1967+
storage(ArenaCacheSelector<'tcx>)
1968+
desc { |tcx| "looking up generator diagnostic data of `{}`", tcx.def_path_str(key) }
1969+
separate_provide_extern
1970+
}
19651971
}

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)