From 33929bda249aa4d61862f16438feaae95ea3061c Mon Sep 17 00:00:00 2001 From: David Mak Date: Wed, 21 Aug 2024 13:52:24 +0800 Subject: [PATCH] [core] typecheck/typedef: Add support for tuple methods --- nac3core/src/typecheck/typedef/mod.rs | 31 +++++++++++++++++---------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/nac3core/src/typecheck/typedef/mod.rs b/nac3core/src/typecheck/typedef/mod.rs index 99a282f2..93ccd9fb 100644 --- a/nac3core/src/typecheck/typedef/mod.rs +++ b/nac3core/src/typecheck/typedef/mod.rs @@ -1,3 +1,11 @@ +use super::magic_methods::{Binop, HasOpInfo}; +use super::type_error::{TypeError, TypeErrorKind}; +use super::unification_table::{UnificationKey, UnificationTable}; +use crate::symbol_resolver::SymbolValue; +use crate::toplevel::helper::PrimDef; +use crate::toplevel::{DefinitionId, TopLevelContext, TopLevelDef}; +use crate::typecheck::magic_methods::OpInfo; +use crate::typecheck::type_inferencer::PrimitiveStore; use indexmap::IndexMap; use itertools::{repeat_n, Itertools}; use nac3parser::ast::{Cmpop, Location, StrRef, Unaryop}; @@ -9,15 +17,6 @@ use std::rc::Rc; use std::sync::{Arc, Mutex}; use std::{borrow::Cow, collections::HashSet}; -use super::magic_methods::Binop; -use super::type_error::{TypeError, TypeErrorKind}; -use super::unification_table::{UnificationKey, UnificationTable}; -use crate::symbol_resolver::SymbolValue; -use crate::toplevel::helper::PrimDef; -use crate::toplevel::{DefinitionId, TopLevelContext, TopLevelDef}; -use crate::typecheck::magic_methods::OpInfo; -use crate::typecheck::type_inferencer::PrimitiveStore; - #[cfg(test)] mod test; @@ -1008,8 +1007,18 @@ impl Unifier { self.unify_impl(v.ty, ty[ind as usize], false) .map_err(|e| e.at(v.loc))?; } - RecordKey::Str(_) => { - return Err(TypeError::new(TypeErrorKind::NoSuchField(*k, b), v.loc)) + RecordKey::Str(s) => { + let tuple_fns = [ + Cmpop::Eq.op_info().method_name, + Cmpop::NotEq.op_info().method_name, + ]; + + if !tuple_fns.into_iter().any(|op| s.to_string() == op) { + return Err(TypeError::new( + TypeErrorKind::NoSuchField(*k, b), + v.loc, + )); + } } } }