From 664d12d91aac04c1e59b1ec1ac7c3bb0ee22f08c Mon Sep 17 00:00:00 2001 From: pierwill Date: Tue, 14 Dec 2021 11:56:00 -0600 Subject: [PATCH 01/35] Remove `Ord` from `chalk_ir::interner::Interner::DefId` See https://github.com/rust-lang/rust/issues/90317 and https://github.com/rust-lang/rust/pull/90749. --- chalk-ir/src/interner.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chalk-ir/src/interner.rs b/chalk-ir/src/interner.rs index 7c80faf8cf1..4779353ccd4 100644 --- a/chalk-ir/src/interner.rs +++ b/chalk-ir/src/interner.rs @@ -188,7 +188,7 @@ pub trait Interner: Debug + Copy + Eq + Ord + Hash + Sized { type InternedVariances: Debug + Clone + Eq + Hash; /// The core "id" type used for trait-ids and the like. - type DefId: Debug + Copy + Eq + Ord + Hash; + type DefId: Debug + Copy + Eq + Hash; /// The ID type for ADTs type InternedAdtId: Debug + Copy + Eq + Ord + Hash; From a441bfd6c5ddd3c425cb6e5be827dea77a3369e3 Mon Sep 17 00:00:00 2001 From: pierwill Date: Tue, 14 Dec 2021 12:00:42 -0600 Subject: [PATCH 02/35] Remove `Ord`/`PartialOrd` from `chalk_solve::display::state::UnifiedId` --- chalk-solve/src/display/state.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chalk-solve/src/display/state.rs b/chalk-solve/src/display/state.rs index d12373de31c..7961a18fd99 100644 --- a/chalk-solve/src/display/state.rs +++ b/chalk-solve/src/display/state.rs @@ -36,7 +36,7 @@ impl Display for InvertedBoundVar { } } -#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] +#[derive(Copy, Clone, Debug, PartialEq, Eq)] enum UnifiedId { AdtId(I::InternedAdtId), DefId(I::DefId), From cb1e147ae5a723f96791848ca363d88f899c857e Mon Sep 17 00:00:00 2001 From: pierwill Date: Tue, 14 Dec 2021 12:01:24 -0600 Subject: [PATCH 03/35] Remove `Ord` from `IdAliasStore` generic --- chalk-solve/src/display/state.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chalk-solve/src/display/state.rs b/chalk-solve/src/display/state.rs index 7961a18fd99..e73017e5e57 100644 --- a/chalk-solve/src/display/state.rs +++ b/chalk-solve/src/display/state.rs @@ -43,7 +43,7 @@ enum UnifiedId { } #[derive(Debug)] -pub struct IdAliasStore { +pub struct IdAliasStore { /// Map from the DefIds we've encountered to a u32 alias id unique to all ids /// the same name. aliases: BTreeMap, From 50b9298bf79dbbe18c2e38984213b6a74bd7ce3b Mon Sep 17 00:00:00 2001 From: pierwill Date: Tue, 14 Dec 2021 12:09:32 -0600 Subject: [PATCH 04/35] Remove `Ord` from Interner and assoc types --- chalk-ir/src/interner.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/chalk-ir/src/interner.rs b/chalk-ir/src/interner.rs index 4779353ccd4..f8b3c58980f 100644 --- a/chalk-ir/src/interner.rs +++ b/chalk-ir/src/interner.rs @@ -57,7 +57,7 @@ use std::sync::Arc; /// (e.g., `SourceI` and `TargetI`) -- even if those type parameters /// wind up being mapped to the same underlying type families in the /// end. -pub trait Interner: Debug + Copy + Eq + Ord + Hash + Sized { +pub trait Interner: Debug + Copy + Eq + Hash + Sized { /// "Interned" representation of types. In normal user code, /// `Self::InternedType` is not referenced. Instead, we refer to /// `Ty`, which wraps this type. @@ -191,7 +191,7 @@ pub trait Interner: Debug + Copy + Eq + Ord + Hash + Sized { type DefId: Debug + Copy + Eq + Hash; /// The ID type for ADTs - type InternedAdtId: Debug + Copy + Eq + Ord + Hash; + type InternedAdtId: Debug + Copy + Eq + Hash; /// Representation of identifiers. type Identifier: Debug + Clone + Eq + Hash; From 8727138ad0f5a658019ece1c15e7fab8f549386c Mon Sep 17 00:00:00 2001 From: pierwill Date: Tue, 14 Dec 2021 12:16:22 -0600 Subject: [PATCH 05/35] Add `indexmap` crate --- chalk-solve/Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/chalk-solve/Cargo.toml b/chalk-solve/Cargo.toml index 90dc589f15f..756e9010143 100644 --- a/chalk-solve/Cargo.toml +++ b/chalk-solve/Cargo.toml @@ -20,6 +20,7 @@ rustc-hash = { version = "1.1.0" } chalk-derive = { version = "0.76.0-dev.0", path = "../chalk-derive" } chalk-ir = { version = "0.76.0-dev.0", path = "../chalk-ir" } +indexmap = "1.7.0" [dev-dependencies] chalk-integration = { path = "../chalk-integration" } From 1356f6d55a112a961e04cacf0857b53cdd4e2655 Mon Sep 17 00:00:00 2001 From: pierwill Date: Tue, 14 Dec 2021 12:21:15 -0600 Subject: [PATCH 06/35] Rm Ord from ChalkIr --- Cargo.lock | 1 + chalk-integration/src/interner.rs | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index d6d60bd5d3f..e60b6d96b31 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -186,6 +186,7 @@ dependencies = [ "chalk-integration", "chalk-ir", "ena", + "indexmap", "itertools", "petgraph", "rustc-hash", diff --git a/chalk-integration/src/interner.rs b/chalk-integration/src/interner.rs index e99999c5c56..441acd0d187 100644 --- a/chalk-integration/src/interner.rs +++ b/chalk-integration/src/interner.rs @@ -52,7 +52,7 @@ impl Debug for ChalkFnAbi { /// The default "interner" and the only interner used by chalk /// itself. In this interner, no interning actually occurs. -#[derive(Debug, Copy, Clone, Hash, PartialOrd, Ord, PartialEq, Eq)] +#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub struct ChalkIr; impl Interner for ChalkIr { From dd5ff49be8355ad93f585392694a8232e3c4eea4 Mon Sep 17 00:00:00 2001 From: pierwill Date: Tue, 14 Dec 2021 12:21:41 -0600 Subject: [PATCH 07/35] Use `indexmap` throughout --- Cargo.lock | 1 + chalk-integration/Cargo.toml | 1 + chalk-integration/src/lowering.rs | 8 +-- chalk-integration/src/lowering/env.rs | 36 +++++------ .../src/lowering/program_lowerer.rs | 39 ++++++------ chalk-integration/src/program.rs | 63 ++++++++++--------- chalk-integration/src/query.rs | 8 +-- chalk-solve/src/coherence.rs | 16 +++-- chalk-solve/src/display/state.rs | 17 ++--- chalk-solve/src/logging_db.rs | 43 ++----------- chalk-solve/src/logging_db/id_collector.rs | 11 ++-- 11 files changed, 109 insertions(+), 134 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e60b6d96b31..5fdf3e898fa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -142,6 +142,7 @@ dependencies = [ "chalk-parse", "chalk-recursive", "chalk-solve", + "indexmap", "salsa", "string_cache", "tracing", diff --git a/chalk-integration/Cargo.toml b/chalk-integration/Cargo.toml index 00c39f13cc6..8431b370e62 100644 --- a/chalk-integration/Cargo.toml +++ b/chalk-integration/Cargo.toml @@ -20,3 +20,4 @@ chalk-solve = { version = "0.76.0-dev.0", path = "../chalk-solve" } chalk-recursive = { version = "0.76.0-dev.0", path = "../chalk-recursive" } chalk-engine = { version = "0.76.0-dev.0", path = "../chalk-engine" } chalk-parse = { version = "0.76.0-dev.0", path = "../chalk-parse" } +indexmap = "1.7.0" diff --git a/chalk-integration/src/lowering.rs b/chalk-integration/src/lowering.rs index 965f2e63e6e..7c00bab8e45 100644 --- a/chalk-integration/src/lowering.rs +++ b/chalk-integration/src/lowering.rs @@ -8,8 +8,8 @@ use chalk_ir::{ }; use chalk_parse::ast::*; use chalk_solve::rust_ir::{self, IntoWhereClauses}; +use indexmap::IndexMap; use program_lowerer::ProgramLowerer; -use std::collections::BTreeMap; use string_cache::DefaultAtom as Atom; use tracing::debug; @@ -564,8 +564,6 @@ impl LowerWithEnv for [QuantifiedInlineBound] { } } - auto_traits.sort_by_key(|b| b.1); - regular_traits .iter() .chain(auto_traits.iter()) @@ -1014,7 +1012,7 @@ impl LowerWithEnv for (&TraitDefn, chalk_ir::TraitId) { pub fn lower_goal(goal: &Goal, program: &LoweredProgram) -> LowerResult> { let interner = ChalkIr; - let associated_ty_lookups: BTreeMap<_, _> = program + let associated_ty_lookups: IndexMap<_, _> = program .associated_ty_data .iter() .map(|(&associated_ty_id, datum)| { @@ -1052,7 +1050,7 @@ pub fn lower_goal(goal: &Goal, program: &LoweredProgram) -> LowerResult>; -pub type FnDefIds = BTreeMap>; -pub type ClosureIds = BTreeMap>; -pub type TraitIds = BTreeMap>; -pub type GeneratorIds = BTreeMap>; -pub type OpaqueTyIds = BTreeMap>; -pub type AdtKinds = BTreeMap, TypeKind>; -pub type FnDefKinds = BTreeMap, TypeKind>; -pub type ClosureKinds = BTreeMap, TypeKind>; -pub type TraitKinds = BTreeMap, TypeKind>; -pub type AutoTraits = BTreeMap, bool>; -pub type OpaqueTyVariableKinds = BTreeMap, TypeKind>; -pub type GeneratorKinds = BTreeMap, TypeKind>; -pub type AssociatedTyLookups = BTreeMap<(chalk_ir::TraitId, Ident), AssociatedTyLookup>; +pub type AdtIds = IndexMap>; +pub type FnDefIds = IndexMap>; +pub type ClosureIds = IndexMap>; +pub type TraitIds = IndexMap>; +pub type GeneratorIds = IndexMap>; +pub type OpaqueTyIds = IndexMap>; +pub type AdtKinds = IndexMap, TypeKind>; +pub type FnDefKinds = IndexMap, TypeKind>; +pub type ClosureKinds = IndexMap, TypeKind>; +pub type TraitKinds = IndexMap, TypeKind>; +pub type AutoTraits = IndexMap, bool>; +pub type OpaqueTyVariableKinds = IndexMap, TypeKind>; +pub type GeneratorKinds = IndexMap, TypeKind>; +pub type AssociatedTyLookups = IndexMap<(chalk_ir::TraitId, Ident), AssociatedTyLookup>; pub type AssociatedTyValueIds = - BTreeMap<(chalk_ir::ImplId, Ident), AssociatedTyValueId>; -pub type ForeignIds = BTreeMap>; + IndexMap<(chalk_ir::ImplId, Ident), AssociatedTyValueId>; +pub type ForeignIds = IndexMap>; -pub type ParameterMap = BTreeMap>; +pub type ParameterMap = IndexMap>; pub type LowerResult = Result; diff --git a/chalk-integration/src/lowering/program_lowerer.rs b/chalk-integration/src/lowering/program_lowerer.rs index b8c88cff140..6ccf0cf814f 100644 --- a/chalk-integration/src/lowering/program_lowerer.rs +++ b/chalk-integration/src/lowering/program_lowerer.rs @@ -8,8 +8,9 @@ use chalk_solve::rust_ir::{ self, Anonymize, AssociatedTyValueId, GeneratorDatum, GeneratorInputOutputDatum, GeneratorWitnessDatum, GeneratorWitnessExistential, OpaqueTyDatum, OpaqueTyDatumBound, }; +use indexmap::IndexMap; use rust_ir::IntoWhereClauses; -use std::collections::{BTreeMap, HashSet}; +use std::collections::HashSet; use std::sync::Arc; use string_cache::DefaultAtom as Atom; @@ -141,23 +142,23 @@ impl ProgramLowerer { } pub fn lower(self, program: &Program, raw_ids: &Vec) -> LowerResult { - let mut adt_data = BTreeMap::new(); - let mut adt_reprs = BTreeMap::new(); - let mut adt_variances = BTreeMap::new(); - let mut fn_def_data = BTreeMap::new(); - let mut fn_def_variances = BTreeMap::new(); - let mut closure_inputs_and_output = BTreeMap::new(); - let mut closure_closure_kind = BTreeMap::new(); - let mut closure_upvars = BTreeMap::new(); - let mut trait_data = BTreeMap::new(); - let mut well_known_traits = BTreeMap::new(); - let mut impl_data = BTreeMap::new(); - let mut associated_ty_data = BTreeMap::new(); - let mut associated_ty_values = BTreeMap::new(); - let mut opaque_ty_data = BTreeMap::new(); - let mut generator_data = BTreeMap::new(); - let mut generator_witness_data = BTreeMap::new(); - let mut hidden_opaque_types = BTreeMap::new(); + let mut adt_data = IndexMap::new(); + let mut adt_reprs = IndexMap::new(); + let mut adt_variances = IndexMap::new(); + let mut fn_def_data = IndexMap::new(); + let mut fn_def_variances = IndexMap::new(); + let mut closure_inputs_and_output = IndexMap::new(); + let mut closure_closure_kind = IndexMap::new(); + let mut closure_upvars = IndexMap::new(); + let mut trait_data = IndexMap::new(); + let mut well_known_traits = IndexMap::new(); + let mut impl_data = IndexMap::new(); + let mut associated_ty_data = IndexMap::new(); + let mut associated_ty_values = IndexMap::new(); + let mut opaque_ty_data = IndexMap::new(); + let mut generator_data = IndexMap::new(); + let mut generator_witness_data = IndexMap::new(); + let mut hidden_opaque_types = IndexMap::new(); let mut custom_clauses = Vec::new(); for (item, &raw_id) in program.items.iter().zip(raw_ids) { @@ -175,7 +176,7 @@ impl ProgramLowerer { generator_ids: &self.generator_ids, generator_kinds: &self.generator_kinds, associated_ty_lookups: &self.associated_ty_lookups, - parameter_map: BTreeMap::new(), + parameter_map: IndexMap::new(), auto_traits: &self.auto_traits, foreign_ty_ids: &self.foreign_ty_ids, }; diff --git a/chalk-integration/src/program.rs b/chalk-integration/src/program.rs index cc9d9e293e5..c1a2a17f813 100644 --- a/chalk-integration/src/program.rs +++ b/chalk-integration/src/program.rs @@ -15,87 +15,88 @@ use chalk_solve::rust_ir::{ }; use chalk_solve::split::Split; use chalk_solve::RustIrDatabase; -use std::collections::{BTreeMap, HashSet}; +use indexmap::IndexMap; +use std::collections::HashSet; use std::fmt; use std::sync::Arc; #[derive(Clone, Debug, PartialEq, Eq)] pub struct Program { /// From ADT name to item-id. Used during lowering only. - pub adt_ids: BTreeMap>, + pub adt_ids: IndexMap>, /// For each ADT: - pub adt_kinds: BTreeMap, TypeKind>, + pub adt_kinds: IndexMap, TypeKind>, - pub adt_variances: BTreeMap, Vec>, + pub adt_variances: IndexMap, Vec>, - pub fn_def_ids: BTreeMap>, + pub fn_def_ids: IndexMap>, - pub fn_def_kinds: BTreeMap, TypeKind>, + pub fn_def_kinds: IndexMap, TypeKind>, - pub fn_def_variances: BTreeMap, Vec>, + pub fn_def_variances: IndexMap, Vec>, - pub closure_ids: BTreeMap>, + pub closure_ids: IndexMap>, - pub closure_upvars: BTreeMap, Binders>>, + pub closure_upvars: IndexMap, Binders>>, - pub closure_kinds: BTreeMap, TypeKind>, + pub closure_kinds: IndexMap, TypeKind>, /// For each generator - pub generator_ids: BTreeMap>, + pub generator_ids: IndexMap>, - pub generator_kinds: BTreeMap, TypeKind>, + pub generator_kinds: IndexMap, TypeKind>, - pub generator_data: BTreeMap, Arc>>, + pub generator_data: IndexMap, Arc>>, - pub generator_witness_data: BTreeMap, Arc>>, + pub generator_witness_data: IndexMap, Arc>>, /// From trait name to item-id. Used during lowering only. - pub trait_ids: BTreeMap>, + pub trait_ids: IndexMap>, /// For each trait: - pub trait_kinds: BTreeMap, TypeKind>, + pub trait_kinds: IndexMap, TypeKind>, /// For each ADT: - pub adt_data: BTreeMap, Arc>>, + pub adt_data: IndexMap, Arc>>, - pub adt_reprs: BTreeMap, Arc>>, + pub adt_reprs: IndexMap, Arc>>, - pub fn_def_data: BTreeMap, Arc>>, + pub fn_def_data: IndexMap, Arc>>, pub closure_inputs_and_output: - BTreeMap, Binders>>, + IndexMap, Binders>>, // Weird name, but otherwise would overlap with `closure_kinds` above. - pub closure_closure_kind: BTreeMap, ClosureKind>, + pub closure_closure_kind: IndexMap, ClosureKind>, /// For each impl: - pub impl_data: BTreeMap, Arc>>, + pub impl_data: IndexMap, Arc>>, /// For each associated ty value `type Foo = XXX` found in an impl: pub associated_ty_values: - BTreeMap, Arc>>, + IndexMap, Arc>>, // From opaque type name to item-id. Used during lowering only. - pub opaque_ty_ids: BTreeMap>, + pub opaque_ty_ids: IndexMap>, /// For each opaque type: - pub opaque_ty_kinds: BTreeMap, TypeKind>, + pub opaque_ty_kinds: IndexMap, TypeKind>, /// For each opaque type: - pub opaque_ty_data: BTreeMap, Arc>>, + pub opaque_ty_data: IndexMap, Arc>>, /// Stores the hidden types for opaque types - pub hidden_opaque_types: BTreeMap, Arc>>, + pub hidden_opaque_types: IndexMap, Arc>>, /// For each trait: - pub trait_data: BTreeMap, Arc>>, + pub trait_data: IndexMap, Arc>>, /// For each trait lang item - pub well_known_traits: BTreeMap>, + pub well_known_traits: IndexMap>, /// For each associated ty declaration `type Foo` found in a trait: - pub associated_ty_data: BTreeMap, Arc>>, + pub associated_ty_data: IndexMap, Arc>>, /// For each user-specified clause pub custom_clauses: Vec>, @@ -104,7 +105,7 @@ pub struct Program { pub object_safe_traits: HashSet>, /// For each foreign type `extern { type A; }` - pub foreign_ty_ids: BTreeMap>, + pub foreign_ty_ids: IndexMap>, } impl Program { diff --git a/chalk-integration/src/query.rs b/chalk-integration/src/query.rs index 1d074b953d9..58cd59ec108 100644 --- a/chalk-integration/src/query.rs +++ b/chalk-integration/src/query.rs @@ -16,10 +16,10 @@ use chalk_solve::coherence::{CoherenceSolver, SpecializationPriorities}; use chalk_solve::wf; use chalk_solve::RustIrDatabase; use chalk_solve::Solver; +use indexmap::IndexMap; use salsa::Database; use std::clone::Clone; use std::cmp::{Eq, PartialEq}; -use std::collections::BTreeMap; use std::ops::{Deref, DerefMut}; use std::sync::Arc; use std::sync::Mutex; @@ -40,7 +40,7 @@ pub trait LoweringDatabase: /// one another (the "specialization priorities"). fn coherence( &self, - ) -> Result, Arc>>, ChalkError>; + ) -> Result, Arc>>, ChalkError>; fn orphan_check(&self) -> Result<(), ChalkError>; @@ -142,12 +142,12 @@ fn orphan_check(db: &dyn LoweringDatabase) -> Result<(), ChalkError> { fn coherence( db: &dyn LoweringDatabase, -) -> Result, Arc>>, ChalkError> { +) -> Result, Arc>>, ChalkError> { let program = db.program_ir()?; let solver_choice = db.solver_choice(); let priorities_map = tls::set_current_program(&program, || -> Result<_, ChalkError> { let solver_builder = || solver_choice.into_solver(); - let priorities_map: Result, ChalkError> = program + let priorities_map: Result, ChalkError> = program .trait_data .keys() .map(|&trait_id| { diff --git a/chalk-solve/src/coherence.rs b/chalk-solve/src/coherence.rs index 31384c0d82c..067876b6f26 100644 --- a/chalk-solve/src/coherence.rs +++ b/chalk-solve/src/coherence.rs @@ -1,10 +1,11 @@ use petgraph::prelude::*; +use indexmap::IndexMap; + use crate::solve::Solver; use crate::RustIrDatabase; use chalk_ir::interner::Interner; use chalk_ir::{self, ImplId, TraitId}; -use std::collections::BTreeMap; use std::fmt; use std::sync::Arc; @@ -42,13 +43,13 @@ impl std::error::Error for CoherenceError {} /// This basically encodes which impls specialize one another. #[derive(Clone, Debug, Default, PartialEq, Eq)] pub struct SpecializationPriorities { - map: BTreeMap, SpecializationPriority>, + map: IndexMap, SpecializationPriority>, } impl SpecializationPriorities { pub fn new() -> Self { Self { - map: BTreeMap::new(), + map: IndexMap::new(), } } @@ -108,16 +109,19 @@ where fn build_specialization_forest(&self) -> Result, ()>, CoherenceError> { // The forest is returned as a graph but built as a GraphMap; this is // so that we never add multiple nodes with the same ItemId. - let mut forest = DiGraphMap::new(); + let mut forest = DiGraph::new(); // Find all specializations (implemented in coherence/solve) // Record them in the forest by adding an edge from the less special // to the more special. self.visit_specializations_of_trait(|less_special, more_special| { - forest.add_edge(less_special, more_special, ()); + let l = forest.add_node(less_special); + let m = forest.add_node(more_special); + + forest.add_edge(l, m, ()); })?; - Ok(forest.into_graph()) + Ok(forest) } // Recursively set priorities for those node and all of its children. diff --git a/chalk-solve/src/display/state.rs b/chalk-solve/src/display/state.rs index e73017e5e57..5d582fb1966 100644 --- a/chalk-solve/src/display/state.rs +++ b/chalk-solve/src/display/state.rs @@ -3,8 +3,10 @@ //! This is essentially `InternalWriterState` and other things supporting that. use std::{ borrow::Borrow, + cmp::{Eq, PartialEq}, collections::BTreeMap, fmt::{Debug, Display, Formatter, Result}, + hash::Hash, marker::PhantomData, rc::Rc, sync::{Arc, Mutex}, @@ -12,6 +14,7 @@ use std::{ use crate::RustIrDatabase; use chalk_ir::{interner::Interner, *}; +use indexmap::IndexMap; use itertools::Itertools; /// Like a BoundVar, but with the debrujin index inverted so as to create a @@ -36,7 +39,7 @@ impl Display for InvertedBoundVar { } } -#[derive(Copy, Clone, Debug, PartialEq, Eq)] +#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] enum UnifiedId { AdtId(I::InternedAdtId), DefId(I::DefId), @@ -46,21 +49,21 @@ enum UnifiedId { pub struct IdAliasStore { /// Map from the DefIds we've encountered to a u32 alias id unique to all ids /// the same name. - aliases: BTreeMap, + aliases: IndexMap, /// Map from each name to the next unused u32 alias id. - next_unused_for_name: BTreeMap, + next_unused_for_name: IndexMap, } -impl Default for IdAliasStore { +impl Default for IdAliasStore { fn default() -> Self { IdAliasStore { - aliases: BTreeMap::default(), - next_unused_for_name: BTreeMap::default(), + aliases: IndexMap::default(), + next_unused_for_name: IndexMap::default(), } } } -impl IdAliasStore { +impl IdAliasStore { fn alias_for_id_name(&mut self, id: T, name: String) -> String { let next_unused_for_name = &mut self.next_unused_for_name; let alias = *self.aliases.entry(id).or_insert_with(|| { diff --git a/chalk-solve/src/logging_db.rs b/chalk-solve/src/logging_db.rs index 15fdfadc2a5..00408a28a0c 100644 --- a/chalk-solve/src/logging_db.rs +++ b/chalk-solve/src/logging_db.rs @@ -2,8 +2,6 @@ //! `.chalk` files containing those definitions. use std::{ borrow::Borrow, - cmp::{Ord, Ordering}, - collections::BTreeSet, fmt::{self, Debug, Display}, io::Write, marker::PhantomData, @@ -18,6 +16,8 @@ use crate::{ }; use chalk_ir::{interner::Interner, *}; +use indexmap::IndexSet; + mod id_collector; /// Wraps another `RustIrDatabase` (`DB`) and records which definitions are @@ -36,7 +36,7 @@ where I: Interner, { ws: WriterState, - def_ids: Mutex>>, + def_ids: Mutex>>, _phantom: PhantomData, } @@ -535,7 +535,7 @@ where } } -#[derive(Copy, Clone, PartialEq, Eq, Debug)] +#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)] pub enum RecordedItemId { Adt(AdtId), Trait(TraitId), @@ -580,38 +580,3 @@ impl From> for RecordedItemId { RecordedItemId::Generator(v) } } - -/// Utility for implementing Ord for RecordedItemId. -#[derive(PartialEq, Eq, PartialOrd, Ord)] -enum OrderedItemId<'a, DefId, AdtId> { - DefId(&'a DefId), - AdtId(&'a AdtId), -} - -impl RecordedItemId { - /// Extract internal identifier. Allows for absolute ordering matching the - /// order in which chalk saw things (and thus reproducing that order in - /// printed programs) - fn ordered_item_id(&self) -> OrderedItemId<'_, I::DefId, I::InternedAdtId> { - match self { - RecordedItemId::Trait(TraitId(x)) - | RecordedItemId::Impl(ImplId(x)) - | RecordedItemId::OpaqueTy(OpaqueTyId(x)) - | RecordedItemId::Generator(GeneratorId(x)) - | RecordedItemId::FnDef(FnDefId(x)) => OrderedItemId::DefId(x), - RecordedItemId::Adt(AdtId(x)) => OrderedItemId::AdtId(x), - } - } -} - -impl PartialOrd for RecordedItemId { - fn partial_cmp(&self, other: &Self) -> Option { - Some(self.cmp(other)) - } -} - -impl Ord for RecordedItemId { - fn cmp(&self, other: &Self) -> Ordering { - self.ordered_item_id().cmp(&other.ordered_item_id()) - } -} diff --git a/chalk-solve/src/logging_db/id_collector.rs b/chalk-solve/src/logging_db/id_collector.rs index f406b8b9a1e..603b4800b05 100644 --- a/chalk-solve/src/logging_db/id_collector.rs +++ b/chalk-solve/src/logging_db/id_collector.rs @@ -6,9 +6,10 @@ use chalk_ir::{ visit::{SuperVisit, Visit}, AliasTy, DebruijnIndex, TyKind, WhereClause, }; -use std::collections::BTreeSet; use std::ops::ControlFlow; +use indexmap::IndexSet; + /// Collects the identifiers needed to resolve all the names for a given /// set of identifers, excluding identifiers we already have. /// @@ -34,11 +35,11 @@ use std::ops::ControlFlow; /// resolution is successful. pub fn collect_unrecorded_ids<'i, I: Interner, DB: RustIrDatabase>( db: &'i DB, - identifiers: &'_ BTreeSet>, -) -> BTreeSet> { + identifiers: &'_ IndexSet>, +) -> IndexSet> { let mut collector = IdCollector { db, - found_identifiers: BTreeSet::new(), + found_identifiers: IndexSet::new(), }; for id in identifiers { match *id { @@ -96,7 +97,7 @@ pub fn collect_unrecorded_ids<'i, I: Interner, DB: RustIrDatabase>( struct IdCollector<'i, I: Interner, DB: RustIrDatabase> { db: &'i DB, - found_identifiers: BTreeSet>, + found_identifiers: IndexSet>, } impl<'i, I: Interner, DB: RustIrDatabase> IdCollector<'i, I, DB> { From 0dd9b31c21a436cfb8bfbec82e173853a71f915f Mon Sep 17 00:00:00 2001 From: pierwill Date: Tue, 14 Dec 2021 13:09:35 -0600 Subject: [PATCH 08/35] Remove Ord from id types --- chalk-ir/src/lib.rs | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/chalk-ir/src/lib.rs b/chalk-ir/src/lib.rs index 2dd073ffc82..30fb31ac0ec 100644 --- a/chalk-ir/src/lib.rs +++ b/chalk-ir/src/lib.rs @@ -359,55 +359,55 @@ impl UniverseMap { } /// The id for an Abstract Data Type (i.e. structs, unions and enums). -#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[derive(Copy, Clone, PartialEq, Eq, Hash)] pub struct AdtId(pub I::InternedAdtId); /// The id of a trait definition; could be used to load the trait datum by /// invoking the [`trait_datum`] method. /// /// [`trait_datum`]: ../chalk_solve/trait.RustIrDatabase.html#tymethod.trait_datum -#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[derive(Copy, Clone, PartialEq, Eq, Hash)] pub struct TraitId(pub I::DefId); /// The id for an impl. -#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[derive(Copy, Clone, PartialEq, Eq, Hash)] pub struct ImplId(pub I::DefId); /// Id for a specific clause. -#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[derive(Copy, Clone, PartialEq, Eq, Hash)] pub struct ClauseId(pub I::DefId); /// The id for the associated type member of a trait. The details of the type /// can be found by invoking the [`associated_ty_data`] method. /// /// [`associated_ty_data`]: ../chalk_solve/trait.RustIrDatabase.html#tymethod.associated_ty_data -#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[derive(Copy, Clone, PartialEq, Eq, Hash)] pub struct AssocTypeId(pub I::DefId); /// Id for an opaque type. -#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[derive(Copy, Clone, PartialEq, Eq, Hash)] pub struct OpaqueTyId(pub I::DefId); /// Function definition id. -#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[derive(Copy, Clone, PartialEq, Eq, Hash)] pub struct FnDefId(pub I::DefId); /// Id for Rust closures. -#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[derive(Copy, Clone, PartialEq, Eq, Hash)] pub struct ClosureId(pub I::DefId); /// Id for Rust generators. -#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[derive(Copy, Clone, PartialEq, Eq, Hash)] pub struct GeneratorId(pub I::DefId); /// Id for foreign types. -#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[derive(Copy, Clone, PartialEq, Eq, Hash)] pub struct ForeignDefId(pub I::DefId); impl_debugs!(ImplId, ClauseId); /// A Rust type. The actual type data is stored in `TyKind`. -#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, HasInterner)] +#[derive(Copy, Clone, PartialEq, Eq, Hash, HasInterner)] pub struct Ty { interned: I::InternedType, } From eb914bb0db255af771879a316d09bce9d3b562ac Mon Sep 17 00:00:00 2001 From: pierwill Date: Tue, 14 Dec 2021 14:07:12 -0600 Subject: [PATCH 09/35] Do not add nodes that already exist --- chalk-solve/src/coherence.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/chalk-solve/src/coherence.rs b/chalk-solve/src/coherence.rs index 067876b6f26..5331520f456 100644 --- a/chalk-solve/src/coherence.rs +++ b/chalk-solve/src/coherence.rs @@ -107,18 +107,21 @@ where // Build the forest of specialization relationships. fn build_specialization_forest(&self) -> Result, ()>, CoherenceError> { - // The forest is returned as a graph but built as a GraphMap; this is - // so that we never add multiple nodes with the same ItemId. let mut forest = DiGraph::new(); // Find all specializations (implemented in coherence/solve) // Record them in the forest by adding an edge from the less special // to the more special. self.visit_specializations_of_trait(|less_special, more_special| { - let l = forest.add_node(less_special); - let m = forest.add_node(more_special); + let node_impls: Vec> = forest.raw_nodes().iter().map(|x| x.weight).collect(); - forest.add_edge(l, m, ()); + // Check so that we never add multiple nodes with the same ImplId. + if !node_impls.contains(&less_special) && !node_impls.contains(&more_special) { + let l = forest.add_node(less_special); + let m = forest.add_node(more_special); + + forest.add_edge(l, m, ()); + } })?; Ok(forest) From fcc3485e24bfbcc6dc6adea6743b5c840bd39ecb Mon Sep 17 00:00:00 2001 From: pierwill Date: Wed, 15 Dec 2021 09:51:35 -0600 Subject: [PATCH 10/35] Use indexmap 1.5.1 (same as rustc) --- chalk-integration/Cargo.toml | 2 +- chalk-solve/Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/chalk-integration/Cargo.toml b/chalk-integration/Cargo.toml index 8431b370e62..ace25f12b55 100644 --- a/chalk-integration/Cargo.toml +++ b/chalk-integration/Cargo.toml @@ -20,4 +20,4 @@ chalk-solve = { version = "0.76.0-dev.0", path = "../chalk-solve" } chalk-recursive = { version = "0.76.0-dev.0", path = "../chalk-recursive" } chalk-engine = { version = "0.76.0-dev.0", path = "../chalk-engine" } chalk-parse = { version = "0.76.0-dev.0", path = "../chalk-parse" } -indexmap = "1.7.0" +indexmap = "1.5.1" diff --git a/chalk-solve/Cargo.toml b/chalk-solve/Cargo.toml index 756e9010143..bde17cef6cd 100644 --- a/chalk-solve/Cargo.toml +++ b/chalk-solve/Cargo.toml @@ -20,7 +20,7 @@ rustc-hash = { version = "1.1.0" } chalk-derive = { version = "0.76.0-dev.0", path = "../chalk-derive" } chalk-ir = { version = "0.76.0-dev.0", path = "../chalk-ir" } -indexmap = "1.7.0" +indexmap = "1.5.1" [dev-dependencies] chalk-integration = { path = "../chalk-integration" } From 8f1d6e858dc6d193925699348c76a65736350f7a Mon Sep 17 00:00:00 2001 From: pierwill Date: Wed, 15 Dec 2021 11:03:38 -0600 Subject: [PATCH 11/35] Add fixme notice --- chalk-integration/src/lowering.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/chalk-integration/src/lowering.rs b/chalk-integration/src/lowering.rs index 7c00bab8e45..1677d8be925 100644 --- a/chalk-integration/src/lowering.rs +++ b/chalk-integration/src/lowering.rs @@ -564,6 +564,11 @@ impl LowerWithEnv for [QuantifiedInlineBound] { } } + // FIXME: removing the line below is causing + // test::unsize::dyn_to_dyn_unsizing to fail. + // + // auto_traits.sort_by_key(|b| b.1); + regular_traits .iter() .chain(auto_traits.iter()) From 6408a3a58c6c53208f207ac55061682aec4ea228 Mon Sep 17 00:00:00 2001 From: pierwill Date: Wed, 15 Dec 2021 12:10:19 -0600 Subject: [PATCH 12/35] Add indexmap to chalk-parse --- Cargo.lock | 1 + chalk-parse/Cargo.toml | 1 + 2 files changed, 2 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 5fdf3e898fa..35488894134 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -161,6 +161,7 @@ dependencies = [ name = "chalk-parse" version = "0.76.0-dev.0" dependencies = [ + "indexmap", "lalrpop", "lalrpop-util", "regex", diff --git a/chalk-parse/Cargo.toml b/chalk-parse/Cargo.toml index e23b2b3b292..9b367239a80 100644 --- a/chalk-parse/Cargo.toml +++ b/chalk-parse/Cargo.toml @@ -16,6 +16,7 @@ version = "0.19" features = ["lexer"] [dependencies] +indexmap = "1.7.0" lalrpop-util = "0.19" regex = "1.3" string_cache = "0.8.0" From 75f82830a227d7f1f36621149633ba8bf8168669 Mon Sep 17 00:00:00 2001 From: pierwill Date: Wed, 15 Dec 2021 12:11:12 -0600 Subject: [PATCH 13/35] Add Hash traits where needed --- chalk-parse/src/ast.rs | 46 +++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/chalk-parse/src/ast.rs b/chalk-parse/src/ast.rs index 0f0e9c54663..b6188b693fd 100644 --- a/chalk-parse/src/ast.rs +++ b/chalk-parse/src/ast.rs @@ -1,7 +1,7 @@ use std::fmt; use string_cache::DefaultAtom as Atom; -#[derive(Copy, Clone, PartialEq, Eq, Debug)] +#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)] pub struct Span { pub lo: usize, pub hi: usize, @@ -98,7 +98,7 @@ pub struct AdtRepr { pub int: Option, } -#[derive(Clone, PartialEq, Eq, Debug)] +#[derive(Clone, PartialEq, Eq, Debug, Hash)] pub struct FnSig { pub abi: FnAbi, pub safety: Safety, @@ -126,7 +126,7 @@ pub struct ClosureDefn { pub upvars: Vec, } -#[derive(Clone, Eq, PartialEq, Debug)] +#[derive(Clone, Eq, PartialEq, Debug, Hash)] pub struct FnAbi(pub Atom); impl Default for FnAbi { @@ -189,7 +189,7 @@ pub struct OpaqueTyDefn { pub where_clauses: Vec, } -#[derive(Clone, PartialEq, Eq, Debug)] +#[derive(Clone, PartialEq, Eq, Debug, Hash)] pub enum VariableKind { Ty(Identifier), IntegerTy(Identifier), @@ -198,7 +198,7 @@ pub enum VariableKind { Const(Identifier), } -#[derive(Clone, PartialEq, Eq, Debug)] +#[derive(Clone, PartialEq, Eq, Debug, Hash)] pub enum GenericArg { Ty(Ty), Lifetime(Lifetime), @@ -206,26 +206,26 @@ pub enum GenericArg { Const(Const), } -#[derive(Clone, PartialEq, Eq, Debug)] +#[derive(Clone, PartialEq, Eq, Debug, Hash)] pub enum Const { Id(Identifier), Value(u32), } -#[derive(Clone, PartialEq, Eq, Debug)] +#[derive(Clone, PartialEq, Eq, Debug, Hash)] /// An inline bound, e.g. `: Foo` in `impl> SomeType`. pub enum InlineBound { TraitBound(TraitBound), AliasEqBound(AliasEqBound), } -#[derive(Clone, PartialEq, Eq, Debug)] +#[derive(Clone, PartialEq, Eq, Debug, Hash)] pub struct QuantifiedInlineBound { pub variable_kinds: Vec, pub bound: InlineBound, } -#[derive(Clone, PartialEq, Eq, Debug)] +#[derive(Clone, PartialEq, Eq, Debug, Hash)] /// Represents a trait bound on e.g. a type or type parameter. /// Does not know anything about what it's binding. pub struct TraitBound { @@ -233,7 +233,7 @@ pub struct TraitBound { pub args_no_self: Vec, } -#[derive(Clone, PartialEq, Eq, Debug)] +#[derive(Clone, PartialEq, Eq, Debug, Hash)] /// Represents an alias equality bound on e.g. a type or type parameter. /// Does not know anything about what it's binding. pub struct AliasEqBound { @@ -284,7 +284,7 @@ pub struct AssocTyValue { pub default: bool, } -#[derive(Clone, PartialEq, Eq, Debug)] +#[derive(Clone, PartialEq, Eq, Debug, Hash)] pub enum Ty { Id { name: Identifier, @@ -331,7 +331,7 @@ pub enum Ty { Never, } -#[derive(Copy, Clone, PartialEq, Eq, Debug)] +#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)] pub enum IntTy { Isize, I8, @@ -341,7 +341,7 @@ pub enum IntTy { I128, } -#[derive(Copy, Clone, PartialEq, Eq, Debug)] +#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)] pub enum UintTy { Usize, U8, @@ -351,13 +351,13 @@ pub enum UintTy { U128, } -#[derive(Copy, Clone, PartialEq, Eq, Debug)] +#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)] pub enum FloatTy { F32, F64, } -#[derive(Copy, Clone, PartialEq, Eq, Debug)] +#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)] pub enum ScalarType { Bool, Char, @@ -366,13 +366,13 @@ pub enum ScalarType { Float(FloatTy), } -#[derive(Copy, Clone, PartialEq, Eq, Debug)] +#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)] pub enum Mutability { Mut, Not, } -#[derive(Copy, Clone, PartialEq, Eq, Debug)] +#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)] pub enum Safety { Safe, Unsafe, @@ -384,7 +384,7 @@ impl Default for Safety { } } -#[derive(Clone, PartialEq, Eq, Debug)] +#[derive(Clone, PartialEq, Eq, Debug, Hash)] pub enum Lifetime { Id { name: Identifier }, Static, @@ -392,14 +392,14 @@ pub enum Lifetime { Empty, } -#[derive(Clone, PartialEq, Eq, Debug)] +#[derive(Clone, PartialEq, Eq, Debug, Hash)] pub struct ProjectionTy { pub trait_ref: TraitRef, pub name: Identifier, pub args: Vec, } -#[derive(Clone, PartialEq, Eq, Debug)] +#[derive(Clone, PartialEq, Eq, Debug, Hash)] pub struct TraitRef { pub trait_name: Identifier, pub args: Vec, @@ -424,7 +424,7 @@ impl Polarity { } } -#[derive(Clone, PartialEq, Eq, Debug)] +#[derive(Clone, PartialEq, Eq, Debug, Hash)] pub struct Identifier { pub str: Atom, pub span: Span, @@ -436,7 +436,7 @@ impl fmt::Display for Identifier { } } -#[derive(Clone, PartialEq, Eq, Debug)] +#[derive(Clone, PartialEq, Eq, Debug, Hash)] pub enum WhereClause { Implemented { trait_ref: TraitRef }, ProjectionEq { projection: ProjectionTy, ty: Ty }, @@ -469,7 +469,7 @@ pub enum LeafGoal { SubtypeGenericArgs { a: Ty, b: Ty }, } -#[derive(Clone, PartialEq, Eq, Debug)] +#[derive(Clone, PartialEq, Eq, Debug, Hash)] pub struct QuantifiedWhereClause { pub variable_kinds: Vec, pub where_clause: WhereClause, From 7a80b58154bfbd72f1b8b0844ad151401b32929f Mon Sep 17 00:00:00 2001 From: pierwill Date: Wed, 15 Dec 2021 12:11:25 -0600 Subject: [PATCH 14/35] Use indexset for where_clauses --- chalk-parse/src/ast.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/chalk-parse/src/ast.rs b/chalk-parse/src/ast.rs index b6188b693fd..c429ba7dc35 100644 --- a/chalk-parse/src/ast.rs +++ b/chalk-parse/src/ast.rs @@ -1,3 +1,4 @@ +use indexmap::IndexSet; use std::fmt; use string_cache::DefaultAtom as Atom; @@ -186,7 +187,7 @@ pub struct OpaqueTyDefn { pub variable_kinds: Vec, pub name: Identifier, pub bounds: Vec, - pub where_clauses: Vec, + pub where_clauses: IndexSet, } #[derive(Clone, PartialEq, Eq, Debug, Hash)] From cfe63d7e9e6b36084b53aa76496641834dd00620 Mon Sep 17 00:00:00 2001 From: pierwill Date: Wed, 15 Dec 2021 12:26:56 -0600 Subject: [PATCH 15/35] Use indexset in impl LowerWithEnv for [QuantifiedInlineBound] --- chalk-integration/src/lowering.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/chalk-integration/src/lowering.rs b/chalk-integration/src/lowering.rs index 1677d8be925..7c77f7c9fa4 100644 --- a/chalk-integration/src/lowering.rs +++ b/chalk-integration/src/lowering.rs @@ -8,7 +8,7 @@ use chalk_ir::{ }; use chalk_parse::ast::*; use chalk_solve::rust_ir::{self, IntoWhereClauses}; -use indexmap::IndexMap; +use indexmap::{IndexMap, IndexSet}; use program_lowerer::ProgramLowerer; use string_cache::DefaultAtom as Atom; use tracing::debug; @@ -542,7 +542,7 @@ impl LowerWithEnv for QuantifiedInlineBound { } impl LowerWithEnv for [QuantifiedInlineBound] { - type Lowered = Vec>; + type Lowered = IndexSet>; fn lower(&self, env: &Env) -> LowerResult { fn trait_identifier(bound: &InlineBound) -> &Identifier { @@ -552,15 +552,15 @@ impl LowerWithEnv for [QuantifiedInlineBound] { } } - let mut regular_traits = Vec::new(); - let mut auto_traits = Vec::new(); + let mut regular_traits = IndexSet::new(); + let mut auto_traits = IndexSet::new(); for b in self { let id = env.lookup_trait(trait_identifier(&b.bound))?; if env.auto_trait(id) { - auto_traits.push((b, id)) + auto_traits.insert((b, id)) } else { - regular_traits.push((b, id)) + regular_traits.insert((b, id)) } } From cd602c694a33752d01e5888eb504a0552c634c01 Mon Sep 17 00:00:00 2001 From: pierwill Date: Wed, 15 Dec 2021 13:10:16 -0600 Subject: [PATCH 16/35] parser.lalrpop: use IndexSet for QuantifiedWhereClauses --- chalk-parse/src/parser.lalrpop | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/chalk-parse/src/parser.lalrpop b/chalk-parse/src/parser.lalrpop index 8f20258560e..70c59c85f45 100644 --- a/chalk-parse/src/parser.lalrpop +++ b/chalk-parse/src/parser.lalrpop @@ -1,5 +1,6 @@ use crate::ast::*; use string_cache::DefaultAtom as Atom; +use indexmap::IndexSet; grammar; @@ -599,9 +600,9 @@ QuantifiedWhereClause: QuantifiedWhereClause = { }, }; -QuantifiedWhereClauses: Vec = { +QuantifiedWhereClauses: IndexSet = { "where" >, - () => vec![], + () => IndexSet::new(), }; DomainGoal: DomainGoal = { From b964a3a009ec42a3157467c6e48cb91cdff03720 Mon Sep 17 00:00:00 2001 From: pierwill Date: Wed, 15 Dec 2021 13:14:24 -0600 Subject: [PATCH 17/35] AST: use IndexSet for `where_clause`s --- chalk-parse/src/ast.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/chalk-parse/src/ast.rs b/chalk-parse/src/ast.rs index c429ba7dc35..9a60a93b4bb 100644 --- a/chalk-parse/src/ast.rs +++ b/chalk-parse/src/ast.rs @@ -39,7 +39,7 @@ pub struct ForeignDefn(pub Identifier); pub struct AdtDefn { pub name: Identifier, pub variable_kinds: Vec, - pub where_clauses: Vec, + pub where_clauses: IndexSet, pub variants: Vec, pub flags: AdtFlags, pub repr: AdtRepr, @@ -110,7 +110,7 @@ pub struct FnSig { pub struct FnDefn { pub name: Identifier, pub variable_kinds: Vec, - pub where_clauses: Vec, + pub where_clauses: IndexSet, pub argument_types: Vec, pub return_type: Ty, pub sig: FnSig, @@ -140,7 +140,7 @@ impl Default for FnAbi { pub struct TraitDefn { pub name: Identifier, pub variable_kinds: Vec, - pub where_clauses: Vec, + pub where_clauses: IndexSet, pub assoc_ty_defns: Vec, pub flags: TraitFlags, pub well_known: Option, @@ -178,7 +178,7 @@ pub struct AssocTyDefn { pub name: Identifier, pub variable_kinds: Vec, pub bounds: Vec, - pub where_clauses: Vec, + pub where_clauses: IndexSet, } #[derive(Clone, PartialEq, Eq, Debug)] @@ -266,7 +266,7 @@ pub struct Impl { pub variable_kinds: Vec, pub trait_ref: TraitRef, pub polarity: Polarity, - pub where_clauses: Vec, + pub where_clauses: IndexSet, pub assoc_ty_values: Vec, pub impl_type: ImplType, } From a8525b6514cb3a00f3bed0fa8cc4833b6915aa2d Mon Sep 17 00:00:00 2001 From: pierwill Date: Wed, 15 Dec 2021 13:27:14 -0600 Subject: [PATCH 18/35] Use IndexSet in lowering.rs --- chalk-integration/src/lowering.rs | 52 ++++++++++++++++++++----------- 1 file changed, 33 insertions(+), 19 deletions(-) diff --git a/chalk-integration/src/lowering.rs b/chalk-integration/src/lowering.rs index 7c77f7c9fa4..eab14055309 100644 --- a/chalk-integration/src/lowering.rs +++ b/chalk-integration/src/lowering.rs @@ -136,20 +136,24 @@ impl Lower for VariableKind { } impl LowerWithEnv for [QuantifiedWhereClause] { - type Lowered = Vec>; + type Lowered = IndexSet>; fn lower(&self, env: &Env) -> LowerResult { self.iter() .flat_map(|wc| match wc.lower(env) { Ok(v) => v.into_iter().map(Ok).collect(), - Err(e) => vec![Err(e)], + Err(e) => { + let mut set = IndexSet::new(); + set.insert([Err(e)]); + set + } }) - .collect() + .collect::>() } } impl LowerWithEnv for WhereClause { - type Lowered = Vec>; + type Lowered = IndexSet>; /// Lower from an AST `where` clause to an internal IR. /// Some AST `where` clauses can lower to multiple ones, this is why we return a `Vec`. @@ -158,37 +162,47 @@ impl LowerWithEnv for WhereClause { fn lower(&self, env: &Env) -> LowerResult { Ok(match self { WhereClause::Implemented { trait_ref } => { - vec![chalk_ir::WhereClause::Implemented(trait_ref.lower(env)?)] + let mut set = IndexSet::new(); + set.insert(chalk_ir::WhereClause::Implemented(trait_ref.lower(env)?)); + set + } + WhereClause::ProjectionEq { projection, ty } => { + let mut set = IndexSet::new(); + set.insert( + chalk_ir::WhereClause::AliasEq(chalk_ir::AliasEq { + alias: chalk_ir::AliasTy::Projection(projection.lower(env)?), + ty: ty.lower(env)?, + }), + chalk_ir::WhereClause::Implemented(projection.trait_ref.lower(env)?), + ); + set } - WhereClause::ProjectionEq { projection, ty } => vec![ - chalk_ir::WhereClause::AliasEq(chalk_ir::AliasEq { - alias: chalk_ir::AliasTy::Projection(projection.lower(env)?), - ty: ty.lower(env)?, - }), - chalk_ir::WhereClause::Implemented(projection.trait_ref.lower(env)?), - ], WhereClause::LifetimeOutlives { a, b } => { - vec![chalk_ir::WhereClause::LifetimeOutlives( + let mut set = IndexSet::new(); + set.insert(chalk_ir::WhereClause::LifetimeOutlives( chalk_ir::LifetimeOutlives { a: a.lower(env)?, b: b.lower(env)?, }, - )] + )); + set } WhereClause::TypeOutlives { ty, lifetime } => { - vec![chalk_ir::WhereClause::TypeOutlives( + let mut set = IndexSet::new(); + set.insert(chalk_ir::WhereClause::TypeOutlives( chalk_ir::TypeOutlives { ty: ty.lower(env)?, lifetime: lifetime.lower(env)?, }, - )] + )); + set } }) } } impl LowerWithEnv for QuantifiedWhereClause { - type Lowered = Vec>; + type Lowered = IndexSet>; /// Lower from an AST `where` clause to an internal IR. /// Some AST `where` clauses can lower to multiple ones, this is why we return a `Vec`. @@ -197,7 +211,7 @@ impl LowerWithEnv for QuantifiedWhereClause { fn lower(&self, env: &Env) -> LowerResult { let variable_kinds = self.variable_kinds.iter().map(|k| k.lower()); let binders = env.in_binders(variable_kinds, |env| Ok(self.where_clause.lower(env)?))?; - Ok(binders.into_iter().collect()) + Ok(binders.into_iter().collect::>()) } } @@ -573,7 +587,7 @@ impl LowerWithEnv for [QuantifiedInlineBound] { .iter() .chain(auto_traits.iter()) .map(|(b, _)| b.lower(env)) - .collect() + .collect::>() } } From aa6c89e6cff5e9d4bd55efe2ed74dc64cff4be68 Mon Sep 17 00:00:00 2001 From: pierwill Date: Wed, 15 Dec 2021 15:43:52 -0600 Subject: [PATCH 19/35] Use indexsets --- chalk-solve/Cargo.toml | 2 +- chalk-solve/src/rust_ir.rs | 25 +++++++++++++++---------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/chalk-solve/Cargo.toml b/chalk-solve/Cargo.toml index bde17cef6cd..756e9010143 100644 --- a/chalk-solve/Cargo.toml +++ b/chalk-solve/Cargo.toml @@ -20,7 +20,7 @@ rustc-hash = { version = "1.1.0" } chalk-derive = { version = "0.76.0-dev.0", path = "../chalk-derive" } chalk-ir = { version = "0.76.0-dev.0", path = "../chalk-ir" } -indexmap = "1.5.1" +indexmap = "1.7.0" [dev-dependencies] chalk-integration = { path = "../chalk-integration" } diff --git a/chalk-solve/src/rust_ir.rs b/chalk-solve/src/rust_ir.rs index 612d05203e9..9c949d0a165 100644 --- a/chalk-solve/src/rust_ir.rs +++ b/chalk-solve/src/rust_ir.rs @@ -11,6 +11,7 @@ use chalk_ir::{ GenericArg, ImplId, OpaqueTyId, ProjectionTy, QuantifiedWhereClause, Substitution, ToGenericArg, TraitId, TraitRef, Ty, TyKind, VariableKind, WhereClause, WithKind, }; +use indexmap::IndexSet; use std::iter; use std::ops::ControlFlow; @@ -55,7 +56,7 @@ impl ImplDatum { #[derive(Clone, Debug, PartialEq, Eq, Hash, HasInterner, Fold, Visit)] pub struct ImplDatumBound { pub trait_ref: TraitRef, - pub where_clauses: Vec>, + pub where_clauses: IndexSet>, } #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] @@ -97,7 +98,7 @@ chalk_ir::const_visit!(AdtKind); #[derive(Clone, Debug, PartialEq, Eq, Hash, Fold, HasInterner, Visit)] pub struct AdtDatumBound { pub variants: Vec>, - pub where_clauses: Vec>, + pub where_clauses: IndexSet>, } #[derive(Clone, Debug, PartialEq, Eq, Hash, Fold, HasInterner, Visit)] @@ -194,7 +195,7 @@ pub struct FnDefDatumBound { /// fn foo() where T: Eq; /// ^^^^^^^^^^^ /// ``` - pub where_clauses: Vec>, + pub where_clauses: IndexSet>, } #[derive(Clone, Debug, PartialEq, Eq, Hash)] @@ -282,7 +283,7 @@ impl TraitDatum { /// trait Foo where T: Debug { } /// ^^^^^^^^^^^^^^ /// ``` - pub fn where_clauses(&self) -> Binders<&Vec>> { + pub fn where_clauses(&self) -> Binders<&IndexSet>> { self.binders.as_ref().map(|td| &td.where_clauses) } } @@ -295,7 +296,7 @@ pub struct TraitDatumBound { /// trait Foo where T: Debug { } /// ^^^^^^^^^^^^^^ /// ``` - pub where_clauses: Vec>, + pub where_clauses: IndexSet>, } #[derive(Clone, Debug, PartialEq, Eq, Hash)] @@ -368,7 +369,11 @@ impl IntoWhereClauses for InlineBound { impl IntoWhereClauses for QuantifiedInlineBound { type Output = QuantifiedWhereClause; - fn into_where_clauses(&self, interner: I, self_ty: Ty) -> Vec> { + fn into_where_clauses( + &self, + interner: I, + self_ty: Ty, + ) -> IndexSet> { let self_ty = self_ty.shifted_in(interner); self.map_ref(|b| b.into_where_clauses(interner, self_ty)) .into_iter() @@ -510,7 +515,7 @@ pub struct AssociatedTyDatumBound { pub bounds: Vec>, /// Where clauses that must hold for the projection to be well-formed. - pub where_clauses: Vec>, + pub where_clauses: IndexSet>, } impl AssociatedTyDatum { @@ -522,7 +527,7 @@ impl AssociatedTyDatum { /// /// these quantified where clauses are in the scope of the /// `binders` field. - pub fn bounds_on_self(&self, interner: I) -> Vec> { + pub fn bounds_on_self(&self, interner: I) -> IndexSet> { let (binders, assoc_ty_datum) = self.binders.as_ref().into(); // Create a list `P0...Pn` of references to the binders in // scope for this associated type: @@ -625,11 +630,11 @@ pub struct OpaqueTyDatum { #[derive(Clone, Debug, PartialEq, Eq, Hash, Fold, HasInterner, Visit)] pub struct OpaqueTyDatumBound { /// Trait bounds for the opaque type. These are bounds that the hidden type must meet. - pub bounds: Binders>>, + pub bounds: Binders>>, /// Where clauses that inform well-formedness conditions for the opaque type. /// These are conditions on the generic parameters of the opaque type which must be true /// for a reference to the opaque type to be well-formed. - pub where_clauses: Binders>>, + pub where_clauses: Binders>>, } // The movability of a generator: whether a generator contains self-references, From b3a8152c97ce2726de981c0800e5f12a21d2f133 Mon Sep 17 00:00:00 2001 From: pierwill Date: Wed, 15 Dec 2021 15:54:32 -0600 Subject: [PATCH 20/35] Revert "Use indexsets" This reverts commit aa6c89e6cff5e9d4bd55efe2ed74dc64cff4be68. --- chalk-solve/Cargo.toml | 2 +- chalk-solve/src/rust_ir.rs | 25 ++++++++++--------------- 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/chalk-solve/Cargo.toml b/chalk-solve/Cargo.toml index 756e9010143..bde17cef6cd 100644 --- a/chalk-solve/Cargo.toml +++ b/chalk-solve/Cargo.toml @@ -20,7 +20,7 @@ rustc-hash = { version = "1.1.0" } chalk-derive = { version = "0.76.0-dev.0", path = "../chalk-derive" } chalk-ir = { version = "0.76.0-dev.0", path = "../chalk-ir" } -indexmap = "1.7.0" +indexmap = "1.5.1" [dev-dependencies] chalk-integration = { path = "../chalk-integration" } diff --git a/chalk-solve/src/rust_ir.rs b/chalk-solve/src/rust_ir.rs index 9c949d0a165..612d05203e9 100644 --- a/chalk-solve/src/rust_ir.rs +++ b/chalk-solve/src/rust_ir.rs @@ -11,7 +11,6 @@ use chalk_ir::{ GenericArg, ImplId, OpaqueTyId, ProjectionTy, QuantifiedWhereClause, Substitution, ToGenericArg, TraitId, TraitRef, Ty, TyKind, VariableKind, WhereClause, WithKind, }; -use indexmap::IndexSet; use std::iter; use std::ops::ControlFlow; @@ -56,7 +55,7 @@ impl ImplDatum { #[derive(Clone, Debug, PartialEq, Eq, Hash, HasInterner, Fold, Visit)] pub struct ImplDatumBound { pub trait_ref: TraitRef, - pub where_clauses: IndexSet>, + pub where_clauses: Vec>, } #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] @@ -98,7 +97,7 @@ chalk_ir::const_visit!(AdtKind); #[derive(Clone, Debug, PartialEq, Eq, Hash, Fold, HasInterner, Visit)] pub struct AdtDatumBound { pub variants: Vec>, - pub where_clauses: IndexSet>, + pub where_clauses: Vec>, } #[derive(Clone, Debug, PartialEq, Eq, Hash, Fold, HasInterner, Visit)] @@ -195,7 +194,7 @@ pub struct FnDefDatumBound { /// fn foo() where T: Eq; /// ^^^^^^^^^^^ /// ``` - pub where_clauses: IndexSet>, + pub where_clauses: Vec>, } #[derive(Clone, Debug, PartialEq, Eq, Hash)] @@ -283,7 +282,7 @@ impl TraitDatum { /// trait Foo where T: Debug { } /// ^^^^^^^^^^^^^^ /// ``` - pub fn where_clauses(&self) -> Binders<&IndexSet>> { + pub fn where_clauses(&self) -> Binders<&Vec>> { self.binders.as_ref().map(|td| &td.where_clauses) } } @@ -296,7 +295,7 @@ pub struct TraitDatumBound { /// trait Foo where T: Debug { } /// ^^^^^^^^^^^^^^ /// ``` - pub where_clauses: IndexSet>, + pub where_clauses: Vec>, } #[derive(Clone, Debug, PartialEq, Eq, Hash)] @@ -369,11 +368,7 @@ impl IntoWhereClauses for InlineBound { impl IntoWhereClauses for QuantifiedInlineBound { type Output = QuantifiedWhereClause; - fn into_where_clauses( - &self, - interner: I, - self_ty: Ty, - ) -> IndexSet> { + fn into_where_clauses(&self, interner: I, self_ty: Ty) -> Vec> { let self_ty = self_ty.shifted_in(interner); self.map_ref(|b| b.into_where_clauses(interner, self_ty)) .into_iter() @@ -515,7 +510,7 @@ pub struct AssociatedTyDatumBound { pub bounds: Vec>, /// Where clauses that must hold for the projection to be well-formed. - pub where_clauses: IndexSet>, + pub where_clauses: Vec>, } impl AssociatedTyDatum { @@ -527,7 +522,7 @@ impl AssociatedTyDatum { /// /// these quantified where clauses are in the scope of the /// `binders` field. - pub fn bounds_on_self(&self, interner: I) -> IndexSet> { + pub fn bounds_on_self(&self, interner: I) -> Vec> { let (binders, assoc_ty_datum) = self.binders.as_ref().into(); // Create a list `P0...Pn` of references to the binders in // scope for this associated type: @@ -630,11 +625,11 @@ pub struct OpaqueTyDatum { #[derive(Clone, Debug, PartialEq, Eq, Hash, Fold, HasInterner, Visit)] pub struct OpaqueTyDatumBound { /// Trait bounds for the opaque type. These are bounds that the hidden type must meet. - pub bounds: Binders>>, + pub bounds: Binders>>, /// Where clauses that inform well-formedness conditions for the opaque type. /// These are conditions on the generic parameters of the opaque type which must be true /// for a reference to the opaque type to be well-formed. - pub where_clauses: Binders>>, + pub where_clauses: Binders>>, } // The movability of a generator: whether a generator contains self-references, From 6f6e3bec17e4c3f29b76ef988b16665e90fee7da Mon Sep 17 00:00:00 2001 From: pierwill Date: Wed, 15 Dec 2021 15:54:35 -0600 Subject: [PATCH 21/35] Revert "Use IndexSet in lowering.rs" This reverts commit a8525b6514cb3a00f3bed0fa8cc4833b6915aa2d. --- chalk-integration/src/lowering.rs | 52 +++++++++++-------------------- 1 file changed, 19 insertions(+), 33 deletions(-) diff --git a/chalk-integration/src/lowering.rs b/chalk-integration/src/lowering.rs index eab14055309..7c77f7c9fa4 100644 --- a/chalk-integration/src/lowering.rs +++ b/chalk-integration/src/lowering.rs @@ -136,24 +136,20 @@ impl Lower for VariableKind { } impl LowerWithEnv for [QuantifiedWhereClause] { - type Lowered = IndexSet>; + type Lowered = Vec>; fn lower(&self, env: &Env) -> LowerResult { self.iter() .flat_map(|wc| match wc.lower(env) { Ok(v) => v.into_iter().map(Ok).collect(), - Err(e) => { - let mut set = IndexSet::new(); - set.insert([Err(e)]); - set - } + Err(e) => vec![Err(e)], }) - .collect::>() + .collect() } } impl LowerWithEnv for WhereClause { - type Lowered = IndexSet>; + type Lowered = Vec>; /// Lower from an AST `where` clause to an internal IR. /// Some AST `where` clauses can lower to multiple ones, this is why we return a `Vec`. @@ -162,47 +158,37 @@ impl LowerWithEnv for WhereClause { fn lower(&self, env: &Env) -> LowerResult { Ok(match self { WhereClause::Implemented { trait_ref } => { - let mut set = IndexSet::new(); - set.insert(chalk_ir::WhereClause::Implemented(trait_ref.lower(env)?)); - set - } - WhereClause::ProjectionEq { projection, ty } => { - let mut set = IndexSet::new(); - set.insert( - chalk_ir::WhereClause::AliasEq(chalk_ir::AliasEq { - alias: chalk_ir::AliasTy::Projection(projection.lower(env)?), - ty: ty.lower(env)?, - }), - chalk_ir::WhereClause::Implemented(projection.trait_ref.lower(env)?), - ); - set + vec![chalk_ir::WhereClause::Implemented(trait_ref.lower(env)?)] } + WhereClause::ProjectionEq { projection, ty } => vec![ + chalk_ir::WhereClause::AliasEq(chalk_ir::AliasEq { + alias: chalk_ir::AliasTy::Projection(projection.lower(env)?), + ty: ty.lower(env)?, + }), + chalk_ir::WhereClause::Implemented(projection.trait_ref.lower(env)?), + ], WhereClause::LifetimeOutlives { a, b } => { - let mut set = IndexSet::new(); - set.insert(chalk_ir::WhereClause::LifetimeOutlives( + vec![chalk_ir::WhereClause::LifetimeOutlives( chalk_ir::LifetimeOutlives { a: a.lower(env)?, b: b.lower(env)?, }, - )); - set + )] } WhereClause::TypeOutlives { ty, lifetime } => { - let mut set = IndexSet::new(); - set.insert(chalk_ir::WhereClause::TypeOutlives( + vec![chalk_ir::WhereClause::TypeOutlives( chalk_ir::TypeOutlives { ty: ty.lower(env)?, lifetime: lifetime.lower(env)?, }, - )); - set + )] } }) } } impl LowerWithEnv for QuantifiedWhereClause { - type Lowered = IndexSet>; + type Lowered = Vec>; /// Lower from an AST `where` clause to an internal IR. /// Some AST `where` clauses can lower to multiple ones, this is why we return a `Vec`. @@ -211,7 +197,7 @@ impl LowerWithEnv for QuantifiedWhereClause { fn lower(&self, env: &Env) -> LowerResult { let variable_kinds = self.variable_kinds.iter().map(|k| k.lower()); let binders = env.in_binders(variable_kinds, |env| Ok(self.where_clause.lower(env)?))?; - Ok(binders.into_iter().collect::>()) + Ok(binders.into_iter().collect()) } } @@ -587,7 +573,7 @@ impl LowerWithEnv for [QuantifiedInlineBound] { .iter() .chain(auto_traits.iter()) .map(|(b, _)| b.lower(env)) - .collect::>() + .collect() } } From 5029521dccdad8bd2db161d2a592239561506427 Mon Sep 17 00:00:00 2001 From: pierwill Date: Wed, 15 Dec 2021 15:54:36 -0600 Subject: [PATCH 22/35] Revert "AST: use IndexSet for `where_clause`s" This reverts commit b964a3a009ec42a3157467c6e48cb91cdff03720. --- chalk-parse/src/ast.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/chalk-parse/src/ast.rs b/chalk-parse/src/ast.rs index 9a60a93b4bb..c429ba7dc35 100644 --- a/chalk-parse/src/ast.rs +++ b/chalk-parse/src/ast.rs @@ -39,7 +39,7 @@ pub struct ForeignDefn(pub Identifier); pub struct AdtDefn { pub name: Identifier, pub variable_kinds: Vec, - pub where_clauses: IndexSet, + pub where_clauses: Vec, pub variants: Vec, pub flags: AdtFlags, pub repr: AdtRepr, @@ -110,7 +110,7 @@ pub struct FnSig { pub struct FnDefn { pub name: Identifier, pub variable_kinds: Vec, - pub where_clauses: IndexSet, + pub where_clauses: Vec, pub argument_types: Vec, pub return_type: Ty, pub sig: FnSig, @@ -140,7 +140,7 @@ impl Default for FnAbi { pub struct TraitDefn { pub name: Identifier, pub variable_kinds: Vec, - pub where_clauses: IndexSet, + pub where_clauses: Vec, pub assoc_ty_defns: Vec, pub flags: TraitFlags, pub well_known: Option, @@ -178,7 +178,7 @@ pub struct AssocTyDefn { pub name: Identifier, pub variable_kinds: Vec, pub bounds: Vec, - pub where_clauses: IndexSet, + pub where_clauses: Vec, } #[derive(Clone, PartialEq, Eq, Debug)] @@ -266,7 +266,7 @@ pub struct Impl { pub variable_kinds: Vec, pub trait_ref: TraitRef, pub polarity: Polarity, - pub where_clauses: IndexSet, + pub where_clauses: Vec, pub assoc_ty_values: Vec, pub impl_type: ImplType, } From f1e4ef6e6ebcf5740a644e663c99f01e9ea2a9e3 Mon Sep 17 00:00:00 2001 From: pierwill Date: Wed, 15 Dec 2021 15:54:37 -0600 Subject: [PATCH 23/35] Revert "parser.lalrpop: use IndexSet for QuantifiedWhereClauses" This reverts commit cd602c694a33752d01e5888eb504a0552c634c01. --- chalk-parse/src/parser.lalrpop | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/chalk-parse/src/parser.lalrpop b/chalk-parse/src/parser.lalrpop index 70c59c85f45..8f20258560e 100644 --- a/chalk-parse/src/parser.lalrpop +++ b/chalk-parse/src/parser.lalrpop @@ -1,6 +1,5 @@ use crate::ast::*; use string_cache::DefaultAtom as Atom; -use indexmap::IndexSet; grammar; @@ -600,9 +599,9 @@ QuantifiedWhereClause: QuantifiedWhereClause = { }, }; -QuantifiedWhereClauses: IndexSet = { +QuantifiedWhereClauses: Vec = { "where" >, - () => IndexSet::new(), + () => vec![], }; DomainGoal: DomainGoal = { From 5203a21dc181f97c8775f1eb49ca4cd6c926f721 Mon Sep 17 00:00:00 2001 From: pierwill Date: Wed, 15 Dec 2021 15:54:38 -0600 Subject: [PATCH 24/35] Revert "Use indexset in impl LowerWithEnv for [QuantifiedInlineBound]" This reverts commit cfe63d7e9e6b36084b53aa76496641834dd00620. --- chalk-integration/src/lowering.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/chalk-integration/src/lowering.rs b/chalk-integration/src/lowering.rs index 7c77f7c9fa4..1677d8be925 100644 --- a/chalk-integration/src/lowering.rs +++ b/chalk-integration/src/lowering.rs @@ -8,7 +8,7 @@ use chalk_ir::{ }; use chalk_parse::ast::*; use chalk_solve::rust_ir::{self, IntoWhereClauses}; -use indexmap::{IndexMap, IndexSet}; +use indexmap::IndexMap; use program_lowerer::ProgramLowerer; use string_cache::DefaultAtom as Atom; use tracing::debug; @@ -542,7 +542,7 @@ impl LowerWithEnv for QuantifiedInlineBound { } impl LowerWithEnv for [QuantifiedInlineBound] { - type Lowered = IndexSet>; + type Lowered = Vec>; fn lower(&self, env: &Env) -> LowerResult { fn trait_identifier(bound: &InlineBound) -> &Identifier { @@ -552,15 +552,15 @@ impl LowerWithEnv for [QuantifiedInlineBound] { } } - let mut regular_traits = IndexSet::new(); - let mut auto_traits = IndexSet::new(); + let mut regular_traits = Vec::new(); + let mut auto_traits = Vec::new(); for b in self { let id = env.lookup_trait(trait_identifier(&b.bound))?; if env.auto_trait(id) { - auto_traits.insert((b, id)) + auto_traits.push((b, id)) } else { - regular_traits.insert((b, id)) + regular_traits.push((b, id)) } } From fa1e5d426cd412d870966b16f30224eb4167c2a0 Mon Sep 17 00:00:00 2001 From: pierwill Date: Wed, 15 Dec 2021 15:54:39 -0600 Subject: [PATCH 25/35] Revert "Use indexset for where_clauses" This reverts commit 7a80b58154bfbd72f1b8b0844ad151401b32929f. --- chalk-parse/src/ast.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/chalk-parse/src/ast.rs b/chalk-parse/src/ast.rs index c429ba7dc35..b6188b693fd 100644 --- a/chalk-parse/src/ast.rs +++ b/chalk-parse/src/ast.rs @@ -1,4 +1,3 @@ -use indexmap::IndexSet; use std::fmt; use string_cache::DefaultAtom as Atom; @@ -187,7 +186,7 @@ pub struct OpaqueTyDefn { pub variable_kinds: Vec, pub name: Identifier, pub bounds: Vec, - pub where_clauses: IndexSet, + pub where_clauses: Vec, } #[derive(Clone, PartialEq, Eq, Debug, Hash)] From 0cd329fd8a77e80091bc0ab0f3cdf6ec0873f8e0 Mon Sep 17 00:00:00 2001 From: pierwill Date: Wed, 15 Dec 2021 15:54:40 -0600 Subject: [PATCH 26/35] Revert "Add Hash traits where needed" This reverts commit 75f82830a227d7f1f36621149633ba8bf8168669. --- chalk-parse/src/ast.rs | 46 +++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/chalk-parse/src/ast.rs b/chalk-parse/src/ast.rs index b6188b693fd..0f0e9c54663 100644 --- a/chalk-parse/src/ast.rs +++ b/chalk-parse/src/ast.rs @@ -1,7 +1,7 @@ use std::fmt; use string_cache::DefaultAtom as Atom; -#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)] +#[derive(Copy, Clone, PartialEq, Eq, Debug)] pub struct Span { pub lo: usize, pub hi: usize, @@ -98,7 +98,7 @@ pub struct AdtRepr { pub int: Option, } -#[derive(Clone, PartialEq, Eq, Debug, Hash)] +#[derive(Clone, PartialEq, Eq, Debug)] pub struct FnSig { pub abi: FnAbi, pub safety: Safety, @@ -126,7 +126,7 @@ pub struct ClosureDefn { pub upvars: Vec, } -#[derive(Clone, Eq, PartialEq, Debug, Hash)] +#[derive(Clone, Eq, PartialEq, Debug)] pub struct FnAbi(pub Atom); impl Default for FnAbi { @@ -189,7 +189,7 @@ pub struct OpaqueTyDefn { pub where_clauses: Vec, } -#[derive(Clone, PartialEq, Eq, Debug, Hash)] +#[derive(Clone, PartialEq, Eq, Debug)] pub enum VariableKind { Ty(Identifier), IntegerTy(Identifier), @@ -198,7 +198,7 @@ pub enum VariableKind { Const(Identifier), } -#[derive(Clone, PartialEq, Eq, Debug, Hash)] +#[derive(Clone, PartialEq, Eq, Debug)] pub enum GenericArg { Ty(Ty), Lifetime(Lifetime), @@ -206,26 +206,26 @@ pub enum GenericArg { Const(Const), } -#[derive(Clone, PartialEq, Eq, Debug, Hash)] +#[derive(Clone, PartialEq, Eq, Debug)] pub enum Const { Id(Identifier), Value(u32), } -#[derive(Clone, PartialEq, Eq, Debug, Hash)] +#[derive(Clone, PartialEq, Eq, Debug)] /// An inline bound, e.g. `: Foo` in `impl> SomeType`. pub enum InlineBound { TraitBound(TraitBound), AliasEqBound(AliasEqBound), } -#[derive(Clone, PartialEq, Eq, Debug, Hash)] +#[derive(Clone, PartialEq, Eq, Debug)] pub struct QuantifiedInlineBound { pub variable_kinds: Vec, pub bound: InlineBound, } -#[derive(Clone, PartialEq, Eq, Debug, Hash)] +#[derive(Clone, PartialEq, Eq, Debug)] /// Represents a trait bound on e.g. a type or type parameter. /// Does not know anything about what it's binding. pub struct TraitBound { @@ -233,7 +233,7 @@ pub struct TraitBound { pub args_no_self: Vec, } -#[derive(Clone, PartialEq, Eq, Debug, Hash)] +#[derive(Clone, PartialEq, Eq, Debug)] /// Represents an alias equality bound on e.g. a type or type parameter. /// Does not know anything about what it's binding. pub struct AliasEqBound { @@ -284,7 +284,7 @@ pub struct AssocTyValue { pub default: bool, } -#[derive(Clone, PartialEq, Eq, Debug, Hash)] +#[derive(Clone, PartialEq, Eq, Debug)] pub enum Ty { Id { name: Identifier, @@ -331,7 +331,7 @@ pub enum Ty { Never, } -#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)] +#[derive(Copy, Clone, PartialEq, Eq, Debug)] pub enum IntTy { Isize, I8, @@ -341,7 +341,7 @@ pub enum IntTy { I128, } -#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)] +#[derive(Copy, Clone, PartialEq, Eq, Debug)] pub enum UintTy { Usize, U8, @@ -351,13 +351,13 @@ pub enum UintTy { U128, } -#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)] +#[derive(Copy, Clone, PartialEq, Eq, Debug)] pub enum FloatTy { F32, F64, } -#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)] +#[derive(Copy, Clone, PartialEq, Eq, Debug)] pub enum ScalarType { Bool, Char, @@ -366,13 +366,13 @@ pub enum ScalarType { Float(FloatTy), } -#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)] +#[derive(Copy, Clone, PartialEq, Eq, Debug)] pub enum Mutability { Mut, Not, } -#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)] +#[derive(Copy, Clone, PartialEq, Eq, Debug)] pub enum Safety { Safe, Unsafe, @@ -384,7 +384,7 @@ impl Default for Safety { } } -#[derive(Clone, PartialEq, Eq, Debug, Hash)] +#[derive(Clone, PartialEq, Eq, Debug)] pub enum Lifetime { Id { name: Identifier }, Static, @@ -392,14 +392,14 @@ pub enum Lifetime { Empty, } -#[derive(Clone, PartialEq, Eq, Debug, Hash)] +#[derive(Clone, PartialEq, Eq, Debug)] pub struct ProjectionTy { pub trait_ref: TraitRef, pub name: Identifier, pub args: Vec, } -#[derive(Clone, PartialEq, Eq, Debug, Hash)] +#[derive(Clone, PartialEq, Eq, Debug)] pub struct TraitRef { pub trait_name: Identifier, pub args: Vec, @@ -424,7 +424,7 @@ impl Polarity { } } -#[derive(Clone, PartialEq, Eq, Debug, Hash)] +#[derive(Clone, PartialEq, Eq, Debug)] pub struct Identifier { pub str: Atom, pub span: Span, @@ -436,7 +436,7 @@ impl fmt::Display for Identifier { } } -#[derive(Clone, PartialEq, Eq, Debug, Hash)] +#[derive(Clone, PartialEq, Eq, Debug)] pub enum WhereClause { Implemented { trait_ref: TraitRef }, ProjectionEq { projection: ProjectionTy, ty: Ty }, @@ -469,7 +469,7 @@ pub enum LeafGoal { SubtypeGenericArgs { a: Ty, b: Ty }, } -#[derive(Clone, PartialEq, Eq, Debug, Hash)] +#[derive(Clone, PartialEq, Eq, Debug)] pub struct QuantifiedWhereClause { pub variable_kinds: Vec, pub where_clause: WhereClause, From 51422d97245fb4efce4af9f50fb0b808ab47c1c9 Mon Sep 17 00:00:00 2001 From: pierwill Date: Wed, 15 Dec 2021 15:54:41 -0600 Subject: [PATCH 27/35] Revert "Add indexmap to chalk-parse" This reverts commit 6408a3a58c6c53208f207ac55061682aec4ea228. --- Cargo.lock | 1 - chalk-parse/Cargo.toml | 1 - 2 files changed, 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 35488894134..5fdf3e898fa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -161,7 +161,6 @@ dependencies = [ name = "chalk-parse" version = "0.76.0-dev.0" dependencies = [ - "indexmap", "lalrpop", "lalrpop-util", "regex", diff --git a/chalk-parse/Cargo.toml b/chalk-parse/Cargo.toml index 9b367239a80..e23b2b3b292 100644 --- a/chalk-parse/Cargo.toml +++ b/chalk-parse/Cargo.toml @@ -16,7 +16,6 @@ version = "0.19" features = ["lexer"] [dependencies] -indexmap = "1.7.0" lalrpop-util = "0.19" regex = "1.3" string_cache = "0.8.0" From 0525fc11dc1fd901368f381a022a35cddd29b434 Mon Sep 17 00:00:00 2001 From: pierwill Date: Wed, 15 Dec 2021 15:54:41 -0600 Subject: [PATCH 28/35] Revert "Add fixme notice" This reverts commit 8f1d6e858dc6d193925699348c76a65736350f7a. --- chalk-integration/src/lowering.rs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/chalk-integration/src/lowering.rs b/chalk-integration/src/lowering.rs index 1677d8be925..7c00bab8e45 100644 --- a/chalk-integration/src/lowering.rs +++ b/chalk-integration/src/lowering.rs @@ -564,11 +564,6 @@ impl LowerWithEnv for [QuantifiedInlineBound] { } } - // FIXME: removing the line below is causing - // test::unsize::dyn_to_dyn_unsizing to fail. - // - // auto_traits.sort_by_key(|b| b.1); - regular_traits .iter() .chain(auto_traits.iter()) From 53003644a8034ce8e1eac91feff681fdfe92aa87 Mon Sep 17 00:00:00 2001 From: pierwill Date: Wed, 15 Dec 2021 16:34:20 -0600 Subject: [PATCH 29/35] Impl HasInterner for IndexSet --- chalk-ir/Cargo.toml | 1 + chalk-ir/src/interner.rs | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/chalk-ir/Cargo.toml b/chalk-ir/Cargo.toml index 783b701d446..488c325acf5 100644 --- a/chalk-ir/Cargo.toml +++ b/chalk-ir/Cargo.toml @@ -13,3 +13,4 @@ edition = "2018" lazy_static = "1.4.0" bitflags = "1.2.1" chalk-derive = { version = "0.76.0-dev.0", path = "../chalk-derive" } +indexmap = "1.7.0" diff --git a/chalk-ir/src/interner.rs b/chalk-ir/src/interner.rs index f8b3c58980f..af771c739a4 100644 --- a/chalk-ir/src/interner.rs +++ b/chalk-ir/src/interner.rs @@ -665,6 +665,10 @@ pub trait HasInterner { type Interner: Interner; } +impl HasInterner for indexmap::IndexSet { + type Interner = T::Interner; +} + impl HasInterner for [T] { type Interner = T::Interner; } From 76fd0108cf5fa62468cd2f1e219b5c14b565bfd4 Mon Sep 17 00:00:00 2001 From: pierwill Date: Wed, 15 Dec 2021 16:52:54 -0600 Subject: [PATCH 30/35] Convert from IndexSet to Vec in lowering --- Cargo.lock | 1 + chalk-integration/src/lowering.rs | 65 +++++++++++++------ .../src/lowering/program_lowerer.rs | 21 ++++-- 3 files changed, 62 insertions(+), 25 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5fdf3e898fa..fa2e2dfbb07 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -154,6 +154,7 @@ version = "0.76.0-dev.0" dependencies = [ "bitflags", "chalk-derive", + "indexmap", "lazy_static", ] diff --git a/chalk-integration/src/lowering.rs b/chalk-integration/src/lowering.rs index 7c00bab8e45..b0efe195bdc 100644 --- a/chalk-integration/src/lowering.rs +++ b/chalk-integration/src/lowering.rs @@ -9,6 +9,7 @@ use chalk_ir::{ use chalk_parse::ast::*; use chalk_solve::rust_ir::{self, IntoWhereClauses}; use indexmap::IndexMap; +use indexmap::IndexSet; use program_lowerer::ProgramLowerer; use string_cache::DefaultAtom as Atom; use tracing::debug; @@ -53,7 +54,7 @@ impl Lower for Program { trait LowerParameterMap { fn synthetic_parameters(&self) -> Option>; fn declared_parameters(&self) -> &[VariableKind]; - fn all_parameters(&self) -> Vec> { + fn all_parameters(&self) -> indexmap::IndexSet> { self.synthetic_parameters() .into_iter() .chain(self.declared_parameters().iter().map(|id| id.lower())) @@ -136,7 +137,7 @@ impl Lower for VariableKind { } impl LowerWithEnv for [QuantifiedWhereClause] { - type Lowered = Vec>; + type Lowered = IndexSet>; fn lower(&self, env: &Env) -> LowerResult { self.iter() @@ -149,7 +150,7 @@ impl LowerWithEnv for [QuantifiedWhereClause] { } impl LowerWithEnv for WhereClause { - type Lowered = Vec>; + type Lowered = IndexSet>; /// Lower from an AST `where` clause to an internal IR. /// Some AST `where` clauses can lower to multiple ones, this is why we return a `Vec`. @@ -158,37 +159,47 @@ impl LowerWithEnv for WhereClause { fn lower(&self, env: &Env) -> LowerResult { Ok(match self { WhereClause::Implemented { trait_ref } => { - vec![chalk_ir::WhereClause::Implemented(trait_ref.lower(env)?)] + let mut set = IndexSet::new(); + set.insert(chalk_ir::WhereClause::Implemented(trait_ref.lower(env)?)); + set } - WhereClause::ProjectionEq { projection, ty } => vec![ - chalk_ir::WhereClause::AliasEq(chalk_ir::AliasEq { + WhereClause::ProjectionEq { projection, ty } => { + let mut set = IndexSet::new(); + set.insert(chalk_ir::WhereClause::AliasEq(chalk_ir::AliasEq { alias: chalk_ir::AliasTy::Projection(projection.lower(env)?), ty: ty.lower(env)?, - }), - chalk_ir::WhereClause::Implemented(projection.trait_ref.lower(env)?), - ], + })); + set.insert(chalk_ir::WhereClause::Implemented( + projection.trait_ref.lower(env)?, + )); + set + } WhereClause::LifetimeOutlives { a, b } => { - vec![chalk_ir::WhereClause::LifetimeOutlives( + let mut set = IndexSet::new(); + set.insert(chalk_ir::WhereClause::LifetimeOutlives( chalk_ir::LifetimeOutlives { a: a.lower(env)?, b: b.lower(env)?, }, - )] + )); + set } WhereClause::TypeOutlives { ty, lifetime } => { - vec![chalk_ir::WhereClause::TypeOutlives( + let mut set = IndexSet::new(); + set.insert(chalk_ir::WhereClause::TypeOutlives( chalk_ir::TypeOutlives { ty: ty.lower(env)?, lifetime: lifetime.lower(env)?, }, - )] + )); + set } }) } } impl LowerWithEnv for QuantifiedWhereClause { - type Lowered = Vec>; + type Lowered = IndexSet>; /// Lower from an AST `where` clause to an internal IR. /// Some AST `where` clauses can lower to multiple ones, this is why we return a `Vec`. @@ -298,7 +309,11 @@ impl LowerWithEnv for (&AdtDefn, chalk_ir::AdtId) { Ok(rust_ir::AdtVariantDatum { fields: fields? }) }) .collect::>()?, - where_clauses: adt_defn.where_clauses.lower(env)?, + where_clauses: adt_defn + .where_clauses + .lower(env)? + .into_iter() + .collect::>(), }) })?; @@ -340,7 +355,11 @@ impl LowerWithEnv for (&FnDefn, chalk_ir::FnDefId) { let (fn_defn, fn_def_id) = self; let binders = env.in_binders(fn_defn.all_parameters(), |env| { - let where_clauses = fn_defn.where_clauses.lower(env)?; + let where_clauses = fn_defn + .where_clauses + .lower(env)? + .into_iter() + .collect::>(); let inputs_and_output = env.in_binders(vec![], |env| { let args: LowerResult<_> = fn_defn @@ -542,7 +561,7 @@ impl LowerWithEnv for QuantifiedInlineBound { } impl LowerWithEnv for [QuantifiedInlineBound] { - type Lowered = Vec>; + type Lowered = IndexSet>; fn lower(&self, env: &Env) -> LowerResult { fn trait_identifier(bound: &InlineBound) -> &Identifier { @@ -899,7 +918,11 @@ impl LowerWithEnv for (&Impl, ImplId, &AssociatedTyValueIds) { ))?; } - let where_clauses = impl_.where_clauses.lower(&env)?; + let where_clauses = impl_ + .where_clauses + .lower(&env)? + .into_iter() + .collect::>(); debug!(where_clauses = ?trait_ref); Ok(rust_ir::ImplDatumBound { trait_ref, @@ -986,7 +1009,11 @@ impl LowerWithEnv for (&TraitDefn, chalk_ir::TraitId) { } Ok(rust_ir::TraitDatumBound { - where_clauses: trait_defn.where_clauses.lower(env)?, + where_clauses: trait_defn + .where_clauses + .lower(env)? + .into_iter() + .collect::>(), }) })?; diff --git a/chalk-integration/src/lowering/program_lowerer.rs b/chalk-integration/src/lowering/program_lowerer.rs index 6ccf0cf814f..78f51fc1e64 100644 --- a/chalk-integration/src/lowering/program_lowerer.rs +++ b/chalk-integration/src/lowering/program_lowerer.rs @@ -66,7 +66,10 @@ impl ProgramLowerer { let addl_variable_kinds = defn.all_parameters(); let lookup = AssociatedTyLookup { id: AssocTypeId(self.next_item_id()), - addl_variable_kinds: addl_variable_kinds.anonymize(), + addl_variable_kinds: addl_variable_kinds + .into_iter() + .collect::>() + .anonymize(), }; self.associated_ty_lookups .insert((TraitId(raw_id), defn.name.str.clone()), lookup); @@ -295,9 +298,13 @@ impl ProgramLowerer { variable_kinds.extend(trait_defn.all_parameters()); let binders = empty_env.in_binders(variable_kinds, |env| { + let i1 = assoc_ty_defn.bounds.lower(&env)?; + let i2 = assoc_ty_defn.where_clauses.lower(&env)?; + let v1 = i1.into_iter().collect::>(); + let v2 = i2.into_iter().collect::>(); Ok(rust_ir::AssociatedTyDatumBound { - bounds: assoc_ty_defn.bounds.lower(&env)?, - where_clauses: assoc_ty_defn.where_clauses.lower(&env)?, + bounds: v1, + where_clauses: v2, }) })?; @@ -393,8 +400,7 @@ impl ProgramLowerer { .collect()) }, )?; - let where_clauses: chalk_ir::Binders>> = env - .in_binders( + let where_clauses = env.in_binders( Some(chalk_ir::WithKind::new( chalk_ir::VariableKind::Ty(TyVariableKind::General), Atom::from(FIXME_SELF), @@ -518,7 +524,10 @@ macro_rules! lower_type_kind { sort: TypeSort::$sort, name: self.name.str.clone(), binders: chalk_ir::Binders::new( - VariableKinds::from_iter(ChalkIr, $params(self).anonymize()), + VariableKinds::from_iter( + ChalkIr, + $params(self).into_iter().collect::>().anonymize(), + ), crate::Unit, ), }) From 72c573eb046c976bd1195e5b1ae801157d051b06 Mon Sep 17 00:00:00 2001 From: pierwill Date: Wed, 15 Dec 2021 17:00:45 -0600 Subject: [PATCH 31/35] Finish lowering conversions --- chalk-integration/src/lowering/program_lowerer.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/chalk-integration/src/lowering/program_lowerer.rs b/chalk-integration/src/lowering/program_lowerer.rs index 78f51fc1e64..4a28a4813a7 100644 --- a/chalk-integration/src/lowering/program_lowerer.rs +++ b/chalk-integration/src/lowering/program_lowerer.rs @@ -400,12 +400,19 @@ impl ProgramLowerer { .collect()) }, )?; + let where_clauses = env.in_binders( Some(chalk_ir::WithKind::new( chalk_ir::VariableKind::Ty(TyVariableKind::General), Atom::from(FIXME_SELF), )), - |env| opaque_ty.where_clauses.lower(env), + |env| { + Ok(opaque_ty + .where_clauses + .lower(env)? + .into_iter() + .collect::>()) + }, )?; Ok(OpaqueTyDatumBound { From 6a8956c7bfb2ff489eefc4035a865cf02078354c Mon Sep 17 00:00:00 2001 From: pierwill Date: Wed, 15 Dec 2021 17:13:23 -0600 Subject: [PATCH 32/35] Another set use --- chalk-integration/src/lowering.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/chalk-integration/src/lowering.rs b/chalk-integration/src/lowering.rs index b0efe195bdc..f78f2690398 100644 --- a/chalk-integration/src/lowering.rs +++ b/chalk-integration/src/lowering.rs @@ -571,15 +571,15 @@ impl LowerWithEnv for [QuantifiedInlineBound] { } } - let mut regular_traits = Vec::new(); - let mut auto_traits = Vec::new(); + let mut regular_traits = IndexSet::new(); + let mut auto_traits = IndexSet::new(); for b in self { let id = env.lookup_trait(trait_identifier(&b.bound))?; if env.auto_trait(id) { - auto_traits.push((b, id)) + auto_traits.insert((b, id)); } else { - regular_traits.push((b, id)) + regular_traits.insert((b, id)); } } From 14f731920abbff5f4c25f40f3b37983743fbdef5 Mon Sep 17 00:00:00 2001 From: pierwill Date: Wed, 15 Dec 2021 17:46:39 -0600 Subject: [PATCH 33/35] another attempt to fix... --- chalk-integration/src/lowering.rs | 4 ++- chalk-parse/src/ast.rs | 42 +++++++++++----------- chalk-solve/src/clauses/program_clauses.rs | 2 +- chalk-solve/src/display/bounds.rs | 6 ++-- chalk-solve/src/rust_ir.rs | 6 ++-- 5 files changed, 31 insertions(+), 29 deletions(-) diff --git a/chalk-integration/src/lowering.rs b/chalk-integration/src/lowering.rs index f78f2690398..d4236a41fd8 100644 --- a/chalk-integration/src/lowering.rs +++ b/chalk-integration/src/lowering.rs @@ -556,7 +556,9 @@ impl LowerWithEnv for QuantifiedInlineBound { fn lower(&self, env: &Env) -> LowerResult { let variable_kinds = self.variable_kinds.iter().map(|k| k.lower()); - env.in_binders(variable_kinds, |env| self.bound.lower(env)) + let x = env.in_binders(variable_kinds, |env| self.bound.lower(env))?; + // + Ok(rust_ir::QuantifiedInlineBound(x)) } } diff --git a/chalk-parse/src/ast.rs b/chalk-parse/src/ast.rs index 0f0e9c54663..7d8d84543b3 100644 --- a/chalk-parse/src/ast.rs +++ b/chalk-parse/src/ast.rs @@ -1,7 +1,7 @@ use std::fmt; use string_cache::DefaultAtom as Atom; -#[derive(Copy, Clone, PartialEq, Eq, Debug)] +#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)] pub struct Span { pub lo: usize, pub hi: usize, @@ -98,7 +98,7 @@ pub struct AdtRepr { pub int: Option, } -#[derive(Clone, PartialEq, Eq, Debug)] +#[derive(Clone, PartialEq, Eq, Debug, Hash)] pub struct FnSig { pub abi: FnAbi, pub safety: Safety, @@ -126,7 +126,7 @@ pub struct ClosureDefn { pub upvars: Vec, } -#[derive(Clone, Eq, PartialEq, Debug)] +#[derive(Clone, Eq, PartialEq, Debug, Hash)] pub struct FnAbi(pub Atom); impl Default for FnAbi { @@ -189,7 +189,7 @@ pub struct OpaqueTyDefn { pub where_clauses: Vec, } -#[derive(Clone, PartialEq, Eq, Debug)] +#[derive(Clone, PartialEq, Eq, Debug, Hash)] pub enum VariableKind { Ty(Identifier), IntegerTy(Identifier), @@ -198,7 +198,7 @@ pub enum VariableKind { Const(Identifier), } -#[derive(Clone, PartialEq, Eq, Debug)] +#[derive(Clone, PartialEq, Eq, Debug, Hash)] pub enum GenericArg { Ty(Ty), Lifetime(Lifetime), @@ -206,26 +206,26 @@ pub enum GenericArg { Const(Const), } -#[derive(Clone, PartialEq, Eq, Debug)] +#[derive(Clone, PartialEq, Eq, Debug, Hash)] pub enum Const { Id(Identifier), Value(u32), } -#[derive(Clone, PartialEq, Eq, Debug)] +#[derive(Clone, PartialEq, Eq, Debug, Hash)] /// An inline bound, e.g. `: Foo` in `impl> SomeType`. pub enum InlineBound { TraitBound(TraitBound), AliasEqBound(AliasEqBound), } -#[derive(Clone, PartialEq, Eq, Debug)] +#[derive(Clone, PartialEq, Eq, Debug, Hash)] pub struct QuantifiedInlineBound { pub variable_kinds: Vec, pub bound: InlineBound, } -#[derive(Clone, PartialEq, Eq, Debug)] +#[derive(Clone, PartialEq, Eq, Debug, Hash)] /// Represents a trait bound on e.g. a type or type parameter. /// Does not know anything about what it's binding. pub struct TraitBound { @@ -233,7 +233,7 @@ pub struct TraitBound { pub args_no_self: Vec, } -#[derive(Clone, PartialEq, Eq, Debug)] +#[derive(Clone, PartialEq, Eq, Debug, Hash)] /// Represents an alias equality bound on e.g. a type or type parameter. /// Does not know anything about what it's binding. pub struct AliasEqBound { @@ -284,7 +284,7 @@ pub struct AssocTyValue { pub default: bool, } -#[derive(Clone, PartialEq, Eq, Debug)] +#[derive(Clone, PartialEq, Eq, Debug, Hash)] pub enum Ty { Id { name: Identifier, @@ -331,7 +331,7 @@ pub enum Ty { Never, } -#[derive(Copy, Clone, PartialEq, Eq, Debug)] +#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)] pub enum IntTy { Isize, I8, @@ -341,7 +341,7 @@ pub enum IntTy { I128, } -#[derive(Copy, Clone, PartialEq, Eq, Debug)] +#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)] pub enum UintTy { Usize, U8, @@ -351,13 +351,13 @@ pub enum UintTy { U128, } -#[derive(Copy, Clone, PartialEq, Eq, Debug)] +#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)] pub enum FloatTy { F32, F64, } -#[derive(Copy, Clone, PartialEq, Eq, Debug)] +#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)] pub enum ScalarType { Bool, Char, @@ -366,13 +366,13 @@ pub enum ScalarType { Float(FloatTy), } -#[derive(Copy, Clone, PartialEq, Eq, Debug)] +#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)] pub enum Mutability { Mut, Not, } -#[derive(Copy, Clone, PartialEq, Eq, Debug)] +#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)] pub enum Safety { Safe, Unsafe, @@ -384,7 +384,7 @@ impl Default for Safety { } } -#[derive(Clone, PartialEq, Eq, Debug)] +#[derive(Clone, PartialEq, Eq, Debug, Hash)] pub enum Lifetime { Id { name: Identifier }, Static, @@ -392,14 +392,14 @@ pub enum Lifetime { Empty, } -#[derive(Clone, PartialEq, Eq, Debug)] +#[derive(Clone, PartialEq, Eq, Debug, Hash)] pub struct ProjectionTy { pub trait_ref: TraitRef, pub name: Identifier, pub args: Vec, } -#[derive(Clone, PartialEq, Eq, Debug)] +#[derive(Clone, PartialEq, Eq, Debug, Hash)] pub struct TraitRef { pub trait_name: Identifier, pub args: Vec, @@ -424,7 +424,7 @@ impl Polarity { } } -#[derive(Clone, PartialEq, Eq, Debug)] +#[derive(Clone, PartialEq, Eq, Debug, Hash)] pub struct Identifier { pub str: Atom, pub span: Span, diff --git a/chalk-solve/src/clauses/program_clauses.rs b/chalk-solve/src/clauses/program_clauses.rs index 19811ff8b30..0a31b9b2a25 100644 --- a/chalk-solve/src/clauses/program_clauses.rs +++ b/chalk-solve/src/clauses/program_clauses.rs @@ -880,7 +880,7 @@ impl ToProgramClauses for AssociatedTyDatum { // FromEnv(::Assoc: Bounds) :- FromEnv(Self: Foo), WC // } for quantified_bound in bounds { - builder.push_binders(quantified_bound, |builder, bound| { + builder.push_binders(quantified_bound.0, |builder, bound| { for wc in bound.into_where_clauses(interner, projection_ty.clone()) { builder.push_clause( wc.into_from_env_goal(interner), diff --git a/chalk-solve/src/display/bounds.rs b/chalk-solve/src/display/bounds.rs index 19dcb31871e..af23e15f9d4 100644 --- a/chalk-solve/src/display/bounds.rs +++ b/chalk-solve/src/display/bounds.rs @@ -62,14 +62,14 @@ impl RenderAsRust for QuantifiedInlineBound { fn fmt(&self, s: &InternalWriterState<'_, I>, f: &'_ mut Formatter<'_>) -> Result { let interner = s.db().interner(); let s = &s.add_debrujin_index(None); - if !self.binders.is_empty(interner) { + if !self.0.binders.is_empty(interner) { write!( f, "forall<{}> ", - s.binder_var_display(&self.binders).format(", ") + s.binder_var_display(&self.0.binders).format(", ") )?; } - self.skip_binders().fmt(s, f) + self.0.skip_binders().fmt(s, f) } } diff --git a/chalk-solve/src/rust_ir.rs b/chalk-solve/src/rust_ir.rs index 612d05203e9..a9507555ea0 100644 --- a/chalk-solve/src/rust_ir.rs +++ b/chalk-solve/src/rust_ir.rs @@ -340,8 +340,8 @@ pub enum InlineBound { AliasEqBound(AliasEqBound), } -#[allow(type_alias_bounds)] -pub type QuantifiedInlineBound = Binders>; +#[derive(Clone, Debug, PartialEq, Eq, Hash, Fold, Visit, HasInterner)] +pub struct QuantifiedInlineBound(pub Binders>); pub trait IntoWhereClauses { type Output; @@ -370,7 +370,7 @@ impl IntoWhereClauses for QuantifiedInlineBound { fn into_where_clauses(&self, interner: I, self_ty: Ty) -> Vec> { let self_ty = self_ty.shifted_in(interner); - self.map_ref(|b| b.into_where_clauses(interner, self_ty)) + self.0.map_ref(|b| b.into_where_clauses(interner, self_ty)) .into_iter() .collect() } From f9ca7ae2120d5651307d3a139f8fe83db610a05c Mon Sep 17 00:00:00 2001 From: pierwill Date: Wed, 15 Dec 2021 22:02:12 -0600 Subject: [PATCH 34/35] use indexset! macro in lowering code --- chalk-integration/src/lowering.rs | 35 ++++++++++++------------------- 1 file changed, 13 insertions(+), 22 deletions(-) diff --git a/chalk-integration/src/lowering.rs b/chalk-integration/src/lowering.rs index d4236a41fd8..8109fa8f133 100644 --- a/chalk-integration/src/lowering.rs +++ b/chalk-integration/src/lowering.rs @@ -8,8 +8,7 @@ use chalk_ir::{ }; use chalk_parse::ast::*; use chalk_solve::rust_ir::{self, IntoWhereClauses}; -use indexmap::IndexMap; -use indexmap::IndexSet; +use indexmap::{indexset, IndexMap, IndexSet}; use program_lowerer::ProgramLowerer; use string_cache::DefaultAtom as Atom; use tracing::debug; @@ -159,40 +158,32 @@ impl LowerWithEnv for WhereClause { fn lower(&self, env: &Env) -> LowerResult { Ok(match self { WhereClause::Implemented { trait_ref } => { - let mut set = IndexSet::new(); - set.insert(chalk_ir::WhereClause::Implemented(trait_ref.lower(env)?)); - set + indexset! { chalk_ir::WhereClause::Implemented(trait_ref.lower(env)?)} } WhereClause::ProjectionEq { projection, ty } => { - let mut set = IndexSet::new(); - set.insert(chalk_ir::WhereClause::AliasEq(chalk_ir::AliasEq { - alias: chalk_ir::AliasTy::Projection(projection.lower(env)?), - ty: ty.lower(env)?, - })); - set.insert(chalk_ir::WhereClause::Implemented( - projection.trait_ref.lower(env)?, - )); - set + indexset! { + chalk_ir::WhereClause::AliasEq(chalk_ir::AliasEq { + alias: chalk_ir::AliasTy::Projection(projection.lower(env)?), + ty: ty.lower(env)?}), + chalk_ir::WhereClause::Implemented( + projection.trait_ref.lower(env)?), + } } WhereClause::LifetimeOutlives { a, b } => { - let mut set = IndexSet::new(); - set.insert(chalk_ir::WhereClause::LifetimeOutlives( + indexset! {chalk_ir::WhereClause::LifetimeOutlives( chalk_ir::LifetimeOutlives { a: a.lower(env)?, b: b.lower(env)?, }, - )); - set + )} } WhereClause::TypeOutlives { ty, lifetime } => { - let mut set = IndexSet::new(); - set.insert(chalk_ir::WhereClause::TypeOutlives( + indexset! {chalk_ir::WhereClause::TypeOutlives( chalk_ir::TypeOutlives { ty: ty.lower(env)?, lifetime: lifetime.lower(env)?, }, - )); - set + )} } }) } From f7ac0512717cde2bf1970ffbb707b3f86af3d427 Mon Sep 17 00:00:00 2001 From: pierwill Date: Wed, 15 Dec 2021 22:02:24 -0600 Subject: [PATCH 35/35] bump back indexmap --- chalk-integration/Cargo.toml | 2 +- chalk-solve/Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/chalk-integration/Cargo.toml b/chalk-integration/Cargo.toml index ace25f12b55..8431b370e62 100644 --- a/chalk-integration/Cargo.toml +++ b/chalk-integration/Cargo.toml @@ -20,4 +20,4 @@ chalk-solve = { version = "0.76.0-dev.0", path = "../chalk-solve" } chalk-recursive = { version = "0.76.0-dev.0", path = "../chalk-recursive" } chalk-engine = { version = "0.76.0-dev.0", path = "../chalk-engine" } chalk-parse = { version = "0.76.0-dev.0", path = "../chalk-parse" } -indexmap = "1.5.1" +indexmap = "1.7.0" diff --git a/chalk-solve/Cargo.toml b/chalk-solve/Cargo.toml index bde17cef6cd..756e9010143 100644 --- a/chalk-solve/Cargo.toml +++ b/chalk-solve/Cargo.toml @@ -20,7 +20,7 @@ rustc-hash = { version = "1.1.0" } chalk-derive = { version = "0.76.0-dev.0", path = "../chalk-derive" } chalk-ir = { version = "0.76.0-dev.0", path = "../chalk-ir" } -indexmap = "1.5.1" +indexmap = "1.7.0" [dev-dependencies] chalk-integration = { path = "../chalk-integration" }