Merge branch 'main' into fix-els

This commit is contained in:
Shunsuke Shibayama 2023-08-22 11:47:22 +09:00
commit b5787cb697
4 changed files with 31 additions and 12 deletions

View file

@ -2277,7 +2277,13 @@ impl Context {
guard: GuardType,
overwritten: &mut Vec<(VarName, VarInfo)>,
) -> TyCheckResult<()> {
if let Variable::Var(name, _) = &guard.var {
if let Variable::Var {
namespace, name, ..
} = &guard.var
{
if !self.name.starts_with(&namespace[..]) {
return Ok(());
}
let vi = if let Some((name, vi)) = self.locals.remove_entry(name) {
overwritten.push((name, vi.clone()));
vi

View file

@ -50,11 +50,15 @@ use crate::{feature_error, unreachable_error};
use VisibilityModifier::*;
pub fn acc_to_variable(acc: &ast::Accessor) -> Option<Variable> {
pub fn acc_to_variable(namespace: Str, acc: &ast::Accessor) -> Option<Variable> {
match acc {
ast::Accessor::Ident(ident) => Some(Variable::Var(ident.inspect().clone(), ident.loc())),
ast::Accessor::Ident(ident) => Some(Variable::Var {
namespace,
name: ident.inspect().clone(),
loc: ident.loc(),
}),
ast::Accessor::Attr(attr) => Some(Variable::attr(
expr_to_variable(&attr.obj)?,
expr_to_variable(namespace, &attr.obj)?,
attr.ident.inspect().clone(),
attr.loc(),
)),
@ -62,9 +66,9 @@ pub fn acc_to_variable(acc: &ast::Accessor) -> Option<Variable> {
}
}
pub fn expr_to_variable(expr: &ast::Expr) -> Option<Variable> {
pub fn expr_to_variable(namespace: Str, expr: &ast::Expr) -> Option<Variable> {
match expr {
ast::Expr::Accessor(acc) => acc_to_variable(acc),
ast::Expr::Accessor(acc) => acc_to_variable(namespace, acc),
_ => None,
}
}
@ -743,9 +747,9 @@ impl ASTLowerer {
fn get_guard_type(&self, op: &Token, lhs: &ast::Expr, rhs: &ast::Expr) -> Option<Type> {
let var = if op.kind == TokenKind::ContainsOp {
expr_to_variable(rhs)?
expr_to_variable(self.module.context.name.clone(), rhs)?
} else {
expr_to_variable(lhs)?
expr_to_variable(self.module.context.name.clone(), lhs)?
};
match op.kind {
// l in T -> T contains l

View file

@ -756,7 +756,11 @@ pub enum Variable {
name: Str,
loc: Location,
},
Var(Str, Location),
Var {
namespace: Str,
name: Str,
loc: Location,
},
Attr {
receiver: Box<Variable>,
attr: Str,
@ -768,7 +772,7 @@ impl fmt::Display for Variable {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::Param { nth, name, .. } => write!(f, "{name}#{nth}"),
Self::Var(name, _) => write!(f, "{name}"),
Self::Var { name, .. } => write!(f, "{name}"),
Self::Attr { receiver, attr, .. } => write!(f, "{receiver}.{attr}"),
}
}
@ -778,7 +782,7 @@ impl Locational for Variable {
fn loc(&self) -> Location {
match self {
Self::Param { loc, .. } => *loc,
Self::Var(_, loc) => *loc,
Self::Var { loc, .. } => *loc,
Self::Attr { loc, .. } => *loc,
}
}