fix: quantified subroutine subtyping bugs

This commit is contained in:
Shunsuke Shibayama 2023-02-22 02:40:51 +09:00
parent 4dcca2b06d
commit aa2cea60dd
28 changed files with 638 additions and 222 deletions

View file

@ -10,7 +10,7 @@ use erg_common::levenshtein::get_similar_name;
use erg_common::shared::Shared;
use erg_common::Str;
use crate::context::{Context, ModuleContext};
use crate::context::ModuleContext;
use crate::hir::HIR;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@ -157,10 +157,8 @@ impl fmt::Display for SharedModuleCache {
}
impl SharedModuleCache {
pub fn new(cfg: ErgConfig) -> Self {
let self_ = Self(Shared::new(ModuleCache::new()));
Context::init_builtins(cfg, &self_);
self_
pub fn new() -> Self {
Self(Shared::new(ModuleCache::new()))
}
pub fn get<Q: Eq + Hash + ?Sized>(&self, path: &Q) -> Option<&ModuleEntry>

View file

@ -1,7 +1,10 @@
use erg_common::config::ErgConfig;
use crate::context::Context;
use super::cache::SharedModuleCache;
use super::graph::SharedModuleGraph;
use super::impls::SharedTraitImpls;
use super::index::SharedModuleIndex;
#[derive(Debug, Clone, Default)]
@ -10,16 +13,25 @@ pub struct SharedCompilerResource {
pub py_mod_cache: SharedModuleCache,
pub index: SharedModuleIndex,
pub graph: SharedModuleGraph,
/// K: name of a trait, V: (type, monomorphised trait that the type implements)
/// K: トレイトの名前, V: (型, その型が実装する単相化トレイト)
/// e.g. { "Named": [(Type, Named), (Func, Named), ...], "Add": [(Nat, Add(Nat)), (Int, Add(Int)), ...], ... }
pub trait_impls: SharedTraitImpls,
}
impl SharedCompilerResource {
/// Initialize the shared compiler resource.
/// This API is normally called only once throughout the compilation phase.
pub fn new(cfg: ErgConfig) -> Self {
Self {
mod_cache: SharedModuleCache::new(cfg.copy()),
py_mod_cache: SharedModuleCache::new(cfg),
let self_ = Self {
mod_cache: SharedModuleCache::new(),
py_mod_cache: SharedModuleCache::new(),
index: SharedModuleIndex::new(),
graph: SharedModuleGraph::new(),
}
trait_impls: SharedTraitImpls::new(),
};
Context::init_builtins(cfg, self_.clone());
self_
}
pub fn clear_all(&self) {
@ -27,5 +39,6 @@ impl SharedCompilerResource {
self.py_mod_cache.initialize();
self.index.initialize();
self.graph.initialize();
self.trait_impls.initialize();
}
}

View file

@ -0,0 +1,114 @@
use std::borrow::Borrow;
use std::fmt;
use std::hash::Hash;
use erg_common::dict::Dict;
use erg_common::set::Set;
use erg_common::shared::Shared;
use erg_common::Str;
use crate::context::TraitImpl;
/// Caches checked modules.
/// In addition to being queried here when re-imported, it is also used when linking
/// (Erg links all scripts defined in erg and outputs them to a single pyc file).
#[derive(Debug, Default)]
pub struct TraitImpls {
cache: Dict<Str, Set<TraitImpl>>,
}
impl fmt::Display for TraitImpls {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "TraitImpls {{")?;
for (name, impls) in self.cache.iter() {
writeln!(f, "{name}: {impls}, ")?;
}
write!(f, "}}")
}
}
impl TraitImpls {
pub fn new() -> Self {
Self { cache: Dict::new() }
}
pub fn get<P: Eq + Hash + ?Sized>(&self, path: &P) -> Option<&Set<TraitImpl>>
where
Str: Borrow<P>,
{
self.cache.get(path)
}
pub fn get_mut<Q: Eq + Hash + ?Sized>(&mut self, path: &Q) -> Option<&mut Set<TraitImpl>>
where
Str: Borrow<Q>,
{
self.cache.get_mut(path)
}
pub fn register(&mut self, name: Str, impls: Set<TraitImpl>) {
self.cache.insert(name, impls);
}
pub fn remove<Q: Eq + Hash + ?Sized>(&mut self, path: &Q) -> Option<Set<TraitImpl>>
where
Str: Borrow<Q>,
{
self.cache.remove(path)
}
pub fn initialize(&mut self) {
self.cache.clear();
}
}
#[derive(Debug, Clone, Default)]
pub struct SharedTraitImpls(Shared<TraitImpls>);
impl fmt::Display for SharedTraitImpls {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Shared{}", self.0)
}
}
impl SharedTraitImpls {
pub fn new() -> Self {
Self(Shared::new(TraitImpls::new()))
}
pub fn get<Q: Eq + Hash + ?Sized>(&self, path: &Q) -> Option<&Set<TraitImpl>>
where
Str: Borrow<Q>,
{
let ref_ = unsafe { self.0.as_ptr().as_ref().unwrap() };
ref_.get(path)
}
pub fn get_mut<Q: Eq + Hash + ?Sized>(&self, path: &Q) -> Option<&mut Set<TraitImpl>>
where
Str: Borrow<Q>,
{
let ref_ = unsafe { self.0.as_ptr().as_mut().unwrap() };
ref_.get_mut(path)
}
pub fn register(&self, name: Str, impls: Set<TraitImpl>) {
self.0.borrow_mut().register(name, impls);
}
pub fn remove<Q: Eq + Hash + ?Sized>(&self, path: &Q) -> Option<Set<TraitImpl>>
where
Str: Borrow<Q>,
{
self.0.borrow_mut().remove(path)
}
pub fn keys(&self) -> impl Iterator<Item = Str> {
let ref_ = unsafe { self.0.as_ptr().as_ref().unwrap() };
ref_.cache.keys().cloned()
}
pub fn initialize(&self) {
self.0.borrow_mut().initialize();
}
}

View file

@ -1,9 +1,11 @@
pub mod cache;
pub mod global;
pub mod graph;
pub mod impls;
pub mod index;
pub use cache::*;
pub use global::*;
pub use graph::*;
pub use impls::*;
pub use index::*;