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

Return type in prototype does not work when the last parameter has a default value #21

Open
baby-gnu opened this issue Jan 2, 2017 · 1 comment

Comments

@baby-gnu
Copy link

baby-gnu commented Jan 2, 2017

Hello,

Doing some tests with Kavorka features show me an issue when the last parameter has a default value.

Here are some tests to add to t/80returntype.t:

diff --git a/t/80returntype.t b/t/80returntype.t
index dbb4a14..fa2d185 100644
--- a/t/80returntype.t
+++ b/t/80returntype.t
@@ -67,4 +67,23 @@ is_deeply( [add3(4,5)], [4,5] );
 like(exception { my @r = add3(4.1,4.9) }, qr{did not pass type constraint "ArrayRef.Int." at \S+ line 67});
 like(exception { my @r = add3(4.1,5) }, qr{did not pass type constraint "ArrayRef.Int." at \S+ line 68});
 
+fun add4($a, $b? → Int){
+	if ($b) {
+		return $a + $b;
+	}
+	else {
+		return $a+1;
+	}
+}
+
+is( add4(1,2), 3, 'return type constraint works with optional parameter set');
+is( add4(1), 2, 'return type constraint works with optional parameter unset');
+
+fun add5($a, $b = 1 → Int) {
+	return $a + $b;
+}
+
+is( add4(1,2), 3, 'return type constraint works with default parameter set');
+is( add4(1), 2, 'return type constraint works with default parameter unset');
+
 done_testing;

The test does not compile at all:

prove -rlv t/80returntype.t 
t/80returntype.t .. Unrecognized character \x{2192}; marked by <-- HERE after a, $b = 1 <-- HERE near column 21 at t/80returntype.t line 82.
Dubious, test returned 255 (wstat 65280, 0xff00)
No subtests run 

Test Summary Report
-------------------
t/80returntype.t (Wstat: 65280 Tests: 0 Failed: 0)
  Non-zero exit status: 255
  Parse errors: No plan found in TAP output
Files=1, Tests=0,  1 wallclock secs ( 0.02 usr  0.00 sys +  0.28 cusr  0.01 csys =  0.31 CPU)
Result: FAIL

I tracked the problem up to Kavorka::Parameter::parse

Set a debug point:

diff --git a/lib/Kavorka/Parameter.pm b/lib/Kavorka/Parameter.pm
index f709630..631fcdb 100644
--- a/lib/Kavorka/Parameter.pm
+++ b/lib/Kavorka/Parameter.pm
@@ -271,6 +271,7 @@ sub parse
 	
 	if (lex_peek(5) =~ m{ \A ( (?: [/]{2} | [|]{2} )?= ) }x)
 	{
+		$DB::single = 1;
 		$default_when = $1;
 		lex_read(length($1));
 		lex_read_space;

Which results in the following trace:

perl -I lib/ -d t/80returntype.t 

Loading DB routines from perl5db.pl version 1.49_05
Editor support available.

Enter h or 'h h' for help, or 'man perldebug' for more help.

Kavorka::Parameter::parse(lib/Kavorka/Parameter.pm:275):
275:			$default_when = $1;
  DB<1> n
Kavorka::Parameter::parse(lib/Kavorka/Parameter.pm:276):
276:			lex_read(length($1));
  DB<1> 
Kavorka::Parameter::parse(lib/Kavorka/Parameter.pm:277):
277:			lex_read_space;
278:			$default = lex_peek(5) =~ m{ \A (?: when\b | [,)] ) }x
  DB<1> 
Kavorka::Parameter::parse(lib/Kavorka/Parameter.pm:280):
280:				: parse_arithexpr;
  DB<1> 
Unrecognized character \x{2192}; marked by <-- HERE after a, $b = 1 <-- HERE near column 21 at t/80returntype.t line 82.
 at lib/Kavorka/Signature.pm line 73.
	Kavorka::Signature::parse("Kavorka::Signature", "package", "main", "_is_dummy", 0) called at lib/Kavorka/Sub.pm line 221
	Kavorka::Sub::parse_signature(Kavorka::Sub::Fun=HASH(0x561606be0968)) called at lib/Kavorka/Sub.pm line 123
	Kavorka::Sub::parse("Kavorka::Sub::Fun", "keyword", "fun") called at lib/Kavorka.pm line 187
	Kavorka::parse_fun("fun") called at t/80returntype.t line 82
Debugged program terminated.  Use q to quit or R to restart,
use o inhibit_exit to avoid stopping after program termination,
h q, h R or h o to get additional info.

Regards.

@tobyink
Copy link
Owner

tobyink commented Feb 4, 2017

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

2 participants