Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type Error in type_check_Ctup.py #32

Closed
Trac91iu opened this issue Nov 14, 2024 · 3 comments
Closed

Type Error in type_check_Ctup.py #32

Trac91iu opened this issue Nov 14, 2024 · 3 comments
Assignees

Comments

@Trac91iu
Copy link

I’ve been testing the tuple but keep encountering the following error:
Traceback (most recent call last):
File "/home/autograder/working_dir/run-tuple-test.py", line 22, in
run_one_test(os.getcwd() + '/tests/' + lang + '/' + test + '.py', lang,
File "/home/autograder/working_dir/utils.py", line 1606, in run_one_test
return compile_and_test(compiler, compiler_name, type_check_dict,
File "/home/autograder/working_dir/utils.py", line 1418, in compile_and_test
type_check_dictpassname
File "/home/autograder/working_dir/type_check_Cif.py", line 115, in type_check
self.type_check_stmts(ss[:-1], env)
File "/home/autograder/working_dir/type_check_Cif.py", line 78, in type_check_stmts
self.type_check_stmt(s, env)
File "/home/autograder/working_dir/type_check_Ctup.py", line 76, in type_check_stmt
return super().type_check_stmt(s, env)
File "/home/autograder/working_dir/type_check_Cif.py", line 83, in type_check_stmt
t = self.type_check_exp(value, env)
File "/home/autograder/working_dir/type_check_Ctup.py", line 58, in type_check_exp
return super().type_check_exp(e, env)
File "/home/autograder/working_dir/type_check_Cif.py", line 74, in type_check_exp
raise Exception('error in type_check_exp, unexpected ' + repr(e))
Exception: error in type_check_exp, unexpected GlobalValue(name='free_ptr')

After investigating, I looked into type_check_Ctup.py and noticed that the type_check_exp function doesn't seem to handle the GlobalValue case. When I assign a GlobalValue to a variable, Assign takes exp as an input, and GlobalValue appears to be a type of exp as well.

If the remove complex operand give statement "tmp.4 = (free_ptr + 32)", it works fine. But according to the slides and the python-book, they both suggest using "tmp.3 = free_ptr". Given the grammar (Figure 7.12), "tmp.3 = free_ptr" should also pass the type checker. Therefore, would it be better to add a case in the type_check_exp function in type_check_Ctup.py to handle the GlobalValue? For example: case GlobalValue(name): return self.type_check_atm(e, env)).

@DarshalShetty
Copy link
Collaborator

DarshalShetty commented Nov 14, 2024

@jsiek this issue is for the Python support code, even though it's been posted on the Racket support code repo.

Here is the complete error trace for @Trac91iu's compiler when run on the get_add test in the tuple test suite:

# source program: get_add

    t = (38, 2,)
    print(((t[0] + t[1]) + len(t)))


Module([  Assign([Name('t')], Tuple([Constant(38), Constant(2)])),   Expr(Call(Name('print'), [BinOp(BinOp(Subscript(Name('t'), Constant(0), <ast.Load object at 0x7f0f91e7a3e0>), Add(), Subscript(Name('t'), Constant(1), <ast.Load object at 0x7f0f91e7a3e0>)), Add(), Call(Name('len'), [Name('t')]))]))])


# type checking source program


# shrink

    t = (38, 2,)
    print(((t[0] + t[1]) + len(t)))


compiler tuples success on pass shrink on test
/home/celebrimbor/Code/compilers/python-compiler/tests/tuple/get_add


# expose_allocation

    t = {
      init.0 = 38
      init.1 = 2
      if (free_ptr + 32) < fromspace_end:
      else:
        collect(32)
      alloc.2 = allocate(32,tuple[int,int])
      alloc.2[0] = init.0
      alloc.2[1] = init.1
      produce alloc.2}
    print(((t[0] + t[1]) + len(t)))

type checking after expose_allocation

type checking passed
1c1
< 72
---
> 42
compiler tuples failed pass expose_allocation on test:
/home/celebrimbor/Code/compilers/python-compiler/tests/tuple/get_add


# remove_complex_operands

    t = {
      init.0 = 38
      init.1 = 2
      tmp.3 = free_ptr
      tmp.4 = (tmp.3 + 32)
      tmp.5 = fromspace_end
      if tmp.4 < tmp.5:
      else:
        collect(32)
      alloc.2 = allocate(32,tuple[int,int])
      alloc.2[0] = init.0
      alloc.2[1] = init.1
      produce alloc.2}
    tmp.6 = t[0]
    tmp.7 = t[1]
    tmp.8 = (tmp.6 + tmp.7)
    tmp.9 = len(t)
    tmp.10 = (tmp.8 + tmp.9)
    print(tmp.10)

Module([  Assign([Name('t')], Begin(body=[  Assign([Name('init.0')], Constant(38)),   Assign([Name('init.1')], Constant(2)),   Assign([Name('tmp.3')], GlobalValue(name='free_ptr')),   Assign([Name('tmp.4')], BinOp(Name('tmp.3'), Add(), Constant(32))),   Assign([Name('tmp.5')], GlobalValue(name='fromspace_end')), If(Compare(Name('tmp.4'), [Lt()], [Name('tmp.5')]), [], [Collect(size=32)]),   Assign([Name('alloc.2')], Allocate(length=32, ty=TupleType(types=[IntType(), IntType()]))),   Assign([Subscript(Name('alloc.2'), Constant(0), <ast.Store object at 0x7f0f9132b460>)], Name('init.0')),   Assign([Subscript(Name('alloc.2'), Constant(1), <ast.Store object at 0x7f0f9132ba00>)], Name('init.1'))], result=Name('alloc.2'))),   Assign([Name('tmp.6')], Subscript(Name('t'), Constant(0), <ast.Load object at 0x7f0f91e7a3e0>)),   Assign([Name('tmp.7')], Subscript(Name('t'), Constant(1), <ast.Load object at 0x7f0f91e7a3e0>)),   Assign([Name('tmp.8')], BinOp(Name('tmp.6'), Add(), Name('tmp.7'))),   Assign([Name('tmp.9')], Call(Name('len'), [Name('t')])),   Assign([Name('tmp.10')], BinOp(Name('tmp.8'), Add(), Name('tmp.9'))),   Expr(Call(Name('print'), [Name('tmp.10')]))])
type checking after remove_complex_operands

type checking passed
1c1
< 72
---
> 42
compiler tuples failed pass remove_complex_operands on test:
/home/celebrimbor/Code/compilers/python-compiler/tests/tuple/get_add


# explicate_control

block.11:
    alloc.2 = allocate(32,tuple[int,int])
    alloc.2:tuple[int,int][0] = init.0
    alloc.2:tuple[int,int][1] = init.1
    t = alloc.2
    tmp.6 = t[0]
    tmp.7 = t[1]
    tmp.8 = (tmp.6 + tmp.7)
    tmp.9 = len(t)
    tmp.10 = (tmp.8 + tmp.9)
    print(tmp.10)
    return 0

block.12:
    goto block.11

block.13:
    collect(32)
    goto block.11

start:
    init.0 = 38
    init.1 = 2
    tmp.3 = free_ptr
    tmp.4 = (tmp.3 + 32)
    tmp.5 = fromspace_end
    if tmp.4 < tmp.5:
      goto block.12
    else:
      goto block.13


CProgram(body={'block.11': [  Assign([Name('alloc.2')], Allocate(length=32, ty=TupleType(types=[IntType(), IntType()]))),   Assign([Subscript(Name('alloc.2'), Constant(0), <ast.Store object at 0x7f0f9132b460>)], Name('init.0')),   Assign([Subscript(Name('alloc.2'), Constant(1), <ast.Store object at 0x7f0f9132ba00>)], Name('init.1')),   Assign([Name('t')], Name('alloc.2')),   Assign([Name('tmp.6')], Subscript(Name('t'), Constant(0), <ast.Load object at 0x7f0f91e7a3e0>)),   Assign([Name('tmp.7')], Subscript(Name('t'), Constant(1), <ast.Load object at 0x7f0f91e7a3e0>)),   Assign([Name('tmp.8')], BinOp(Name('tmp.6'), Add(), Name('tmp.7'))),   Assign([Name('tmp.9')], Call(Name('len'), [Name('t')])),   Assign([Name('tmp.10')], BinOp(Name('tmp.8'), Add(), Name('tmp.9'))),   Expr(Call(Name('print'), [Name('tmp.10')])),   Return(Constant(0))], 'block.12': [Goto(label='block.11')], 'block.13': [Collect(size=32), Goto(label='block.11')], 'start': [  Assign([Name('init.0')], Constant(38)),   Assign([Name('init.1')], Constant(2)),   Assign([Name('tmp.3')], GlobalValue(name='free_ptr')),   Assign([Name('tmp.4')], BinOp(Name('tmp.3'), Add(), Constant(32))),   Assign([Name('tmp.5')], GlobalValue(name='fromspace_end')), If(Compare(Name('tmp.4'), [Lt()], [Name('tmp.5')]), [Goto(label='block.12')], [Goto(label='block.13')])]})
type checking after explicate_control

Traceback (most recent call last):
  File "/home/celebrimbor/Code/compilers/python-compiler/run-tuple-test.py", line 22, in <module>
    run_one_test(os.getcwd() + '/tests/' + lang + '/' + test + '.py', lang,
  File "/home/celebrimbor/Code/compilers/python-compiler/../python-student-support-code/utils.py", line 1606, in run_one_test
    return compile_and_test(compiler, compiler_name, type_check_dict,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/celebrimbor/Code/compilers/python-compiler/../python-student-support-code/utils.py", line 1418, in compile_and_test
    type_check_dict[passname](program)
  File "/home/celebrimbor/Code/compilers/python-compiler/../python-student-support-code/type_check_Cif.py", line 115, in type_check
    self.type_check_stmts(ss[:-1], env)
  File "/home/celebrimbor/Code/compilers/python-compiler/../python-student-support-code/type_check_Cif.py", line 78, in type_check_stmts
    self.type_check_stmt(s, env)
  File "/home/celebrimbor/Code/compilers/python-compiler/../python-student-support-code/type_check_Ctup.py", line 76, in type_check_stmt
    return super().type_check_stmt(s, env)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/celebrimbor/Code/compilers/python-compiler/../python-student-support-code/type_check_Cif.py", line 83, in type_check_stmt
    t = self.type_check_exp(value, env)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/celebrimbor/Code/compilers/python-compiler/../python-student-support-code/type_check_Ctup.py", line 58, in type_check_exp
    return super().type_check_exp(e, env)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/celebrimbor/Code/compilers/python-compiler/../python-student-support-code/type_check_Cif.py", line 74, in type_check_exp
    raise Exception('error in type_check_exp, unexpected ' + repr(e))
Exception: error in type_check_exp, unexpected GlobalValue(name='free_ptr')

@DarshalShetty
Copy link
Collaborator

@Trac91iu's solution to modify type_check_exp function in type_check_Ctup.py seems to stop the type-checker from erroring out for statements like x=free_ptr. Here is what this change would look like:

  def type_check_exp(self, e, env):
    match e:
        case GlobalValue(name):
          return self.type_check_atm(e, env)
         ...

This would make the example code in the textbook and the lecture slides type-check successfully as well. I believe this bug was introduced in this commit: IUCompilerCourse/python-student-support-code@04dbae2. Therefore, it seems like we need to handle the GlobalValue case in order to preserve the behavior of the type-checker before this commit was made.

Let me know if I should push this fix to the Python support code repository @jsiek.

@jsiek
Copy link
Contributor

jsiek commented Nov 14, 2024

Yes, did the same as you suggest. (Didn't see your comment until after I already pushed.)

@jsiek jsiek closed this as completed Nov 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants