Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade Chalk to 0.21 #75173

Merged
merged 5 commits into from
Sep 5, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 52 additions & 9 deletions Cargo.lock
Original file line number Diff line number Diff line change
@@ -427,9 +427,9 @@ dependencies = [

[[package]]
name = "chalk-derive"
version = "0.14.0"
version = "0.21.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d463e01905d607e181de72e8608721d3269f29176c9a14ce037011316ae7131d"
checksum = "c1df0dbb57d74b4acd20f20fa66ab2acd09776b79eaeb9d8f947b2f3e01c40bf"
dependencies = [
"proc-macro2",
"quote",
@@ -439,40 +439,42 @@ dependencies = [

[[package]]
name = "chalk-engine"
version = "0.14.0"
version = "0.21.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "efaf428f5398d36284f79690cf988762b7c091249f50a6c11db613a46c057000"
checksum = "fb7c65a13f32f02aba8f1d9a37f206af615f77ac564624b81a4c593c6c1735b9"
dependencies = [
"chalk-derive",
"chalk-ir",
"chalk-solve",
"rustc-hash",
"tracing",
]

[[package]]
name = "chalk-ir"
version = "0.14.0"
version = "0.21.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd3fdc1e9f68498ffe80f4a23b0b95f1ca6fb21d5a4c9b0c085fab3ca712bdbe"
checksum = "44361a25dbdb1dc428f56ad7a3c21ba9ca12f3225c26a47919ff6fcb10a583d4"
dependencies = [
"chalk-derive",
"lazy_static",
]

[[package]]
name = "chalk-solve"
version = "0.14.0"
version = "0.21.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b9fd4102807b7ebe8fb034fa0f488c5656e1966d3261b558b81a08d519cdb29"
checksum = "a886da37a0dc457057d86f78f026f7a09c6d8088aa13f4f4127fdb8dc80119a3"
dependencies = [
"chalk-derive",
"chalk-engine",
"chalk-ir",
"ena",
"itertools 0.9.0",
"petgraph",
"rustc-hash",
"tracing",
"tracing-subscriber",
"tracing-tree",
]

[[package]]
@@ -4038,6 +4040,7 @@ dependencies = [
name = "rustc_traits"
version = "0.0.0"
dependencies = [
"chalk-engine",
"chalk-ir",
"chalk-solve",
"rustc_ast",
@@ -4996,21 +4999,61 @@ dependencies = [
"lazy_static",
]

[[package]]
name = "tracing-log"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5e0f8c7178e13481ff6765bd169b33e8d554c5d2bbede5e32c356194be02b9b9"
dependencies = [
"lazy_static",
"log",
"tracing-core",
]

[[package]]
name = "tracing-serde"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6ccba2f8f16e0ed268fc765d9b7ff22e965e7185d32f8f1ec8294fe17d86e79"
dependencies = [
"serde",
"tracing-core",
]

[[package]]
name = "tracing-subscriber"
version = "0.2.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "abd165311cc4d7a555ad11cc77a37756df836182db0d81aac908c8184c584f40"
dependencies = [
"ansi_term 0.12.1",
"chrono",
"lazy_static",
"matchers",
"parking_lot 0.11.0",
"regex",
"serde",
"serde_json",
"sharded-slab",
"smallvec 1.4.2",
"thread_local",
"tracing-core",
"tracing-log",
"tracing-serde",
]

[[package]]
name = "tracing-tree"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e1a3dc4774db3a6b2d66a4f8d8de670e874ec3ed55615860c994927419b32c5f"
dependencies = [
"ansi_term 0.12.1",
"atty",
"chrono",
"termcolor",
"tracing",
"tracing-subscriber",
]

[[package]]
2 changes: 1 addition & 1 deletion compiler/rustc_middle/Cargo.toml
Original file line number Diff line number Diff line change
@@ -27,7 +27,7 @@ rustc_serialize = { path = "../rustc_serialize" }
rustc_ast = { path = "../rustc_ast" }
rustc_span = { path = "../rustc_span" }
byteorder = { version = "1.3" }
chalk-ir = "0.14.0"
chalk-ir = "0.21.0"
smallvec = { version = "1.0", features = ["union", "may_dangle"] }
measureme = "0.7.1"
rustc_session = { path = "../rustc_session" }
53 changes: 51 additions & 2 deletions compiler/rustc_middle/src/traits/chalk.rs
Original file line number Diff line number Diff line change
@@ -75,6 +75,7 @@ impl<'tcx> chalk_ir::interner::Interner for RustInterner<'tcx> {
type InternedQuantifiedWhereClauses = Vec<chalk_ir::QuantifiedWhereClause<Self>>;
type InternedVariableKinds = Vec<chalk_ir::VariableKind<Self>>;
type InternedCanonicalVarKinds = Vec<chalk_ir::CanonicalVarKind<Self>>;
type InternedConstraints = Vec<chalk_ir::InEnvironment<chalk_ir::Constraint<Self>>>;
type DefId = DefId;
type InternedAdtId = &'tcx AdtDef;
type Identifier = ();
@@ -108,8 +109,42 @@ impl<'tcx> chalk_ir::interner::Interner for RustInterner<'tcx> {
application_ty: &chalk_ir::ApplicationTy<Self>,
fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
let chalk_ir::ApplicationTy { name, substitution } = application_ty;
Some(write!(fmt, "{:?}{:?}", name, chalk_ir::debug::Angle(substitution.interned())))
match application_ty.name {
chalk_ir::TypeName::Ref(mutbl) => {
let data = application_ty.substitution.interned();
match (&**data[0].interned(), &**data[1].interned()) {
(
chalk_ir::GenericArgData::Lifetime(lifetime),
chalk_ir::GenericArgData::Ty(ty),
) => Some(match mutbl {
chalk_ir::Mutability::Not => write!(fmt, "(&{:?} {:?})", lifetime, ty),
chalk_ir::Mutability::Mut => write!(fmt, "(&{:?} mut {:?})", lifetime, ty),
}),
_ => unreachable!(),
}
}
chalk_ir::TypeName::Array => {
let data = application_ty.substitution.interned();
match (&**data[0].interned(), &**data[1].interned()) {
(chalk_ir::GenericArgData::Ty(ty), chalk_ir::GenericArgData::Const(len)) => {
Some(write!(fmt, "[{:?}; {:?}]", ty, len))
}
_ => unreachable!(),
}
}
chalk_ir::TypeName::Slice => {
let data = application_ty.substitution.interned();
let ty = match &**data[0].interned() {
chalk_ir::GenericArgData::Ty(t) => t,
_ => unreachable!(),
};
Some(write!(fmt, "[{:?}]", ty))
}
_ => {
let chalk_ir::ApplicationTy { name, substitution } = application_ty;
Some(write!(fmt, "{:?}{:?}", name, chalk_ir::debug::Angle(substitution.interned())))
}
}
}

fn debug_substitution(
@@ -321,6 +356,20 @@ impl<'tcx> chalk_ir::interner::Interner for RustInterner<'tcx> {
) -> &'a [chalk_ir::CanonicalVarKind<Self>] {
canonical_var_kinds
}

fn intern_constraints<E>(
&self,
data: impl IntoIterator<Item = Result<chalk_ir::InEnvironment<chalk_ir::Constraint<Self>>, E>>,
) -> Result<Self::InternedConstraints, E> {
data.into_iter().collect::<Result<Vec<_>, _>>()
}

fn constraints_data<'a>(
&self,
constraints: &'a Self::InternedConstraints,
) -> &'a [chalk_ir::InEnvironment<chalk_ir::Constraint<Self>>] {
constraints
}
}

impl<'tcx> chalk_ir::interner::HasInterner for RustInterner<'tcx> {
5 changes: 3 additions & 2 deletions compiler/rustc_traits/Cargo.toml
Original file line number Diff line number Diff line change
@@ -12,8 +12,9 @@ rustc_hir = { path = "../rustc_hir" }
rustc_index = { path = "../rustc_index" }
rustc_ast = { path = "../rustc_ast" }
rustc_span = { path = "../rustc_span" }
chalk-ir = "0.14.0"
chalk-solve = "0.14.0"
chalk-ir = "0.21.0"
chalk-solve = "0.21.0"
chalk-engine = "0.21.0"
smallvec = { version = "1.0", features = ["union", "may_dangle"] }
rustc_infer = { path = "../rustc_infer" }
rustc_trait_selection = { path = "../rustc_trait_selection" }
343 changes: 171 additions & 172 deletions compiler/rustc_traits/src/chalk/db.rs

Large diffs are not rendered by default.

605 changes: 395 additions & 210 deletions compiler/rustc_traits/src/chalk/lowering.rs

Large diffs are not rendered by default.

147 changes: 39 additions & 108 deletions compiler/rustc_traits/src/chalk/mod.rs
Original file line number Diff line number Diff line change
@@ -15,17 +15,17 @@ use rustc_middle::infer::canonical::{CanonicalTyVarKind, CanonicalVarKind};
use rustc_middle::traits::ChalkRustInterner;
use rustc_middle::ty::query::Providers;
use rustc_middle::ty::subst::GenericArg;
use rustc_middle::ty::{
self, Bound, BoundVar, ParamTy, Region, RegionKind, Ty, TyCtxt, TypeFoldable,
};
use rustc_middle::ty::{self, BoundVar, ParamTy, TyCtxt, TypeFoldable};

use rustc_infer::infer::canonical::{
Canonical, CanonicalVarValues, Certainty, QueryRegionConstraints, QueryResponse,
};
use rustc_infer::traits::{self, ChalkCanonicalGoal};

use crate::chalk::db::RustIrDatabase as ChalkRustIrDatabase;
use crate::chalk::lowering::{LowerInto, ParamsSubstitutor};
use crate::chalk::lowering::{
LowerInto, ParamsSubstitutor, PlaceholdersCollector, RegionsSubstitutor,
};

use chalk_solve::Solution;

@@ -40,16 +40,35 @@ crate fn evaluate_goal<'tcx>(
let interner = ChalkRustInterner { tcx };

// Chalk doesn't have a notion of `Params`, so instead we use placeholders.
let mut params_substitutor = ParamsSubstitutor::new(tcx);
let mut placeholders_collector = PlaceholdersCollector::new();
obligation.visit_with(&mut placeholders_collector);

let restatic_placeholder = tcx.mk_region(ty::RegionKind::RePlaceholder(ty::Placeholder {
universe: ty::UniverseIndex::ROOT,
name: ty::BoundRegion::BrAnon(placeholders_collector.next_anon_region_placeholder),
}));
let reempty_placeholder = tcx.mk_region(ty::RegionKind::RePlaceholder(ty::Placeholder {
universe: ty::UniverseIndex::ROOT,
name: ty::BoundRegion::BrAnon(placeholders_collector.next_anon_region_placeholder + 1),
}));

let mut params_substitutor =
ParamsSubstitutor::new(tcx, placeholders_collector.next_ty_placeholder);
let obligation = obligation.fold_with(&mut params_substitutor);
// FIXME(chalk): we really should be substituting these back in the solution
let _params: FxHashMap<usize, ParamTy> = params_substitutor.params;

let mut regions_substitutor =
RegionsSubstitutor::new(tcx, restatic_placeholder, reempty_placeholder);
let obligation = obligation.fold_with(&mut regions_substitutor);

let max_universe = obligation.max_universe.index();

let _lowered_goal: chalk_ir::UCanonical<
let lowered_goal: chalk_ir::UCanonical<
chalk_ir::InEnvironment<chalk_ir::Goal<ChalkRustInterner<'tcx>>>,
> = chalk_ir::UCanonical {
canonical: chalk_ir::Canonical {
binders: chalk_ir::CanonicalVarKinds::from(
binders: chalk_ir::CanonicalVarKinds::from_iter(
&interner,
obligation.variables.iter().map(|v| match v.kind {
CanonicalVarKind::PlaceholderTy(_ty) => unimplemented!(),
@@ -81,108 +100,20 @@ crate fn evaluate_goal<'tcx>(
universes: max_universe + 1,
};

let solver_choice = chalk_solve::SolverChoice::SLG { max_size: 32, expected_answers: None };
let mut solver = solver_choice.into_solver::<ChalkRustInterner<'tcx>>();

let db = ChalkRustIrDatabase { tcx, interner };
let solution = solver.solve(&db, &_lowered_goal);
use chalk_solve::Solver;
let mut solver = chalk_engine::solve::SLGSolver::new(32, None);
let db = ChalkRustIrDatabase { interner, restatic_placeholder, reempty_placeholder };
let solution = chalk_solve::logging::with_tracing_logs(|| solver.solve(&db, &lowered_goal));

// Ideally, the code to convert *back* to rustc types would live close to
// the code to convert *from* rustc types. Right now though, we don't
// really need this and so it's really minimal.
// Right now, we also treat a `Unique` solution the same as
// `Ambig(Definite)`. This really isn't right.
let make_solution = |_subst: chalk_ir::Substitution<_>| {
let make_solution = |subst: chalk_ir::Substitution<_>| {
let mut var_values: IndexVec<BoundVar, GenericArg<'tcx>> = IndexVec::new();
_subst.parameters(&interner).iter().for_each(|p| {
// FIXME(chalk): we should move this elsewhere, since this is
// essentially inverse of lowering a `GenericArg`.
let _data = p.data(&interner);
match _data {
chalk_ir::GenericArgData::Ty(_t) => {
use chalk_ir::TyData;
use rustc_ast as ast;

let _data = _t.data(&interner);
let kind = match _data {
TyData::Apply(_application_ty) => match _application_ty.name {
chalk_ir::TypeName::Adt(_struct_id) => unimplemented!(),
chalk_ir::TypeName::Scalar(scalar) => match scalar {
chalk_ir::Scalar::Bool => ty::Bool,
chalk_ir::Scalar::Char => ty::Char,
chalk_ir::Scalar::Int(int_ty) => match int_ty {
chalk_ir::IntTy::Isize => ty::Int(ast::IntTy::Isize),
chalk_ir::IntTy::I8 => ty::Int(ast::IntTy::I8),
chalk_ir::IntTy::I16 => ty::Int(ast::IntTy::I16),
chalk_ir::IntTy::I32 => ty::Int(ast::IntTy::I32),
chalk_ir::IntTy::I64 => ty::Int(ast::IntTy::I64),
chalk_ir::IntTy::I128 => ty::Int(ast::IntTy::I128),
},
chalk_ir::Scalar::Uint(int_ty) => match int_ty {
chalk_ir::UintTy::Usize => ty::Uint(ast::UintTy::Usize),
chalk_ir::UintTy::U8 => ty::Uint(ast::UintTy::U8),
chalk_ir::UintTy::U16 => ty::Uint(ast::UintTy::U16),
chalk_ir::UintTy::U32 => ty::Uint(ast::UintTy::U32),
chalk_ir::UintTy::U64 => ty::Uint(ast::UintTy::U64),
chalk_ir::UintTy::U128 => ty::Uint(ast::UintTy::U128),
},
chalk_ir::Scalar::Float(float_ty) => match float_ty {
chalk_ir::FloatTy::F32 => ty::Float(ast::FloatTy::F32),
chalk_ir::FloatTy::F64 => ty::Float(ast::FloatTy::F64),
},
},
chalk_ir::TypeName::Array => unimplemented!(),
chalk_ir::TypeName::FnDef(_) => unimplemented!(),
chalk_ir::TypeName::Closure(_) => unimplemented!(),
chalk_ir::TypeName::Never => unimplemented!(),
chalk_ir::TypeName::Tuple(_size) => unimplemented!(),
chalk_ir::TypeName::Slice => unimplemented!(),
chalk_ir::TypeName::Raw(_) => unimplemented!(),
chalk_ir::TypeName::Ref(_) => unimplemented!(),
chalk_ir::TypeName::Str => unimplemented!(),
chalk_ir::TypeName::OpaqueType(_ty) => unimplemented!(),
chalk_ir::TypeName::AssociatedType(_assoc_ty) => unimplemented!(),
chalk_ir::TypeName::Error => unimplemented!(),
},
TyData::Placeholder(_placeholder) => {
unimplemented!();
}
TyData::Alias(_alias_ty) => unimplemented!(),
TyData::Function(_quantified_ty) => unimplemented!(),
TyData::BoundVar(_bound) => Bound(
ty::DebruijnIndex::from_usize(_bound.debruijn.depth() as usize),
ty::BoundTy {
var: ty::BoundVar::from_usize(_bound.index),
kind: ty::BoundTyKind::Anon,
},
),
TyData::InferenceVar(_, _) => unimplemented!(),
TyData::Dyn(_) => unimplemented!(),
};
let _ty: Ty<'_> = tcx.mk_ty(kind);
let _arg: GenericArg<'_> = _ty.into();
var_values.push(_arg);
}
chalk_ir::GenericArgData::Lifetime(_l) => {
let _data = _l.data(&interner);
let _lifetime: Region<'_> = match _data {
chalk_ir::LifetimeData::BoundVar(_var) => {
tcx.mk_region(RegionKind::ReLateBound(
rustc_middle::ty::DebruijnIndex::from_usize(
_var.debruijn.depth() as usize
),
rustc_middle::ty::BoundRegion::BrAnon(_var.index as u32),
))
}
chalk_ir::LifetimeData::InferenceVar(_var) => unimplemented!(),
chalk_ir::LifetimeData::Placeholder(_index) => unimplemented!(),
chalk_ir::LifetimeData::Phantom(_, _) => unimplemented!(),
};
let _arg: GenericArg<'_> = _lifetime.into();
var_values.push(_arg);
}
chalk_ir::GenericArgData::Const(_) => unimplemented!(),
}
subst.as_slice(&interner).iter().for_each(|p| {
var_values.push(p.lower_into(&interner));
});
let sol = Canonical {
max_universe: ty::UniverseIndex::from_usize(0),
@@ -194,17 +125,17 @@ crate fn evaluate_goal<'tcx>(
value: (),
},
};
&*tcx.arena.alloc(sol)
tcx.arena.alloc(sol)
};
solution
.map(|s| match s {
Solution::Unique(_subst) => {
Solution::Unique(subst) => {
// FIXME(chalk): handle constraints
make_solution(_subst.value.subst)
make_solution(subst.value.subst)
}
Solution::Ambig(_guidance) => {
match _guidance {
chalk_solve::Guidance::Definite(_subst) => make_solution(_subst.value),
Solution::Ambig(guidance) => {
match guidance {
chalk_solve::Guidance::Definite(subst) => make_solution(subst.value),
chalk_solve::Guidance::Suggested(_) => unimplemented!(),
chalk_solve::Guidance::Unknown => {
// chalk_fulfill doesn't use the var_values here, so
2 changes: 1 addition & 1 deletion src/test/ui/chalkify/type_inference.rs
Original file line number Diff line number Diff line change
@@ -24,5 +24,5 @@ fn main() {

// Here we have two solutions so we get back the behavior of the old-style
// trait solver.
only_bar(x); //~ ERROR the trait bound `f64: Bar` is not satisfied
only_bar(x); //~ ERROR the trait bound `{float}: Bar` is not satisfied
}
8 changes: 6 additions & 2 deletions src/test/ui/chalkify/type_inference.stderr
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
error[E0277]: the trait bound `f64: Bar` is not satisfied
error[E0277]: the trait bound `{float}: Bar` is not satisfied
--> $DIR/type_inference.rs:27:5
|
LL | fn only_bar<T: Bar>(_x: T) { }
| --- required by this bound in `only_bar`
...
LL | only_bar(x);
| ^^^^^^^^ the trait `Bar` is not implemented for `f64`
| ^^^^^^^^ the trait `Bar` is not implemented for `{float}`
|
= help: the following implementations were found:
<i32 as Bar>
<u32 as Bar>

error: aborting due to previous error

2 changes: 1 addition & 1 deletion src/test/ui/chalkify/type_wf.rs
Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@ fn main() {
x: 5,
};

let s = S { //~ ERROR the trait bound `f64: Foo` is not satisfied
let s = S { //~ ERROR the trait bound `{float}: Foo` is not satisfied
x: 5.0,
};

8 changes: 6 additions & 2 deletions src/test/ui/chalkify/type_wf.stderr
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
error[E0277]: the trait bound `f64: Foo` is not satisfied
error[E0277]: the trait bound `{float}: Foo` is not satisfied
--> $DIR/type_wf.rs:18:13
|
LL | struct S<T: Foo> {
| ---------------- required by `S`
...
LL | let s = S {
| ^ the trait `Foo` is not implemented for `f64`
| ^ the trait `Foo` is not implemented for `{float}`
|
= help: the following implementations were found:
<Option<T> as Foo>
<i32 as Foo>

error: aborting due to previous error