Skip to content

Commit cf05218

Browse files
committed
Don't generate access for equality check (#399)
* Don't generate access for equality check Also inherit constraints when entering class. Useful if you have defined variables you use within the class outside the class. * Add test for equality of different types
1 parent e366681 commit cf05218

11 files changed

Lines changed: 76 additions & 27 deletions

File tree

src/check/constrain/constraint/builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ impl ConstrBuilder {
2929
pub fn is_top_level(&self) -> bool { self.level == 0 }
3030

3131
pub fn new_set_in_class(&mut self, inherit_class: bool, class: &StringName) {
32-
self.new_set(false);
32+
self.new_set(true);
3333
if self.level > 0 && inherit_class {
3434
let mut previous = self.constraints[self.level - 1].0.clone();
3535
self.constraints[self.level].0.append(&mut previous);

src/check/constrain/generate/operation.rs

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ use crate::check::constrain::generate::collection::{constr_col, gen_collection_l
99
use crate::check::constrain::generate::env::Environment;
1010
use crate::check::context::{clss, Context, LookupClass};
1111
use crate::check::context::clss::{FLOAT, INT, STRING};
12-
use crate::check::context::function::{
13-
ADD, DIV, EQ, FDIV, GE, GEQ, LE, LEQ, MOD, MUL, POW, SQRT, SUB,
14-
};
12+
use crate::check::context::function::{ADD, DIV, EQ, FDIV, GE, GEQ, LE, LEQ, MOD, MUL, NEQ, POW, SQRT, SUB};
1513
use crate::check::name::{Any, Name};
1614
use crate::check::name::string_name::StringName;
1715
use crate::check::name::true_name::TrueName;
@@ -92,9 +90,8 @@ pub fn gen_op(
9290
Node::Ge { left, right } => impl_bool_op(GE, ast, left, right, env, ctx, constr),
9391
Node::Leq { left, right } => impl_bool_op(LEQ, ast, left, right, env, ctx, constr),
9492
Node::Geq { left, right } => impl_bool_op(GEQ, ast, left, right, env, ctx, constr),
95-
Node::Neq { left, right } | Node::Eq { left, right } => {
96-
impl_bool_op(EQ, ast, left, right, env, ctx, constr)
97-
}
93+
Node::Neq { left, right } => impl_bool_op(EQ, ast, left, right, env, ctx, constr),
94+
Node::Eq { left, right } => impl_bool_op(EQ, ast, left, right, env, ctx, constr),
9895

9996
Node::AddU { expr } | Node::SubU { expr } => generate(expr, env, ctx, constr),
10097
Node::Sqrt { expr } => {
@@ -307,26 +304,29 @@ fn impl_bool_op(
307304
ctx: &Context,
308305
constr: &mut ConstrBuilder,
309306
) -> Constrained {
310-
constr.add(
311-
"bool operation",
312-
&Expected::try_from((ast, &env.var_mappings))?,
313-
&Expected::new(
314-
left.pos,
315-
&Access {
316-
entity: Box::new(Expected::try_from((left, &env.var_mappings))?),
317-
name: Box::new(Expected::new(
318-
left.pos,
319-
&Function {
320-
name: StringName::from(fun),
321-
args: vec![
322-
Expected::try_from((left, &env.var_mappings))?,
323-
Expected::try_from((right, &env.var_mappings))?,
324-
],
325-
},
326-
)),
327-
},
328-
),
329-
);
307+
if fun != EQ && fun != NEQ {
308+
constr.add(
309+
"bool operation",
310+
&Expected::try_from((ast, &env.var_mappings))?,
311+
&Expected::new(
312+
left.pos,
313+
&Access {
314+
entity: Box::new(Expected::try_from((left, &env.var_mappings))?),
315+
name: Box::new(Expected::new(
316+
left.pos,
317+
&Function {
318+
name: StringName::from(fun),
319+
args: vec![
320+
Expected::try_from((left, &env.var_mappings))?,
321+
Expected::try_from((right, &env.var_mappings))?,
322+
],
323+
},
324+
)),
325+
},
326+
),
327+
);
328+
}
329+
330330
let ty = Type { name: Name::from(clss::BOOL) };
331331
constr.add(
332332
"bool operation",
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
def message := "may be mutable, for now"
2+
3+
class MyClass
4+
def f(self) =>
5+
print(message)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
message: str = "may be mutable, for now"
2+
3+
class MyClass:
4+
def f(self):
5+
print(message)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
def my_string := "asdf"
2+
3+
print(my_string)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
my_string: str = "asdf"
2+
3+
print(my_string)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class MyClass
2+
class MyOtherClass
3+
4+
def a := MyClass()
5+
def b := MyOtherClass()
6+
7+
a = b
8+
a != b
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class MyClass:
2+
pass
3+
class MyOtherClass:
4+
pass
5+
6+
a: MyClass = MyClass()
7+
b: MyOtherClass = MyOtherClass()
8+
9+
a == b
10+
a != b

tests/system/valid/class.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,8 @@ fn tuple_as_class() -> OutTestRet {
8484
fn unassigned_tuple_second_nullable() -> OutTestRet {
8585
test_directory(true, &["class"], &["class", "target"], "unassigned_tuple_second_nullable")
8686
}
87+
88+
#[test]
89+
fn var_from_outside_class() -> OutTestRet {
90+
test_directory(true, &["class"], &["class", "target"], "var_from_outside_class")
91+
}

tests/system/valid/function.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ fn match_function() -> OutTestRet {
2525
test_directory(true, &["function"], &["function", "target"], "match_function")
2626
}
2727

28+
#[test]
29+
fn print_string() -> OutTestRet {
30+
test_directory(true, &["function"], &["function", "target"], "print_string")
31+
}
32+
2833
#[test]
2934
fn return_last_expression() -> OutTestRet {
3035
test_directory(true, &["function"], &["function", "target"], "return_last_expression")

0 commit comments

Comments
 (0)