Skip to content

Commit

Permalink
[mycpp] Remove obsolete casting hack, with type names *_t
Browse files Browse the repository at this point in the history
- _MakeAssignPair() no longer tickles it
- Disabling it no longer makes tests fail

Add failing test for another mycpp code gen bug, involving casting.
  • Loading branch information
Andy C committed Mar 11, 2024
1 parent b2424c8 commit acb9724
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 8 deletions.
5 changes: 3 additions & 2 deletions mycpp/cppgen_pass.py
Original file line number Diff line number Diff line change
Expand Up @@ -1427,8 +1427,9 @@ def visit_assignment_stmt(self, o: 'mypy.nodes.AssignmentStmt') -> T:
# HACK: Distinguish between UP cast and DOWN cast.
# osh/cmd_parse.py _MakeAssignPair does an UP cast within branches.
# _t is the base type, so that means it's an upcast.
if (isinstance(type_expr, NameExpr) and
type_expr.name.endswith('_t')):
if 0:
#if (isinstance(type_expr, NameExpr) and
# type_expr.name.endswith('_t')):
if self.decl:
self.local_var_list.append((lval.name, subtype_name))
self.def_write_ind('%s = %s<%s>(', lval.name, cast_kind,
Expand Down
76 changes: 70 additions & 6 deletions mycpp/examples/test_cast.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#!/usr/bin/env python2
"""
cast.py - For debugging a problem with StackRoots generation
test_cast.py
"""
from __future__ import print_function

import os
from typing import Tuple, cast

from mycpp import mylib
from mycpp.mylib import log
from mycpp.mylib import log, tagswitch


class ColorOutput(object):
Expand All @@ -22,9 +22,9 @@ def __init__(self, f):
def WriteRaw(self, raw):
# type: (Tuple[str, int]) -> None
"""
Write raw data without escaping, and without counting control codes in the
length.
"""
Write raw data without escaping, and without counting control codes in the
length.
"""
s, num_chars = raw
self.f.write(s)
self.num_chars += num_chars
Expand All @@ -37,15 +37,79 @@ def GetRaw(self):
return f.getvalue(), self.num_chars


def run_tests():
def Test1():
# type: () -> None
"""For debugging a problem with StackRoots generation"""

f = mylib.BufWriter()
out = ColorOutput(f)
out.WriteRaw(('yo', 2))
s, num_chars = out.GetRaw()
print(s)


class value_t:

def __init__(self):
# type: () -> None
pass

def tag(self):
# type: () -> int
raise NotImplementedError()


class value__Int(value_t):

def __init__(self, i):
# type: (int) -> None
self.i = i

def tag(self):
# type: () -> int
return 1


class value__Eggex(value_t):

def __init__(self, ere):
# type: (str) -> None
self.ere = ere

def tag(self):
# type: () -> int
return 2


def Test2():
# type: () -> None

# Inspired by HasAffix()

e = value__Eggex('[0-9]+')

pattern_val = e # type: value_t

pattern_eggex = None # type: value__Eggex
i = 42
with tagswitch(pattern_val) as case:
if case(1): # Int
raise AssertionError()
elif case(2):
pattern_eggex = cast(value__Eggex, pattern_val)
else:
raise AssertionError()

print('eggex = %r' % pattern_eggex.ere)


def run_tests():
# type: () -> None

Test1()
#Test2()


def run_benchmarks():
# type: () -> None
raise NotImplementedError()
Expand Down

0 comments on commit acb9724

Please sign in to comment.