Skip to content

Commit fb58b7b

Browse files
committed
Add compare-mode=chalk and add a little bit more implementations and fixmes
1 parent f9fdf64 commit fb58b7b

File tree

10 files changed

+91
-35
lines changed

10 files changed

+91
-35
lines changed

src/librustc_middle/traits/chalk.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,10 @@ pub enum RustDefId {
3232
RawPtr,
3333

3434
Trait(DefId),
35-
3635
Impl(DefId),
37-
3836
FnDef(DefId),
39-
4037
AssocTy(DefId),
38+
Opaque(DefId),
4139
}
4240

4341
#[derive(Copy, Clone)]

src/librustc_trait_selection/traits/chalk_fulfill.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@ fn environment<'tcx>(
8787
NodeKind::TraitImpl => {
8888
let trait_ref = tcx.impl_trait_ref(def_id).expect("not an impl");
8989

90-
inputs.extend(trait_ref.substs.iter().flat_map(|arg| arg.walk()));
90+
// FIXME(chalk): this has problems because of late-bound regions
91+
//inputs.extend(trait_ref.substs.iter().flat_map(|arg| arg.walk()));
92+
inputs.extend(trait_ref.substs.iter());
9193
}
9294

9395
// In an inherent impl, we assume that the receiver type and all its
@@ -136,6 +138,8 @@ fn in_environment(
136138
let environment = match obligation.param_env.def_id {
137139
Some(def_id) => environment(infcx.tcx, def_id),
138140
None if obligation.param_env.caller_bounds.is_empty() => ty::List::empty(),
141+
// FIXME(chalk): this is hit in ui/where-clauses/where-clause-constraints-are-local-for-trait-impl
142+
// and ui/generics/generic-static-methods
139143
_ => bug!("non-empty `ParamEnv` with no def-id"),
140144
};
141145

src/librustc_traits/chalk/db.rs

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ impl<'tcx> chalk_solve::RustIrDatabase<RustInterner<'tcx>> for RustIrDatabase<'t
168168
});
169169
struct_datum
170170
}
171-
RustDefId::Ref(_) => Arc::new(chalk_rust_ir::StructDatum {
171+
RustDefId::Ref(_) | RustDefId::RawPtr => Arc::new(chalk_rust_ir::StructDatum {
172172
id: struct_id,
173173
binders: chalk_ir::Binders::new(
174174
chalk_ir::ParameterKinds::from(
@@ -204,7 +204,7 @@ impl<'tcx> chalk_solve::RustIrDatabase<RustInterner<'tcx>> for RustIrDatabase<'t
204204
})
205205
}
206206

207-
_ => bug!("Used not struct variant when expecting struct variant."),
207+
v => bug!("Used not struct variant ({:?}) when expecting struct variant.", v),
208208
}
209209
}
210210

@@ -283,6 +283,17 @@ impl<'tcx> chalk_solve::RustIrDatabase<RustInterner<'tcx>> for RustIrDatabase<'t
283283
RustDefId::Trait(def_id) => def_id,
284284
_ => bug!("Did not use `Trait` variant when expecting trait."),
285285
};
286+
// FIXME(chalk): this match can be removed when builtin types supported
287+
match struct_id.0 {
288+
RustDefId::Adt(_) => {}
289+
RustDefId::Str => return false,
290+
RustDefId::Never => return false,
291+
RustDefId::Slice => return false,
292+
RustDefId::Array => return false,
293+
RustDefId::Ref(_) => return false,
294+
RustDefId::RawPtr => return false,
295+
_ => bug!("Did not use `Adt` variant when expecting adt."),
296+
}
286297
let adt_def_id: DefId = match struct_id.0 {
287298
RustDefId::Adt(def_id) => def_id,
288299
_ => bug!("Did not use `Adt` variant when expecting adt."),
@@ -347,9 +358,19 @@ impl<'tcx> chalk_solve::RustIrDatabase<RustInterner<'tcx>> for RustIrDatabase<'t
347358

348359
fn opaque_ty_data(
349360
&self,
350-
_id: chalk_ir::OpaqueTyId<RustInterner<'tcx>>,
361+
opaque_ty_id: chalk_ir::OpaqueTyId<RustInterner<'tcx>>,
351362
) -> Arc<chalk_rust_ir::OpaqueTyDatum<RustInterner<'tcx>>> {
352-
unimplemented!()
363+
// FIXME(chalk): actually lower opaque ty
364+
let hidden_ty =
365+
self.tcx.mk_ty(ty::Tuple(self.tcx.intern_substs(&[]))).lower_into(&self.interner);
366+
let value = chalk_rust_ir::OpaqueTyDatumBound {
367+
hidden_ty,
368+
bounds: chalk_ir::Binders::new(chalk_ir::ParameterKinds::new(&self.interner), vec![]),
369+
};
370+
Arc::new(chalk_rust_ir::OpaqueTyDatum {
371+
opaque_ty_id,
372+
bound: chalk_ir::Binders::new(chalk_ir::ParameterKinds::new(&self.interner), value),
373+
})
353374
}
354375

355376
/// Since Chalk can't handle all Rust types currently, we have to handle
@@ -386,7 +407,7 @@ impl<'tcx> chalk_solve::RustIrDatabase<RustInterner<'tcx>> for RustIrDatabase<'t
386407

387408
Str | Slice => Some(false),
388409

389-
Trait(_) | Impl(_) | AssocTy(_) => panic!(),
410+
Trait(_) | Impl(_) | AssocTy(_) | Opaque(_) => panic!(),
390411
}
391412
}
392413
_ => None,
@@ -416,7 +437,7 @@ impl<'tcx> chalk_solve::RustIrDatabase<RustInterner<'tcx>> for RustIrDatabase<'t
416437
}
417438
}
418439
}
419-
Trait(_) | Impl(_) | AssocTy(_) => panic!(),
440+
Trait(_) | Impl(_) | AssocTy(_) | Opaque(_) => panic!(),
420441
}
421442
}
422443
_ => None,

src/librustc_traits/chalk/lowering.rs

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,11 @@ impl<'tcx> LowerInto<'tcx, chalk_ir::Ty<RustInterner<'tcx>>> for Ty<'tcx> {
352352
})
353353
.intern(interner)
354354
}
355-
Dynamic(_, _) => unimplemented!(),
355+
// FIXME(chalk): add region
356+
Dynamic(predicates, _region) => {
357+
TyData::Dyn(chalk_ir::DynTy { bounds: predicates.lower_into(interner) })
358+
.intern(interner)
359+
}
356360
Closure(_def_id, _) => unimplemented!(),
357361
Generator(_def_id, _substs, _) => unimplemented!(),
358362
GeneratorWitness(_) => unimplemented!(),
@@ -361,7 +365,13 @@ impl<'tcx> LowerInto<'tcx, chalk_ir::Ty<RustInterner<'tcx>>> for Ty<'tcx> {
361365
apply(chalk_ir::TypeName::Tuple(substs.len()), substs.lower_into(interner))
362366
}
363367
Projection(proj) => TyData::Alias(proj.lower_into(interner)).intern(interner),
364-
Opaque(_def_id, _substs) => unimplemented!(),
368+
Opaque(def_id, substs) => {
369+
TyData::Alias(chalk_ir::AliasTy::Opaque(chalk_ir::OpaqueTy {
370+
opaque_ty_id: chalk_ir::OpaqueTyId(RustDefId::Opaque(def_id)),
371+
substitution: substs.lower_into(interner),
372+
}))
373+
.intern(interner)
374+
}
365375
// This should have been done eagerly prior to this, and all Params
366376
// should have been substituted to placeholders
367377
Param(_) => panic!("Lowering Param when not expected."),
@@ -376,7 +386,7 @@ impl<'tcx> LowerInto<'tcx, chalk_ir::Ty<RustInterner<'tcx>>> for Ty<'tcx> {
376386
})
377387
.intern(interner),
378388
Infer(_infer) => unimplemented!(),
379-
Error => unimplemented!(),
389+
Error => apply(chalk_ir::TypeName::Error, empty()),
380390
}
381391
}
382392
}
@@ -401,6 +411,7 @@ impl<'tcx> LowerInto<'tcx, chalk_ir::Lifetime<RustInterner<'tcx>>> for Region<'t
401411
ty::BrEnv => unimplemented!(),
402412
},
403413
ReFree(_) => unimplemented!(),
414+
// FIXME(chalk): need to handle ReStatic
404415
ReStatic => unimplemented!(),
405416
ReVar(_) => unimplemented!(),
406417
RePlaceholder(placeholder_region) => {
@@ -411,6 +422,7 @@ impl<'tcx> LowerInto<'tcx, chalk_ir::Lifetime<RustInterner<'tcx>>> for Region<'t
411422
.intern(interner)
412423
}
413424
ReEmpty(_) => unimplemented!(),
425+
// FIXME(chalk): need to handle ReErased
414426
ReErased => unimplemented!(),
415427
}
416428
}
@@ -472,6 +484,39 @@ impl<'tcx> LowerInto<'tcx, Option<chalk_ir::QuantifiedWhereClause<RustInterner<'
472484
}
473485
}
474486

487+
impl<'tcx> LowerInto<'tcx, chalk_ir::Binders<chalk_ir::QuantifiedWhereClauses<RustInterner<'tcx>>>>
488+
for Binder<&'tcx ty::List<ty::ExistentialPredicate<'tcx>>>
489+
{
490+
fn lower_into(
491+
self,
492+
interner: &RustInterner<'tcx>,
493+
) -> chalk_ir::Binders<chalk_ir::QuantifiedWhereClauses<RustInterner<'tcx>>> {
494+
let (predicates, binders, _named_regions) =
495+
collect_bound_vars(interner, interner.tcx, &self);
496+
let where_clauses = predicates.into_iter().map(|predicate| match predicate {
497+
ty::ExistentialPredicate::Trait(ty::ExistentialTraitRef { def_id, substs }) => {
498+
chalk_ir::Binders::new(
499+
chalk_ir::ParameterKinds::new(interner),
500+
chalk_ir::WhereClause::Implemented(chalk_ir::TraitRef {
501+
trait_id: chalk_ir::TraitId(RustDefId::Trait(*def_id)),
502+
substitution: substs.lower_into(interner),
503+
}),
504+
)
505+
}
506+
ty::ExistentialPredicate::Projection(_predicate) => unimplemented!(),
507+
ty::ExistentialPredicate::AutoTrait(def_id) => chalk_ir::Binders::new(
508+
chalk_ir::ParameterKinds::new(interner),
509+
chalk_ir::WhereClause::Implemented(chalk_ir::TraitRef {
510+
trait_id: chalk_ir::TraitId(RustDefId::Trait(*def_id)),
511+
substitution: chalk_ir::Substitution::empty(interner),
512+
}),
513+
),
514+
});
515+
let value = chalk_ir::QuantifiedWhereClauses::from(interner, where_clauses);
516+
chalk_ir::Binders::new(binders, value)
517+
}
518+
}
519+
475520
/// To collect bound vars, we have to do two passes. In the first pass, we
476521
/// collect all `BoundRegion`s and `ty::Bound`s. In the second pass, we then
477522
/// replace `BrNamed` into `BrAnon`. The two separate passes are important,

src/test/ui/coherence/coherence-subtyping.re.stderr

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/test/ui/coherence/coherence-subtyping.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
// Note: This scenario is currently accepted, but as part of the
55
// universe transition (#56105) may eventually become an error.
66

7-
// revisions: old re
87
// check-pass
98

109
trait TheTrait {
@@ -14,10 +13,8 @@ trait TheTrait {
1413
impl TheTrait for for<'a, 'b> fn(&'a u8, &'b u8) -> &'a u8 {}
1514

1615
impl TheTrait for for<'a> fn(&'a u8, &'a u8) -> &'a u8 {
17-
//[re]~^ WARNING conflicting implementation
18-
//[re]~^^ WARNING this was previously accepted by the compiler but is being phased out
19-
//[old]~^^^ WARNING conflicting implementation
20-
//[old]~^^^^ WARNING this was previously accepted by the compiler but is being phased out
16+
//~^ WARNING conflicting implementation
17+
//~^^ WARNING this was previously accepted by the compiler but is being phased out
2118
}
2219

2320
fn main() {}

src/test/ui/coherence/coherence-subtyping.old.stderr renamed to src/test/ui/coherence/coherence-subtyping.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
warning: conflicting implementations of trait `TheTrait` for type `for<'a, 'b> fn(&'a u8, &'b u8) -> &'a u8`:
2-
--> $DIR/coherence-subtyping.rs:16:1
2+
--> $DIR/coherence-subtyping.rs:15:1
33
|
44
LL | impl TheTrait for for<'a, 'b> fn(&'a u8, &'b u8) -> &'a u8 {}
55
| ---------------------------------------------------------- first implementation here

src/tools/compiletest/src/common.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,20 +123,23 @@ pub enum FailMode {
123123
pub enum CompareMode {
124124
Nll,
125125
Polonius,
126+
Chalk,
126127
}
127128

128129
impl CompareMode {
129130
pub(crate) fn to_str(&self) -> &'static str {
130131
match *self {
131132
CompareMode::Nll => "nll",
132133
CompareMode::Polonius => "polonius",
134+
CompareMode::Chalk => "chalk",
133135
}
134136
}
135137

136138
pub fn parse(s: String) -> CompareMode {
137139
match s.as_str() {
138140
"nll" => CompareMode::Nll,
139141
"polonius" => CompareMode::Polonius,
142+
"chalk" => CompareMode::Chalk,
140143
x => panic!("unknown --compare-mode option: {}", x),
141144
}
142145
}

src/tools/compiletest/src/header.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,7 @@ impl Config {
857857
match self.compare_mode {
858858
Some(CompareMode::Nll) => name == "compare-mode-nll",
859859
Some(CompareMode::Polonius) => name == "compare-mode-polonius",
860+
Some(CompareMode::Chalk) => name == "compare-mode-chalk",
860861
None => false,
861862
} ||
862863
(cfg!(debug_assertions) && name == "debug") ||

src/tools/compiletest/src/runtest.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1962,6 +1962,9 @@ impl<'test> TestCx<'test> {
19621962
Some(CompareMode::Polonius) => {
19631963
rustc.args(&["-Zpolonius", "-Zborrowck=mir"]);
19641964
}
1965+
Some(CompareMode::Chalk) => {
1966+
rustc.args(&["-Zchalk"]);
1967+
}
19651968
None => {}
19661969
}
19671970

0 commit comments

Comments
 (0)