Skip to content

Commit

Permalink
Factor out type check error generation to one place and use it consis…
Browse files Browse the repository at this point in the history
…tently. There are improvements we need to do to the reporting, but at least now we need to fix it in one place and the errors will now all consistently contain the type we got and the type we expected.
  • Loading branch information
jnthn committed Jun 30, 2009
1 parent c51b9aa commit fbc5428
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 18 deletions.
31 changes: 31 additions & 0 deletions src/builtins/guts.pir
Expand Up @@ -1679,6 +1679,37 @@ Creates whatever closures (*.foo => { $_.foo })
'die'("You used handles on attribute ", attrname, ", but nothing in the array can do method ", methodname)
.end


=item !make_type_fail_message

Makes a type check failure error message, so we don't have to be doing so all
over the rest of the code base.
=cut
.sub '!make_type_fail_message'
.param string what_failed
.param pmc got_type
.param pmc wanted_type
# Initial bit.
.local string output
output = concat what_failed, " type check failed; expected "
# Work out what we were looking for and show that.
$P0 = wanted_type.'WHAT'()
$S0 = $P0.'perl'()
output = concat $S0
# Report what we actually got.
output = concat ", but got "
$P0 = got_type.'WHAT'()
$S0 = $P0.'perl'()
output = concat $S0
.return (output)
.end
=back
=cut
Expand Down
3 changes: 2 additions & 1 deletion src/classes/Array.pir
Expand Up @@ -350,7 +350,8 @@ Store things into an Array (e.g., upon assignment)
splice self, array, 0, $I0
.return (self)
type_error:
'die'("Type mismatch in assignment to Array.")
$S0 = '!make_type_fail_message'('Array assignment', $P0, type)
'die'($S0)
.end

=back
Expand Down
3 changes: 2 additions & 1 deletion src/classes/Hash.pir
Expand Up @@ -236,7 +236,8 @@ Store a value into a hash.
err_odd_list:
die "Odd number of elements found where hash expected"
type_error:
'die'("Type mismatch in assignment to Hash.")
$S0 = '!make_type_fail_message'('Hash assignment', value, type)
'die'($S0)
.end
Expand Down
6 changes: 2 additions & 4 deletions src/classes/Object.pir
Expand Up @@ -574,10 +574,8 @@ in the future.)
.return (self)
err_type:
$S0 = type.'perl'()
$S1 = source.'WHAT'()
'die'("Type mismatch in assignment; expected something matching type ", $S0, " but got something of type ", $S1)
.return (self)
$S0 = '!make_type_fail_message'('Assignment', source, type)
'die'($S0)
.end
Expand Down
12 changes: 2 additions & 10 deletions src/classes/Signature.pir
Expand Up @@ -344,8 +344,6 @@ Gets a perl representation of the signature.
concat s, ':'
no_trailing_colon:

# XXX TODO: Return type, once we support those.

# Done.
concat s, ')'
.return (s)
Expand Down Expand Up @@ -455,14 +453,8 @@ lexicals as needed and performing type checks.
$P0 = '!DISPATCH_JUNCTION_SINGLE'(callersub, callerlex, callersig)
'return'($P0)
not_junctional:
.local string errmsg
errmsg = 'Parameter type check failed; expected something matching '
$S0 = type.'WHAT'()
concat errmsg, $S0
concat errmsg, ' but got something of type '
$S0 = orig.'WHAT'()
concat errmsg, $S0
.local string callername
.local string errmsg, callername
errmsg = '!make_type_fail_message'('Parameter', orig, type)
callername = callersub
if callername goto have_callername
callername = '<anon>'
Expand Down
11 changes: 9 additions & 2 deletions src/parser/actions.pm
Expand Up @@ -3127,7 +3127,8 @@ sub return_handler_past() {
:name('ACCEPTS'),
PAST::Op.new( :inline(" %r = interpinfo .INTERPINFO_CURRENT_SUB",
" %r = getprop '$!real_self', %r",
" %r = %r.'of'()") ),
" %r = %r.'of'()",
" $P0 = %r") ),
PAST::Var.new( :name('exception'), :scope('register') )
),
PAST::Op.new(
Expand All @@ -3137,7 +3138,13 @@ sub return_handler_past() {
PAST::Op.new(
:pasttype('call'),
:name('die'),
'Type check failed on return value'
PAST::Op.new(
:pasttype('call'),
:name('!make_type_fail_message'),
'Return value',
PAST::Var.new( :name('exception'), :scope('register') ),
PAST::Var.new( :name('$P0'), :scope('register') )
)
)
)
)
Expand Down

0 comments on commit fbc5428

Please sign in to comment.