Skip to content

Commit

Permalink
fix: type formatting bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
mtshiba committed Dec 16, 2024
1 parent 135d431 commit 4971511
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 29 deletions.
30 changes: 18 additions & 12 deletions crates/erg_compiler/context/inquire.rs
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,10 @@ impl Context {
}
}
if acc_kind.is_local() {
if let Some(parent) = self.get_outer_scope().or_else(|| self.get_builtins()) {
if let Some(parent) = self
.get_outer_scope()
.or_else(|| self.get_builtins_not_self())
{
return parent.rec_get_var_info(ident, acc_kind, input, namespace);
}
}
Expand Down Expand Up @@ -743,7 +746,10 @@ impl Context {
}
}
if acc_kind.is_local() {
if let Some(parent) = self.get_outer_scope().or_else(|| self.get_builtins()) {
if let Some(parent) = self
.get_outer_scope()
.or_else(|| self.get_builtins_not_self())
{
return parent.rec_get_decl_info(ident, acc_kind, input, namespace);
}
}
Expand Down Expand Up @@ -3312,7 +3318,7 @@ impl Context {
.is_some_and(|ctx| &ctx.typ.qual_name() == "ProcMetaType")
{
if let Some(ctx) = self
.get_builtins()
.get_builtins_not_self()
.unwrap_or(self)
.rec_local_get_mono_type("QuantifiedProcMetaType")
{
Expand All @@ -3323,15 +3329,15 @@ impl Context {
.is_some_and(|ctx| &ctx.typ.qual_name() == "FuncMetaType")
{
if let Some(ctx) = self
.get_builtins()
.get_builtins_not_self()
.unwrap_or(self)
.rec_local_get_mono_type("QuantifiedFuncMetaType")
{
return Some(ctx);
}
}
if let Some(ctx) = self
.get_builtins()
.get_builtins_not_self()
.unwrap_or(self)
.rec_local_get_mono_type("QuantifiedFunc")
{
Expand All @@ -3342,15 +3348,15 @@ impl Context {
SubrKind::Func => {
if self.subtype_of(&subr.return_t, &Type) {
if let Some(ctx) = self
.get_builtins()
.get_builtins_not_self()
.unwrap_or(self)
.rec_local_get_mono_type("FuncMetaType")
{
return Some(ctx);
}
}
if let Some(ctx) = self
.get_builtins()
.get_builtins_not_self()
.unwrap_or(self)
.rec_local_get_mono_type("Func")
{
Expand All @@ -3360,15 +3366,15 @@ impl Context {
SubrKind::Proc => {
if self.subtype_of(&subr.return_t, &Type) {
if let Some(ctx) = self
.get_builtins()
.get_builtins_not_self()
.unwrap_or(self)
.rec_local_get_mono_type("ProcMetaType")
{
return Some(ctx);
}
}
if let Some(ctx) = self
.get_builtins()
.get_builtins_not_self()
.unwrap_or(self)
.rec_local_get_mono_type("Proc")
{
Expand All @@ -3385,21 +3391,21 @@ impl Context {
Type::Record(rec) => {
if rec.values().all(|t| self.subtype_of(t, &Type)) {
if let Some(ctx) = self
.get_builtins()
.get_builtins_not_self()
.unwrap_or(self)
.rec_local_get_mono_type("RecordMetaType")
{
return Some(ctx);
}
}
return self
.get_builtins()
.get_builtins_not_self()
.unwrap_or(self)
.rec_local_get_mono_type("Record");
}
Type::NamedTuple(_) => {
return self
.get_builtins()
.get_builtins_not_self()
.unwrap_or(self)
.rec_local_get_mono_type("GenericNamedTuple");
}
Expand Down
24 changes: 15 additions & 9 deletions crates/erg_compiler/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ impl ContextProvider for Context {
let mut vars = self.type_dir(self);
if let Some(outer) = self.get_outer_scope() {
vars.guaranteed_extend(outer.dir());
} else if let Some(builtins) = self.get_builtins() {
} else if let Some(builtins) = self.get_builtins_not_self() {
vars.guaranteed_extend(builtins.locals.iter());
}
vars
Expand All @@ -705,7 +705,7 @@ impl ContextProvider for Context {
// this is internally recursive
fn get_var_info(&self, name: &str) -> Option<(&VarName, &VarInfo)> {
self.get_var_kv(name).or_else(|| {
self.get_builtins()
self.get_builtins_not_self()
.and_then(|builtin| builtin.get_var_kv(name))
})
}
Expand Down Expand Up @@ -1209,7 +1209,8 @@ impl Context {
}

pub(crate) fn get_outer_scope_or_builtins(&self) -> Option<&Context> {
self.get_outer_scope().or_else(|| self.get_builtins())
self.get_outer_scope()
.or_else(|| self.get_builtins_not_self())
}

pub(crate) fn get_mut_outer(&mut self) -> Option<&mut Context> {
Expand Down Expand Up @@ -1258,18 +1259,23 @@ impl Context {

/// Returns None if self is `<builtins>`.
/// This avoids infinite loops.
pub(crate) fn get_builtins(&self) -> Option<&Context> {
// builtins中で定義した型等はmod_cacheがNoneになっている
pub(crate) fn get_builtins_not_self(&self) -> Option<&Context> {
if &self.path()[..] != "<builtins>" {
self.shared
.as_ref()
.map(|shared| shared.mod_cache.raw_ref_builtins_ctx().unwrap())
.map(|mod_ctx| &mod_ctx.context)
self.get_builtins()
} else {
None
}
}

/// Be careful with infinite loops. See also `get_builtins_not_self`.
pub(crate) fn get_builtins(&self) -> Option<&Context> {
// builtins中で定義した型等はmod_cacheがNoneになっている
self.shared
.as_ref()
.map(|shared| shared.mod_cache.raw_ref_builtins_ctx().unwrap())
.map(|mod_ctx| &mod_ctx.context)
}

pub(crate) fn get_module(&self) -> Option<&Context> {
self.get_outer()
.and_then(|outer| {
Expand Down
2 changes: 1 addition & 1 deletion crates/erg_compiler/context/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2810,7 +2810,7 @@ impl Context {
.rec_get_var_info(&ident.raw, crate::AccessKind::Name, &self.cfg.input, self)
.map_ok_or(false, |vi| vi.muty.is_const());
let is_builtin = self
.get_builtins()
.get_builtins_not_self()
.unwrap()
.get_var_kv(ident.inspect())
.is_some();
Expand Down
2 changes: 1 addition & 1 deletion crates/erg_compiler/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2191,7 +2191,7 @@ impl<A: ASTBuildable> GenericASTLowerer<A> {
} else if self
.module
.context
.get_builtins()
.get_builtins_not_self()
.and_then(|ctx| ctx.get_var_info(&name))
.is_some()
&& def.sig.vis().is_private()
Expand Down
27 changes: 24 additions & 3 deletions crates/erg_compiler/ty/const_subr.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::fmt;

use erg_common::consts::DEBUG_MODE;
use erg_common::dict::Dict;
#[allow(unused_imports)]
use erg_common::log;
Expand Down Expand Up @@ -124,7 +125,11 @@ impl std::hash::Hash for BuiltinConstSubr {

impl fmt::Display for BuiltinConstSubr {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "<built-in const subroutine '{}'>", self.name)
if DEBUG_MODE {
write!(f, "<built-in const subroutine '{}'>", self.name)
} else {
write!(f, "{}", self.name)
}
}
}

Expand Down Expand Up @@ -201,7 +206,11 @@ impl std::hash::Hash for GenConstSubr {

impl fmt::Display for GenConstSubr {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "<const subroutine '{}'>", self.name)
if DEBUG_MODE {
write!(f, "<const subroutine '{}'>", self.name)
} else {
write!(f, "{}", self.name)
}
}
}

Expand Down Expand Up @@ -238,7 +247,11 @@ impl fmt::Display for ConstSubr {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ConstSubr::User(subr) => {
write!(f, "<user-defined const subroutine '{}'>", subr.name)
if DEBUG_MODE {
write!(f, "<user-defined const subroutine '{}'>", subr.name)
} else {
write!(f, "{}", subr.name)
}
}
ConstSubr::Builtin(subr) => write!(f, "{subr}"),
ConstSubr::Gen(subr) => write!(f, "{subr}"),
Expand Down Expand Up @@ -299,4 +312,12 @@ impl ConstSubr {
ConstSubr::Gen(gen) => gen.as_type.clone(),
}
}

pub fn name(&self) -> Str {
match self {
Self::Builtin(subr) => subr.name.clone(),
Self::User(subr) => subr.name.clone(),
Self::Gen(subr) => subr.name.clone(),
}
}
}
10 changes: 9 additions & 1 deletion crates/erg_compiler/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1593,7 +1593,12 @@ impl LimitedDisplay for Type {
}
match self {
Self::FreeVar(fv) => fv.limited_fmt(f, limit),
Self::Mono(name) => write!(f, "{name}"),
Self::Mono(name) => {
if limit.is_negative() && self.namespace().is_empty() {
write!(f, "global::")?;
}
write!(f, "{name}")
}
Self::Ref(t) => {
write!(f, "{}(", self.qual_name())?;
t.limited_fmt(f, limit - 1)?;
Expand Down Expand Up @@ -1696,6 +1701,9 @@ impl LimitedDisplay for Type {
ty.limited_fmt(f, limit - 1)
}
Self::Poly { name, params } => {
if limit.is_negative() && self.namespace().is_empty() {
write!(f, "global::")?;
}
write!(f, "{name}(")?;
if !DEBUG_MODE && self.is_module() {
// Module("path/to/module.er") -> Module("module.er")
Expand Down
13 changes: 11 additions & 2 deletions crates/erg_compiler/ty/typaram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,9 @@ impl LimitedDisplay for TyParam {
rhs.limited_fmt(f, limit - 1)
}
Self::App { name, args } => {
if limit.is_negative() && self.namespace().is_empty() {
write!(f, "global::")?;
}
write!(f, "{name}")?;
write!(f, "(")?;
for (i, arg) in args.iter().enumerate() {
Expand All @@ -414,7 +417,12 @@ impl LimitedDisplay for TyParam {
write!(f, "_: ")?;
t.limited_fmt(f, limit - 1)
}
Self::Mono(name) => write!(f, "{name}"),
Self::Mono(name) => {
if limit.is_negative() && self.namespace().is_empty() {
write!(f, "global::")?;
}
write!(f, "{name}")
}
Self::Proj { obj, attr } => {
obj.limited_fmt(f, limit - 1)?;
write!(f, ".")?;
Expand Down Expand Up @@ -1071,7 +1079,8 @@ impl TyParam {
Self::FreeVar(fv) if fv.is_linked() => fv.crack().qual_name(),
Self::FreeVar(fv) if fv.is_generalized() => fv.unbound_name(),
Self::Mono(name) => Some(name.clone()),
Self::Value(ValueObj::Type(t)) => Some(t.typ().qual_name()),
Self::Value(val) => val.qual_name(),
Self::App { name, .. } => Some(name.clone()),
_ => None,
}
}
Expand Down
9 changes: 9 additions & 0 deletions crates/erg_compiler/ty/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2253,6 +2253,15 @@ impl ValueObj {
mono_value_pattern!() => Set::new(),
}
}

pub fn qual_name(&self) -> Option<Str> {
match self {
Self::Type(t) => Some(t.typ().qual_name()),
Self::Subr(subr) => Some(subr.name()),
Self::DataClass { name, .. } => Some(name.clone()),
_ => None,
}
}
}

pub mod value_set {
Expand Down

0 comments on commit 4971511

Please sign in to comment.