Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 3510805

Browse files
committed
Provide program clauses for builtin types
1 parent 79ba84f commit 3510805

File tree

7 files changed

+512
-229
lines changed

7 files changed

+512
-229
lines changed

src/Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2419,6 +2419,7 @@ dependencies = [
24192419
"log 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)",
24202420
"rustc 0.0.0",
24212421
"rustc_data_structures 0.0.0",
2422+
"rustc_target 0.0.0",
24222423
"smallvec 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)",
24232424
"syntax 0.0.0",
24242425
"syntax_pos 0.0.0",

src/librustc_traits/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ graphviz = { path = "../libgraphviz" }
1414
log = { version = "0.4" }
1515
rustc = { path = "../librustc" }
1616
rustc_data_structures = { path = "../librustc_data_structures" }
17+
rustc_target = { path = "../librustc_target" }
1718
syntax = { path = "../libsyntax" }
1819
syntax_pos = { path = "../libsyntax_pos" }
1920
chalk-engine = { version = "0.8.0", default-features=false }

src/librustc_traits/chalk_context.rs renamed to src/librustc_traits/chalk_context/mod.rs

Lines changed: 3 additions & 226 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,28 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
mod program_clauses;
12+
1113
use chalk_engine::fallible::Fallible as ChalkEngineFallible;
1214
use chalk_engine::{context, hh::HhGoal, DelayedLiteral, ExClause};
1315
use rustc::infer::canonical::{
1416
Canonical, CanonicalVarValues, OriginalQueryValues, QueryRegionConstraint, QueryResponse,
1517
};
1618
use rustc::infer::{InferCtxt, InferOk, LateBoundRegionConversionTime};
1719
use rustc::traits::{
18-
WellFormed,
19-
FromEnv,
2020
DomainGoal,
2121
ExClauseFold,
2222
ExClauseLift,
2323
Goal,
2424
GoalKind,
2525
Clause,
26-
ProgramClauseCategory,
2726
QuantifierKind,
2827
Environment,
2928
InEnvironment,
3029
};
3130
use rustc::ty::fold::{TypeFoldable, TypeFolder, TypeVisitor};
3231
use rustc::ty::subst::Kind;
3332
use rustc::ty::{self, TyCtxt};
34-
use rustc::hir::def_id::DefId;
3533

3634
use std::fmt::{self, Debug};
3735
use std::marker::PhantomData;
@@ -335,228 +333,7 @@ impl context::UnificationOps<ChalkArenas<'gcx>, ChalkArenas<'tcx>>
335333
environment: &Environment<'tcx>,
336334
goal: &DomainGoal<'tcx>,
337335
) -> Vec<Clause<'tcx>> {
338-
use rustc::traits::WhereClause::*;
339-
340-
fn assemble_clauses_from_impls<'tcx>(
341-
tcx: ty::TyCtxt<'_, '_, 'tcx>,
342-
trait_def_id: DefId,
343-
clauses: &mut Vec<Clause<'tcx>>
344-
) {
345-
tcx.for_each_impl(trait_def_id, |impl_def_id| {
346-
clauses.extend(
347-
tcx.program_clauses_for(impl_def_id)
348-
.into_iter()
349-
.cloned()
350-
);
351-
});
352-
}
353-
354-
fn assemble_clauses_from_assoc_ty_values<'tcx>(
355-
tcx: ty::TyCtxt<'_, '_, 'tcx>,
356-
trait_def_id: DefId,
357-
clauses: &mut Vec<Clause<'tcx>>
358-
) {
359-
tcx.for_each_impl(trait_def_id, |impl_def_id| {
360-
for def_id in tcx.associated_item_def_ids(impl_def_id).iter() {
361-
clauses.extend(
362-
tcx.program_clauses_for(*def_id)
363-
.into_iter()
364-
.cloned()
365-
);
366-
}
367-
});
368-
}
369-
370-
let mut clauses = match goal {
371-
DomainGoal::Holds(Implemented(trait_predicate)) => {
372-
// These come from:
373-
// * implementations of the trait itself (rule `Implemented-From-Impl`)
374-
// * the trait decl (rule `Implemented-From-Env`)
375-
376-
let mut clauses = vec![];
377-
assemble_clauses_from_impls(
378-
self.infcx.tcx,
379-
trait_predicate.def_id(),
380-
&mut clauses
381-
);
382-
383-
// FIXME: we need to add special rules for builtin impls:
384-
// * `Copy` / `Clone`
385-
// * `Sized`
386-
// * `Unsize`
387-
// * `Generator`
388-
// * `FnOnce` / `FnMut` / `Fn`
389-
// * trait objects
390-
// * auto traits
391-
392-
// Rule `Implemented-From-Env` will be computed from the environment.
393-
clauses
394-
}
395-
396-
DomainGoal::Holds(ProjectionEq(projection_predicate)) => {
397-
// These come from:
398-
// * the assoc type definition (rule `ProjectionEq-Placeholder`)
399-
// * normalization of the assoc ty values (rule `ProjectionEq-Normalize`)
400-
// * implied bounds from trait definitions (rule `Implied-Bound-From-Trait`)
401-
// * implied bounds from type definitions (rule `Implied-Bound-From-Type`)
402-
403-
let clauses = self.infcx.tcx.program_clauses_for(
404-
projection_predicate.projection_ty.item_def_id
405-
).into_iter()
406-
407-
// only select `ProjectionEq-Placeholder` and `ProjectionEq-Normalize`
408-
.filter(|clause| clause.category() == ProgramClauseCategory::Other)
409-
410-
.cloned()
411-
.collect::<Vec<_>>();
412-
413-
// Rules `Implied-Bound-From-Trait` and `Implied-Bound-From-Type` will be computed
414-
// from the environment.
415-
clauses
416-
}
417-
418-
DomainGoal::Holds(RegionOutlives(..)) => {
419-
// These come from:
420-
// * implied bounds from trait definitions (rule `Implied-Bound-From-Trait`)
421-
// * implied bounds from type definitions (rule `Implied-Bound-From-Type`)
422-
423-
// All of these rules are computed in the environment.
424-
vec![]
425-
}
426-
427-
DomainGoal::Holds(TypeOutlives(..)) => {
428-
// These come from:
429-
// * implied bounds from trait definitions (rule `Implied-Bound-From-Trait`)
430-
// * implied bounds from type definitions (rule `Implied-Bound-From-Type`)
431-
432-
// All of these rules are computed in the environment.
433-
vec![]
434-
}
435-
436-
DomainGoal::WellFormed(WellFormed::Trait(trait_predicate)) => {
437-
// These come from -- the trait decl (rule `WellFormed-TraitRef`).
438-
self.infcx.tcx.program_clauses_for(trait_predicate.def_id())
439-
.into_iter()
440-
441-
// only select `WellFormed-TraitRef`
442-
.filter(|clause| clause.category() == ProgramClauseCategory::WellFormed)
443-
444-
.cloned()
445-
.collect()
446-
}
447-
448-
DomainGoal::WellFormed(WellFormed::Ty(ty)) => {
449-
// These come from:
450-
// * the associated type definition if `ty` refers to an unnormalized
451-
// associated type (rule `WellFormed-AssocTy`)
452-
// * custom rules for built-in types
453-
// * the type definition otherwise (rule `WellFormed-Type`)
454-
let clauses = match ty.sty {
455-
ty::Projection(data) => {
456-
self.infcx.tcx.program_clauses_for(data.item_def_id)
457-
}
458-
459-
// These types are always WF (recall that we do not check
460-
// for parameters to be WF)
461-
ty::Bool |
462-
ty::Char |
463-
ty::Int(..) |
464-
ty::Uint(..) |
465-
ty::Float(..) |
466-
ty::Str |
467-
ty::RawPtr(..) |
468-
ty::FnPtr(..) |
469-
ty::Param(..) |
470-
ty::Never => {
471-
ty::List::empty()
472-
}
473-
474-
// WF if inner type is `Sized`
475-
ty::Slice(..) |
476-
ty::Array(..) => {
477-
ty::List::empty()
478-
}
479-
480-
ty::Tuple(..) => {
481-
ty::List::empty()
482-
}
483-
484-
// WF if `sub_ty` outlives `region`
485-
ty::Ref(..) => {
486-
ty::List::empty()
487-
}
488-
489-
ty::Dynamic(..) => {
490-
// FIXME: no rules yet for trait objects
491-
ty::List::empty()
492-
}
493-
494-
ty::Adt(def, ..) => {
495-
self.infcx.tcx.program_clauses_for(def.did)
496-
}
497-
498-
ty::Foreign(def_id) |
499-
ty::FnDef(def_id, ..) |
500-
ty::Closure(def_id, ..) |
501-
ty::Generator(def_id, ..) |
502-
ty::Opaque(def_id, ..) => {
503-
self.infcx.tcx.program_clauses_for(def_id)
504-
}
505-
506-
ty::GeneratorWitness(..) |
507-
ty::UnnormalizedProjection(..) |
508-
ty::Infer(..) |
509-
ty::Bound(..) |
510-
ty::Error => {
511-
bug!("unexpected type {:?}", ty)
512-
}
513-
};
514-
515-
clauses.into_iter()
516-
.filter(|clause| clause.category() == ProgramClauseCategory::WellFormed)
517-
.cloned()
518-
.collect()
519-
}
520-
521-
DomainGoal::FromEnv(FromEnv::Trait(..)) => {
522-
// These come from:
523-
// * implied bounds from trait definitions (rule `Implied-Bound-From-Trait`)
524-
// * implied bounds from type definitions (rule `Implied-Bound-From-Type`)
525-
// * implied bounds from assoc type defs (rules `Implied-Trait-From-AssocTy`,
526-
// `Implied-Bound-From-AssocTy` and `Implied-WC-From-AssocTy`)
527-
528-
// All of these rules are computed in the environment.
529-
vec![]
530-
}
531-
532-
DomainGoal::FromEnv(FromEnv::Ty(..)) => {
533-
// There are no `FromEnv::Ty(..) :- ...` rules (this predicate only
534-
// comes from the environment).
535-
vec![]
536-
}
537-
538-
DomainGoal::Normalize(projection_predicate) => {
539-
// These come from -- assoc ty values (rule `Normalize-From-Impl`).
540-
let mut clauses = vec![];
541-
542-
assemble_clauses_from_assoc_ty_values(
543-
self.infcx.tcx,
544-
projection_predicate.projection_ty.trait_ref(self.infcx.tcx).def_id,
545-
&mut clauses
546-
);
547-
548-
clauses
549-
}
550-
};
551-
552-
let environment = self.infcx.tcx.lift_to_global(environment)
553-
.expect("environment is not global");
554-
clauses.extend(
555-
self.infcx.tcx.program_clauses_for_env(environment)
556-
.into_iter()
557-
.cloned()
558-
);
559-
clauses
336+
self.program_clauses_impl(environment, goal)
560337
}
561338

562339
fn instantiate_binders_universally(

0 commit comments

Comments
 (0)