Skip to content

Commit 1dfd232

Browse files
committed
Touch-ups, tests, addressing Erik's feedback
1 parent 4f2a7e9 commit 1dfd232

File tree

10 files changed

+517
-329
lines changed

10 files changed

+517
-329
lines changed

py/dml/c_backend.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -805,13 +805,13 @@ def generate_implement_method(device, ifacestruct, meth, indices):
805805
# method in DML
806806
raise EMETH(meth.site, None, 'interface method is variadic')
807807
for ((mp, mt), it) in zip(meth.inp, iface_input_types):
808-
if safe_realtype_unconst(mt).cmp(safe_realtype_unconst(it)) != 0:
808+
if not safe_realtype_unconst(mt).eq(safe_realtype_unconst(it)):
809809
raise EARGT(meth.site, 'implement', meth.name,
810810
mt, mp, it, 'method')
811811
if iface_num_outputs and dml.globals.dml_version != (1, 2):
812812
[(_, mt)] = meth.outp
813-
if safe_realtype_unconst(mt).cmp(
814-
safe_realtype_unconst(func_type.output_type)) != 0:
813+
if not safe_realtype_unconst(mt).eq(
814+
safe_realtype_unconst(func_type.output_type)):
815815
raise EARGT(meth.site, 'implement', meth.name,
816816
mt, '<return value>', func_type.output_type,
817817
'method')

py/dml/clone_test.py

Lines changed: 0 additions & 45 deletions
This file was deleted.

py/dml/codegen.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1507,7 +1507,7 @@ def eval_type(asttype, site, location, scope, extern=False, typename=None,
15071507
raise EBFLD(fsite, "field %s has wrong size" % name)
15081508

15091509
members[name] = (mtype, msb, lsb)
1510-
etype = TInt(width, False, members)
1510+
etype = TInt(width, False, members, label=typename)
15111511
elif tag == 'typeof':
15121512
expr = codegen_expression_maybe_nonvalue(info, location, scope)
15131513
if isinstance(expr, NonValue):

py/dml/compat.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,10 +225,16 @@ class lenient_typechecking(CompatFeature):
225225
macros, because in those scenarios DMLC can become wholly responsible for
226226
proper type checking.
227227
228-
While migrating from this feature, novel type errors due to uses of
229-
`extern`:d macros can often be resolved by changing the signature of
230-
the `extern` declaration to more accurately reflect the macro's effective
231-
type.
228+
While migrating away from this feature, the most common type errors that
229+
its disablement introduces are due to discrepencies between pointer
230+
types. In particular, implicitly discarding `const`-qualification of a
231+
pointer's base type will never be tolerated, and `void` pointers are only
232+
considered equivalent with any other pointer type in the same contexts as
233+
C.
234+
235+
Novel type errors from uses of `extern`:d macros can often be resolved by
236+
changing the signature of the `extern` declaration to more accurately
237+
reflect the macro's effective type.
232238
'''
233239
short = "Make type checking inexact and lenient"
234240
last_api_version = api_7

py/dml/ctree.py

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,7 +1135,7 @@ def as_int(e):
11351135
if t.is_endian:
11361136
(fun, funtype) = t.get_load_fun()
11371137
e = dml.expr.Apply(e.site, mkLit(e.site, fun, funtype), (e,), funtype)
1138-
if not compatible_types(realtype(e.ctype()), target_type):
1138+
if not realtype(e.ctype()).eq(target_type):
11391139
e = mkCast(e.site, e, target_type)
11401140
return e
11411141
else:
@@ -1203,7 +1203,7 @@ def mkIfExpr(site, cond, texpr, fexpr):
12031203
# Normally handled by expr_conditional; this only happens
12041204
# in DMLC-internal mkIfExpr calls
12051205
(result, rtype) = (texpr, ttype) if cond.value else (fexpr, ftype)
1206-
assert rtype.cmp(utype) == 0
1206+
assert rtype.eq(utype)
12071207
return result
12081208
return IfExpr(site, cond, texpr, fexpr, utype)
12091209
else:
@@ -1225,8 +1225,8 @@ def mkIfExpr(site, cond, texpr, fexpr):
12251225
if (isinstance(ttype, (TPtr, TArray))
12261226
and isinstance(ftype, (TPtr, TArray))
12271227
and (ttype.base.void or ftype.base.void
1228-
or compatible_types(safe_realtype_unconst(ttype.base),
1229-
safe_realtype_unconst(ftype.base)))):
1228+
or safe_realtype_unconst(ttype.base).eq(
1229+
safe_realtype_unconst(ftype.base)))):
12301230
# if any branch is void, then the union is too
12311231
base = (ftype if ftype.base.void else ttype).base.clone()
12321232
# if any branch is const *, then the union is too
@@ -1235,8 +1235,7 @@ def mkIfExpr(site, cond, texpr, fexpr):
12351235
or shallow_const(ttype.base)
12361236
or shallow_const(ftype.base))
12371237
utype = TPtr(base)
1238-
elif compatible_types(safe_realtype_unconst(ttype),
1239-
safe_realtype_unconst(ftype)):
1238+
elif safe_realtype_unconst(ttype).eq(safe_realtype_unconst(ftype)):
12401239
utype = ttype
12411240
else:
12421241
raise EBINOP(site, ':', texpr, fexpr)
@@ -1401,8 +1400,8 @@ def make(cls, site, lh, rh):
14011400
if ((lhtype.is_arith and rhtype.is_arith)
14021401
or (isinstance(lhtype, (TPtr, TArray))
14031402
and isinstance(rhtype, (TPtr, TArray))
1404-
and compatible_types(safe_realtype_unconst(lhtype.base),
1405-
safe_realtype_unconst(rhtype.base)))):
1403+
and safe_realtype_unconst(lhtype.base).eq(
1404+
safe_realtype_unconst(rhtype.base)))):
14061405
return cls.make_simple(site, lh, rh)
14071406
raise EILLCOMP(site, lh, lhtype, rh, rhtype)
14081407

@@ -1565,7 +1564,7 @@ def make(cls, site, lh, rh):
15651564
return mkBoolConstant(site, (lhc.node is rhc.node
15661565
and lhc_indices == rhc_indices))
15671566
if (isinstance(lhc, HookRef) and isinstance(rhc, HookRef)
1568-
and lhtype.cmp(rhtype) == 0):
1567+
and lhtype.eq(rhtype)):
15691568
lh_indices = [idx.value for idx in lhc.indices]
15701569
rh_indices = [idx.value for idx in rhc.indices]
15711570
return mkBoolConstant(site, (lhc.hook is rhc.hook
@@ -1607,9 +1606,9 @@ def make(cls, site, lh, rh):
16071606
if ((lhtype.is_arith and rhtype.is_arith)
16081607
or (isinstance(lhtype, (TPtr, TArray))
16091608
and isinstance(rhtype, (TPtr, TArray))
1610-
and (compatible_types(safe_realtype_unconst(lhtype.base),
1611-
safe_realtype_unconst(rhtype.base))
1612-
or lhtype.base.void or rhtype.base.void))
1609+
and (lhtype.base.void or rhtype.base.void
1610+
or safe_realtype_unconst(lhtype.base).eq(
1611+
safe_realtype_unconst(rhtype.base))))
16131612
or (isinstance(lhtype, TBool) and isinstance(rhtype, TBool))):
16141613
return Equals(site, lh, rh)
16151614

@@ -1618,7 +1617,7 @@ def make(cls, site, lh, rh):
16181617
return IdentityEq(site, TraitObjIdentity(lh.site, lh),
16191618
TraitObjIdentity(rh.site, rh))
16201619
if (isinstance(lhtype, THook) and isinstance(rhtype, THook)
1621-
and lhtype.cmp(rhtype) == 0):
1620+
and lhtype.eq(rhtype)):
16221621
return IdentityEq(site, lh, rh)
16231622

16241623
raise EILLCOMP(site, lh, lhtype, rh, rhtype)
@@ -2940,8 +2939,8 @@ def mkInterfaceMethodRef(site, iface_node, indices, method_name):
29402939
if (not isinstance(ftype, TPtr)
29412940
or not isinstance(ftype.base, TFunction)
29422941
or not ftype.base.input_types
2943-
or TPtr(safe_realtype_unconst(TNamed('conf_object_t'))).cmp(
2944-
safe_realtype_unconst(ftype.base.input_types[0])) != 0):
2942+
or not TPtr(safe_realtype_unconst(TNamed('conf_object_t'))).eq(
2943+
safe_realtype_unconst(ftype.base.input_types[0]))):
29452944
# non-method members are not accessible
29462945
raise EMEMBER(site, struct_name, method_name)
29472946

@@ -4826,18 +4825,13 @@ def mkCast(site, expr, new_type):
48264825
return Cast(site, expr, new_type)
48274826
if isinstance(real, (TVoid, TArray, TFunction)):
48284827
raise ECAST(site, expr, new_type)
4829-
if old_type.cmp(real) == 0:
4830-
if (old_type.is_int
4831-
and not old_type.is_endian
4832-
and dml.globals.compat_dml12_int(expr.site)):
4828+
if old_type.eq(real):
4829+
if (dml.globals.compat_dml12_int(expr.site)
4830+
and old_type.is_int
4831+
and not old_type.is_endian):
48334832
# 1.2 integer expressions often lie about their actual type,
48344833
# and require a "redundant" cast! Why yes, this IS horrid!
48354834
return Cast(site, expr, new_type)
4836-
if real.is_int and real.members is not None:
4837-
# An integer type can be compatible with a bitfields without being
4838-
# equal to it, as DMLC will treat bitfields differently. Leverage
4839-
# Cast in this case
4840-
return Cast(site, expr, new_type)
48414835
return mkRValue(expr)
48424836
if isinstance(real, (TStruct, TExternStruct, TVector, TTraitList)):
48434837
raise ECAST(site, expr, new_type)

py/dml/ctree_test.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,8 @@ def framework_assertions(self):
284284
@subtest()
285285
def long_types(self):
286286
def tcmp(t1, t2):
287-
left = t1.cmp(t2)
288-
right = t2.cmp(t1)
287+
left = t1.eq(t2)
288+
right = t2.eq(t1)
289289
self.assertEqual(left, right)
290290
return left
291291
u = types.TLong(False)
@@ -301,8 +301,7 @@ def tcmp(t1, t2):
301301
# incompatible
302302
for t1 in int_types:
303303
for t2 in int_types:
304-
self.assertEqual(tcmp(t1, t2),
305-
0 if t1 is t2 else NotImplemented, (t1, t2))
304+
self.assertEqual(tcmp(t1, t2), t1 is t2, (t1, t2))
306305

307306
return [u.declaration('u') + ';',
308307
'unsigned long *up UNUSED = &u;',
@@ -399,7 +398,7 @@ def int_cast_sequence(self):
399398
curr_expr = base
400399
for t in cast_sequence:
401400
curr_expr = ctree.mkCast(site, curr_expr, t)
402-
self.assertEqual(cast_sequence[-1].cmp(curr_expr.ctype()), 0)
401+
self.assertTrue(cast_sequence[-1].eq(curr_expr.ctype()))
403402
self.assertEqual(curr_expr.expr, base)
404403

405404
@subtest()
@@ -417,7 +416,7 @@ def int_cast_sequence_bit_increase(self):
417416
def cast_to_int(self, old_type, old_value, new_type, new_value):
418417
cast = ctree.mkCast(site, ctree.mkLit(site, old_value, old_type),
419418
new_type)
420-
self.assertTrue(types.compatible_types(cast.ctype(), new_type))
419+
self.assertTrue(cast.ctype().eq(new_type))
421420
return ['EXPECT(%s == %s);' % (cast.read(), new_value)]
422421

423422
def unendianed_type(self, endian_type):
@@ -460,8 +459,8 @@ def as_int(self, expr, value):
460459
])
461460
def endian_int_cast(self, expr, endiantype):
462461
"""Check that casting to endianint results in the right type"""
463-
self.assertEqual(
464-
ctree.mkCast(site, expr, endiantype).ctype().cmp(endiantype), 0)
462+
self.assertTrue(
463+
ctree.mkCast(site, expr, endiantype).ctype().eq(endiantype))
465464

466465
@subtest([(targettype, endiantype)
467466
for targettype in (
@@ -483,7 +482,7 @@ def endian_int_cast_from(self, targettype, endiantype):
483482
ctree.mkIntegerConstant(site, -5, True),
484483
endiantype)
485484
cast = ctree.mkCast(site, expr, targettype)
486-
self.assertEqual(cast.ctype().cmp(targettype), 0)
485+
self.assertTrue(cast.ctype().eq(targettype))
487486
return ['%s UNUSED = %s;' % (targettype.declaration('x'), cast.read())]
488487

489488
@subtest([(op, lh, rh)
@@ -535,7 +534,7 @@ def as_int_binop_coverage(self, op, lh, rh):
535534
types.TInt(64, True))])
536535
def as_int_ifexpr(self, lh, rh, expected_type):
537536
expr = ctree.mkIfExpr(site, variable('b', types.TBool()), lh, rh)
538-
self.assertEqual(expr.type.cmp(expected_type), 0)
537+
self.assertTrue(expr.type.eq(expected_type))
539538
return ["bool b = false;",
540539
"EXPECT(%s == %s);" % (
541540
expr.read(), ctree.mkCast(site, rh, expected_type).read())]
@@ -572,7 +571,7 @@ def as_int_unop_coverage(self, op, lh):
572571
def as_int_new_coverage(self, count):
573572
"""Checks for as_int coverage"""
574573
expr = ctree.mkNew(site, types.TInt(8, False), count)
575-
self.assertEqual(expr.type.cmp(types.TPtr(types.TInt(8, False))), 0)
574+
self.assertTrue(expr.type.eq(types.TPtr(types.TInt(8, False))))
576575
return ["EXPECT(%s == 3);" % expr.count.read()]
577576

578577
def expect_int_unop(self, const, unop, expected):
@@ -1025,8 +1024,7 @@ def add_sub_ptr(self, ptype):
10251024
sub1 = ctree.mkSubtract(site, ptr, ctree.mkUnaryMinus(site, i))
10261025
sub2 = ctree.mkSubtract(site, ptr, add1)
10271026
for expr in (add1, add2, sub1):
1028-
self.assertTrue(types.compatible_types(
1029-
expr.ctype().base, ptype))
1027+
self.assertTrue(expr.ctype().base.eq(ptype))
10301028
self.expect_int_type(sub2.ctype(), True)
10311029
stmts.extend([
10321030
'EXPECT(%s == exp);' % (add1.read(),),
@@ -1416,8 +1414,6 @@ def const_types(self):
14161414
# abstract type
14171415
types.IntegerType,
14181416
# abstract type
1419-
types.ArchDependentIntegerType,
1420-
# abstract type
14211417
types.StructType,
14221418
# 1.2, weird
14231419
types.TUnknown,

py/dml/structure.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -740,9 +740,9 @@ def typecheck_method_override(m1, m2, location):
740740
type1 = safe_realtype_unconst(type1)
741741
type2 = safe_realtype_unconst(type2)
742742

743-
ok = (type1.cmp_fuzzy(type2)
743+
ok = (type1.eq_fuzzy(type2)
744744
if compat.lenient_typechecking in dml.globals.enabled_compat
745-
else type1.cmp(type2)) == 0
745+
else type1.eq(type2))
746746
if not ok:
747747
raise EMETH(a1.site, a2.site,
748748
f"mismatching types in input argument {n1}")
@@ -754,9 +754,9 @@ def typecheck_method_override(m1, m2, location):
754754
(_, type2) = eval_type(t2, a2.site, location, global_scope)
755755
type1 = safe_realtype_unconst(type1)
756756
type2 = safe_realtype_unconst(type2)
757-
ok = (type1.cmp_fuzzy(type2)
757+
ok = (type1.eq_fuzzy(type2)
758758
if compat.lenient_typechecking in dml.globals.enabled_compat
759-
else type1.cmp(type2)) == 0
759+
else type1.eq(type2))
760760
if not ok:
761761
msg = "mismatching types in return value"
762762
if len(outp1) > 1:

py/dml/traits.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -403,18 +403,18 @@ def typecheck_method_override(left, right):
403403
for ((n, t0), (_, t1)) in zip(inp0, inp1):
404404
t0 = safe_realtype_unconst(t0)
405405
t1 = safe_realtype_unconst(t1)
406-
ok = (t0.cmp_fuzzy(t1)
406+
ok = (t0.eq_fuzzy(t1)
407407
if compat.lenient_typechecking in dml.globals.enabled_compat
408-
else t0.cmp(t1)) == 0
408+
else t0.eq(t1))
409409
if not ok:
410410
raise EMETH(site0, site1,
411411
"mismatching types in input argument %s" % (n,))
412412
for (i, ((_, t0), (_, t1))) in enumerate(zip(outp0, outp1)):
413413
t0 = safe_realtype_unconst(t0)
414414
t1 = safe_realtype_unconst(t1)
415-
ok = (t0.cmp_fuzzy(t1)
415+
ok = (t0.eq_fuzzy(t1)
416416
if compat.lenient_typechecking in dml.globals.enabled_compat
417-
else t0.cmp(t1)) == 0
417+
else t0.eq(t1))
418418
if not ok:
419419
raise EMETH(site0, site1,
420420
"mismatching types in output argument %d" % (i + 1,))

0 commit comments

Comments
 (0)