Add python shadowing mode

This commit is contained in:
Shunsuke Shibayama 2022-12-20 23:21:36 +09:00
parent a6da05147c
commit 351ac57188
2 changed files with 11 additions and 5 deletions

View file

@ -1093,22 +1093,24 @@ impl ASTLowerer {
Ok(block) => { Ok(block) => {
let found_body_t = block.ref_t(); let found_body_t = block.ref_t();
let outer = self.ctx.outer.as_ref().unwrap(); let outer = self.ctx.outer.as_ref().unwrap();
let opt_expect_body_t = sig let opt_expect_body_vi = sig
.inspect() .inspect()
.and_then(|name| outer.get_current_scope_var(name).map(|vi| vi.t.clone())); .and_then(|name| outer.get_current_scope_var(name));
let ident = match &sig.pat { let ident = match &sig.pat {
ast::VarPattern::Ident(ident) => ident, ast::VarPattern::Ident(ident) => ident,
ast::VarPattern::Discard(_) => ast::Identifier::UBAR, ast::VarPattern::Discard(_) => ast::Identifier::UBAR,
_ => unreachable!(), _ => unreachable!(),
}; };
if let Some(expect_body_t) = opt_expect_body_t { if let Some(expect_body_vi) = opt_expect_body_vi {
let python_shadowing =
self.cfg.python_compatible_mode && expect_body_vi.kind.is_defined();
// TODO: expect_body_t is smaller for constants // TODO: expect_body_t is smaller for constants
// TODO: 定数の場合、expect_body_tのほうが小さくなってしまう // TODO: 定数の場合、expect_body_tのほうが小さくなってしまう
if !sig.is_const() { if !sig.is_const() && !python_shadowing {
if let Err(e) = self.var_result_t_check( if let Err(e) = self.var_result_t_check(
sig.loc(), sig.loc(),
ident.inspect(), ident.inspect(),
&expect_body_t, &expect_body_vi.t,
found_body_t, found_body_t,
) { ) {
self.errs.push(e); self.errs.push(e);

View file

@ -63,6 +63,10 @@ impl VarKind {
pub const fn is_parameter(&self) -> bool { pub const fn is_parameter(&self) -> bool {
matches!(self, Self::Parameter { .. }) matches!(self, Self::Parameter { .. })
} }
pub const fn is_defined(&self) -> bool {
matches!(self, Self::Defined(_))
}
} }
/// Has information about the type, variability, visibility, and where the variable was defined (or declared, generated) /// Has information about the type, variability, visibility, and where the variable was defined (or declared, generated)