Skip to content

Commit 131772c

Browse files
committed
Auto merge of #69076 - cjgillot:split_trait, r=matthewjasper
Split librustc::{traits,infer} to their respective crates Followup on #67953. I tried to follow the existing module structures. cc @eddyb r? @Zoxc
2 parents 0f1e814 + 0144a97 commit 131772c

File tree

114 files changed

+5588
-4695
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

114 files changed

+5588
-4695
lines changed

Cargo.lock

+29-5
Original file line numberDiff line numberDiff line change
@@ -3783,19 +3783,15 @@ dependencies = [
37833783
name = "rustc_infer"
37843784
version = "0.0.0"
37853785
dependencies = [
3786-
"fmt_macros",
37873786
"graphviz",
37883787
"log",
37893788
"rustc",
37903789
"rustc_ast",
3791-
"rustc_attr",
37923790
"rustc_data_structures",
3793-
"rustc_error_codes",
37943791
"rustc_errors",
37953792
"rustc_hir",
37963793
"rustc_index",
37973794
"rustc_macros",
3798-
"rustc_session",
37993795
"rustc_span",
38003796
"rustc_target",
38013797
"smallvec 1.0.0",
@@ -3835,6 +3831,7 @@ dependencies = [
38353831
"rustc_session",
38363832
"rustc_span",
38373833
"rustc_target",
3834+
"rustc_trait_selection",
38383835
"rustc_traits",
38393836
"rustc_ty",
38403837
"rustc_typeck",
@@ -3869,6 +3866,7 @@ dependencies = [
38693866
"rustc_session",
38703867
"rustc_span",
38713868
"rustc_target",
3869+
"rustc_trait_selection",
38723870
"unicode-security",
38733871
]
38743872

@@ -3938,6 +3936,7 @@ dependencies = [
39383936
"rustc_macros",
39393937
"rustc_span",
39403938
"rustc_target",
3939+
"rustc_trait_selection",
39413940
"serialize",
39423941
"smallvec 1.0.0",
39433942
]
@@ -3961,6 +3960,7 @@ dependencies = [
39613960
"rustc_session",
39623961
"rustc_span",
39633962
"rustc_target",
3963+
"rustc_trait_selection",
39643964
"serialize",
39653965
"smallvec 1.0.0",
39663966
]
@@ -4001,6 +4001,7 @@ dependencies = [
40014001
"rustc_session",
40024002
"rustc_span",
40034003
"rustc_target",
4004+
"rustc_trait_selection",
40044005
]
40054006

40064007
[[package]]
@@ -4048,7 +4049,6 @@ dependencies = [
40484049
"rustc_expand",
40494050
"rustc_feature",
40504051
"rustc_hir",
4051-
"rustc_infer",
40524052
"rustc_metadata",
40534053
"rustc_session",
40544054
"rustc_span",
@@ -4128,6 +4128,27 @@ version = "0.2.0"
41284128
source = "registry+https://github.com/rust-lang/crates.io-index"
41294129
checksum = "b725dadae9fabc488df69a287f5a99c5eaf5d10853842a8a3dfac52476f544ee"
41304130

4131+
[[package]]
4132+
name = "rustc_trait_selection"
4133+
version = "0.0.0"
4134+
dependencies = [
4135+
"fmt_macros",
4136+
"log",
4137+
"rustc",
4138+
"rustc_ast",
4139+
"rustc_attr",
4140+
"rustc_data_structures",
4141+
"rustc_errors",
4142+
"rustc_hir",
4143+
"rustc_index",
4144+
"rustc_infer",
4145+
"rustc_macros",
4146+
"rustc_session",
4147+
"rustc_span",
4148+
"rustc_target",
4149+
"smallvec 1.0.0",
4150+
]
4151+
41314152
[[package]]
41324153
name = "rustc_traits"
41334154
version = "0.0.0"
@@ -4141,6 +4162,7 @@ dependencies = [
41414162
"rustc_macros",
41424163
"rustc_span",
41434164
"rustc_target",
4165+
"rustc_trait_selection",
41444166
"smallvec 1.0.0",
41454167
]
41464168

@@ -4155,6 +4177,7 @@ dependencies = [
41554177
"rustc_infer",
41564178
"rustc_span",
41574179
"rustc_target",
4180+
"rustc_trait_selection",
41584181
]
41594182

41604183
[[package]]
@@ -4173,6 +4196,7 @@ dependencies = [
41734196
"rustc_infer",
41744197
"rustc_span",
41754198
"rustc_target",
4199+
"rustc_trait_selection",
41764200
"smallvec 1.0.0",
41774201
]
41784202

src/librustc/traits/select.rs

+41
Original file line numberDiff line numberDiff line change
@@ -288,3 +288,44 @@ impl<T: Clone> WithDepNode<T> {
288288
self.cached_value.clone()
289289
}
290290
}
291+
292+
#[derive(Clone, Debug)]
293+
pub enum IntercrateAmbiguityCause {
294+
DownstreamCrate { trait_desc: String, self_desc: Option<String> },
295+
UpstreamCrateUpdate { trait_desc: String, self_desc: Option<String> },
296+
ReservationImpl { message: String },
297+
}
298+
299+
impl IntercrateAmbiguityCause {
300+
/// Emits notes when the overlap is caused by complex intercrate ambiguities.
301+
/// See #23980 for details.
302+
pub fn add_intercrate_ambiguity_hint(&self, err: &mut rustc_errors::DiagnosticBuilder<'_>) {
303+
err.note(&self.intercrate_ambiguity_hint());
304+
}
305+
306+
pub fn intercrate_ambiguity_hint(&self) -> String {
307+
match self {
308+
&IntercrateAmbiguityCause::DownstreamCrate { ref trait_desc, ref self_desc } => {
309+
let self_desc = if let &Some(ref ty) = self_desc {
310+
format!(" for type `{}`", ty)
311+
} else {
312+
String::new()
313+
};
314+
format!("downstream crates may implement trait `{}`{}", trait_desc, self_desc)
315+
}
316+
&IntercrateAmbiguityCause::UpstreamCrateUpdate { ref trait_desc, ref self_desc } => {
317+
let self_desc = if let &Some(ref ty) = self_desc {
318+
format!(" for type `{}`", ty)
319+
} else {
320+
String::new()
321+
};
322+
format!(
323+
"upstream crates may add a new impl of trait `{}`{} \
324+
in future versions",
325+
trait_desc, self_desc
326+
)
327+
}
328+
&IntercrateAmbiguityCause::ReservationImpl { ref message } => message.clone(),
329+
}
330+
}
331+
}

src/librustc_infer/Cargo.toml

-4
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,14 @@ path = "lib.rs"
1010
doctest = false
1111

1212
[dependencies]
13-
fmt_macros = { path = "../libfmt_macros" }
1413
graphviz = { path = "../libgraphviz" }
1514
log = { version = "0.4", features = ["release_max_level_info", "std"] }
16-
rustc_attr = { path = "../librustc_attr" }
1715
rustc = { path = "../librustc" }
1816
rustc_data_structures = { path = "../librustc_data_structures" }
1917
rustc_errors = { path = "../librustc_errors" }
20-
rustc_error_codes = { path = "../librustc_error_codes" }
2118
rustc_hir = { path = "../librustc_hir" }
2219
rustc_index = { path = "../librustc_index" }
2320
rustc_macros = { path = "../librustc_macros" }
24-
rustc_session = { path = "../librustc_session" }
2521
rustc_span = { path = "../librustc_span" }
2622
rustc_target = { path = "../librustc_target" }
2723
smallvec = { version = "1.0", features = ["union", "may_dangle"] }

src/librustc_infer/infer/canonical/query_response.rs

+1-45
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::infer::canonical::{
1414
};
1515
use crate::infer::nll_relate::{NormalizationStrategy, TypeRelating, TypeRelatingDelegate};
1616
use crate::infer::region_constraints::{Constraint, RegionConstraintData};
17-
use crate::infer::{InferCtxt, InferCtxtBuilder, InferOk, InferResult, NLLRegionVariableOrigin};
17+
use crate::infer::{InferCtxt, InferOk, InferResult, NLLRegionVariableOrigin};
1818
use crate::traits::query::{Fallible, NoSolution};
1919
use crate::traits::{DomainGoal, TraitEngine};
2020
use crate::traits::{Obligation, ObligationCause, PredicateObligation};
@@ -26,52 +26,8 @@ use rustc::ty::{self, BoundVar, Ty, TyCtxt};
2626
use rustc_data_structures::captures::Captures;
2727
use rustc_index::vec::Idx;
2828
use rustc_index::vec::IndexVec;
29-
use rustc_span::DUMMY_SP;
3029
use std::fmt::Debug;
3130

32-
impl<'tcx> InferCtxtBuilder<'tcx> {
33-
/// The "main method" for a canonicalized trait query. Given the
34-
/// canonical key `canonical_key`, this method will create a new
35-
/// inference context, instantiate the key, and run your operation
36-
/// `op`. The operation should yield up a result (of type `R`) as
37-
/// well as a set of trait obligations that must be fully
38-
/// satisfied. These obligations will be processed and the
39-
/// canonical result created.
40-
///
41-
/// Returns `NoSolution` in the event of any error.
42-
///
43-
/// (It might be mildly nicer to implement this on `TyCtxt`, and
44-
/// not `InferCtxtBuilder`, but that is a bit tricky right now.
45-
/// In part because we would need a `for<'tcx>` sort of
46-
/// bound for the closure and in part because it is convenient to
47-
/// have `'tcx` be free on this function so that we can talk about
48-
/// `K: TypeFoldable<'tcx>`.)
49-
pub fn enter_canonical_trait_query<K, R>(
50-
&mut self,
51-
canonical_key: &Canonical<'tcx, K>,
52-
operation: impl FnOnce(&InferCtxt<'_, 'tcx>, &mut dyn TraitEngine<'tcx>, K) -> Fallible<R>,
53-
) -> Fallible<CanonicalizedQueryResponse<'tcx, R>>
54-
where
55-
K: TypeFoldable<'tcx>,
56-
R: Debug + TypeFoldable<'tcx>,
57-
Canonical<'tcx, QueryResponse<'tcx, R>>: ArenaAllocatable,
58-
{
59-
self.enter_with_canonical(
60-
DUMMY_SP,
61-
canonical_key,
62-
|ref infcx, key, canonical_inference_vars| {
63-
let mut fulfill_cx = TraitEngine::new(infcx.tcx);
64-
let value = operation(infcx, &mut *fulfill_cx, key)?;
65-
infcx.make_canonicalized_query_response(
66-
canonical_inference_vars,
67-
value,
68-
&mut *fulfill_cx,
69-
)
70-
},
71-
)
72-
}
73-
}
74-
7531
impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> {
7632
/// This method is meant to be invoked as the final step of a canonical query
7733
/// implementation. It is given:

src/librustc_infer/infer/error_reporting/mod.rs

+82-3
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ use super::lexical_region_resolve::RegionResolutionError;
4949
use super::region_constraints::GenericKind;
5050
use super::{InferCtxt, RegionVariableOrigin, SubregionOrigin, TypeTrace, ValuePairs};
5151

52-
use crate::infer::opaque_types;
5352
use crate::infer::{self, SuppressRegionErrors};
5453
use crate::traits::error_reporting::report_object_safety_error;
5554
use crate::traits::{
@@ -288,6 +287,86 @@ fn explain_span(tcx: TyCtxt<'tcx>, heading: &str, span: Span) -> (String, Option
288287
(format!("the {} at {}:{}", heading, lo.line, lo.col.to_usize() + 1), Some(span))
289288
}
290289

290+
pub fn unexpected_hidden_region_diagnostic(
291+
tcx: TyCtxt<'tcx>,
292+
region_scope_tree: Option<&region::ScopeTree>,
293+
span: Span,
294+
hidden_ty: Ty<'tcx>,
295+
hidden_region: ty::Region<'tcx>,
296+
) -> DiagnosticBuilder<'tcx> {
297+
let mut err = struct_span_err!(
298+
tcx.sess,
299+
span,
300+
E0700,
301+
"hidden type for `impl Trait` captures lifetime that does not appear in bounds",
302+
);
303+
304+
// Explain the region we are capturing.
305+
if let ty::ReEarlyBound(_) | ty::ReFree(_) | ty::ReStatic | ty::ReEmpty(_) = hidden_region {
306+
// Assuming regionck succeeded (*), we ought to always be
307+
// capturing *some* region from the fn header, and hence it
308+
// ought to be free. So under normal circumstances, we will go
309+
// down this path which gives a decent human readable
310+
// explanation.
311+
//
312+
// (*) if not, the `tainted_by_errors` flag would be set to
313+
// true in any case, so we wouldn't be here at all.
314+
note_and_explain_free_region(
315+
tcx,
316+
&mut err,
317+
&format!("hidden type `{}` captures ", hidden_ty),
318+
hidden_region,
319+
"",
320+
);
321+
} else {
322+
// Ugh. This is a painful case: the hidden region is not one
323+
// that we can easily summarize or explain. This can happen
324+
// in a case like
325+
// `src/test/ui/multiple-lifetimes/ordinary-bounds-unsuited.rs`:
326+
//
327+
// ```
328+
// fn upper_bounds<'a, 'b>(a: Ordinary<'a>, b: Ordinary<'b>) -> impl Trait<'a, 'b> {
329+
// if condition() { a } else { b }
330+
// }
331+
// ```
332+
//
333+
// Here the captured lifetime is the intersection of `'a` and
334+
// `'b`, which we can't quite express.
335+
336+
if let Some(region_scope_tree) = region_scope_tree {
337+
// If the `region_scope_tree` is available, this is being
338+
// invoked from the "region inferencer error". We can at
339+
// least report a really cryptic error for now.
340+
note_and_explain_region(
341+
tcx,
342+
region_scope_tree,
343+
&mut err,
344+
&format!("hidden type `{}` captures ", hidden_ty),
345+
hidden_region,
346+
"",
347+
);
348+
} else {
349+
// If the `region_scope_tree` is *unavailable*, this is
350+
// being invoked by the code that comes *after* region
351+
// inferencing. This is a bug, as the region inferencer
352+
// ought to have noticed the failed constraint and invoked
353+
// error reporting, which in turn should have prevented us
354+
// from getting trying to infer the hidden type
355+
// completely.
356+
tcx.sess.delay_span_bug(
357+
span,
358+
&format!(
359+
"hidden type captures unexpected lifetime `{:?}` \
360+
but no region inference failure",
361+
hidden_region,
362+
),
363+
);
364+
}
365+
}
366+
367+
err
368+
}
369+
291370
impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
292371
pub fn report_region_errors(
293372
&self,
@@ -410,7 +489,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
410489
span,
411490
} => {
412491
let hidden_ty = self.resolve_vars_if_possible(&hidden_ty);
413-
opaque_types::unexpected_hidden_region_diagnostic(
492+
unexpected_hidden_region_diagnostic(
414493
self.tcx,
415494
Some(region_scope_tree),
416495
span,
@@ -2077,7 +2156,7 @@ impl<'tcx> ObligationCauseExt<'tcx> for ObligationCause<'tcx> {
20772156
/// This is a bare signal of what kind of type we're dealing with. `ty::TyKind` tracks
20782157
/// extra information about each type, but we only care about the category.
20792158
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
2080-
crate enum TyCategory {
2159+
pub enum TyCategory {
20812160
Closure,
20822161
Opaque,
20832162
Generator,

0 commit comments

Comments
 (0)