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

unknown attribute in Regex should not compile #3430

Closed
p6rt opened this issue Jul 9, 2014 · 7 comments
Closed

unknown attribute in Regex should not compile #3430

p6rt opened this issue Jul 9, 2014 · 7 comments

Comments

@p6rt
Copy link

p6rt commented Jul 9, 2014

Migrated from rt.perl.org#122253 (status was 'resolved')

Searchable as RT122253$

@p6rt
Copy link
Author

p6rt commented Jul 9, 2014

From @lizmat

[13​:33​:31] <lizmat> m​: class A { has $.a = "a"; method b { "aaa".subst(/$!a/,"b") }}; say A.new.a; say A.new.b # why isn't the .subst working ?
[13​:33​:32] <+camelia> rakudo-moar 41574a​: OUTPUT«a␤aaa␤»
[13​:34​:31] <lizmat> m​: class A { has $.a = "a"; method b { my $x = $!a; "aaa".subst(/$x/,"b") }}; say A.new.b # putting it into a lexical first works
[13​:34​:33] <+camelia> rakudo-moar 41574a​: OUTPUT«baa␤»
[13​:34​:50] <lizmat> rakudobug?
[13​:35​:55] <lizmat> FROGGS, jnthn​: ^^^
[13​:36​:47] <jnthn> m​: /$!foo/
[13​:36​:48] <+camelia> rakudo-moar 41574a​: OUTPUT«�[31m===�[0mSORRY!�[31m===�[0m Error while compiling /tmp/GrGGxT5ir_␤Variable $!foo used where no 'self' is available␤at /tmp/GrGGxT5ir_​:1␤------&gt; �[32m/$!foo�[33m⏏�[31m/�[0m␤»
[13​:36​:56] <FROGGS> m​: class A { has $.a = "a"; method b { "aaa".subst(/"$!a"/,"b") }}; say A.new.a; say A.new.b
[13​:36​:57] <+camelia> rakudo-moar 41574a​: OUTPUT«a␤use of uninitialized value of type Any in string context␤baaa␤»
[13​:37​:21] <FROGGS> that seems to be the runtime lexpad problem thingy masak++ had described in his blög
[13​:37​:35] <jnthn> Um
[13​:37​:38] <Ven> class variables are in lexpads ?
[13​:37​:49] <jnthn> Wait...a regex is method-ish, and has its own self...
[13​:37​:56] <FROGGS> eww
[13​:38​:16] <FROGGS> m​: class A { has $.a = "a"; method b { "aaa".subst($!a,"b") }}; say A.new.a; say A.new.b
[13​:38​:17] <lizmat> aha... the plot thickens... rakudobug it ?
[13​:38​:18] <+camelia> rakudo-moar 41574a​: OUTPUT«a␤baa␤»
[13​:38​:24] <FROGGS> yeah
[13​:38​:28] <FROGGS> jnthn​: is right
[13​:38​:56] <jnthn> lizmat​: Um, at this point all we have is "it looks odd" rather than "it should do X"
[13​:38​:59] <FROGGS> m​: class A { has $.a = "a"; method b { "aaa".subst(/{say self}/,"b") }}; say A.new.a; say A.new.b
[13​:39​:02] <+camelia> rakudo-moar 41574a​: OUTPUT«a␤A.new(a => "a")␤baaa␤»
[13​:39​:16] <FROGGS> I have no idea
[13​:39​:25] <FROGGS> m​: class A { has $.a = "a"; method b { "aaa".subst(/"{say self}"/,"b") }}; say A.new.a; say A.new.b
[13​:39​:27] <+camelia> rakudo-moar 41574a​: OUTPUT«a␤A.new(a => "a")␤A.new(a => "a")␤A.new(a => "a")␤A.new(a => "a")␤aaa␤»
[13​:40​:02] <jnthn> m​: class A { has $.a = "a"; method b { "aaa".subst(/​:my $ = say $!a; ./, b') } }; A.new.b
[13​:40​:04] <+camelia> rakudo-moar 41574a​: OUTPUT«�[31m===�[0mSORRY!�[31m===�[0m Error while compiling /tmp/TCkwyckpg_␤Unable to parse expression in argument list; couldn't find final ')' ␤at /tmp/TCkwyckpg_​:1␤------> �[32m b { "aaa".subst(/​:my $ = say $!a; ./, b�[33m⏏�[31m') } }; A.new.b�[0m␤ …»
[13​:40​:13] <jnthn> m​: class A { has $.a = "a"; method b { "aaa".subst(/​:my $ = say $!a; ./, 'b') } }; A.new.b
[13​:40​:15] <+camelia> rakudo-moar 41574a​: OUTPUT«(Any)␤»
[13​:40​:42] <lizmat> m​: say "aaa".subst(/a$/,"b") # the reason I ran into this
[13​:40​:43] <+camelia> rakudo-moar 41574a​: OUTPUT«aab␤»
[13​:40​:51] <jnthn> m​: class A { has $.a = "a"; method b { "aaa".subst(/​:my $ = say self.WHICH; ./, 'b') } }; A.new.b
[13​:40​:54] <+camelia> rakudo-moar 41574a​: OUTPUT«Cursor|139834212483800␤»
[13​:42​:13] <jnthn> m​: class A { has $.a = "a"; method b { "aaa".subst(/​:my $ = say self.WHICH, $!a; ./, 'b') } }; A.new.b
[13​:42​:15] <+camelia> rakudo-moar 41574a​: OUTPUT«Cursor|139900812459976(Any)␤»
[13​:42​:21] <jnthn> m​: class A { has $.a = "a"; method b { "aaa".subst(/​:my $ = say self.WHICH, $!pos; ./, 'b') } }; A.new.b
[13​:42​:22] <+camelia> rakudo-moar 41574a​: OUTPUT«�[31m===�[0mSORRY!�[31m===�[0m Error while compiling /tmp/DBWEyB7Zo2␤Attribute $!pos not declared in class A␤at /tmp/DBWEyB7Zo2​:1␤------> �[32m $ = say self.WHICH, $!pos; ./, 'b') } }�[33m⏏�[31m; A.new.b�[0m␤ expecting any of​:␤ postfi…»
[13​:42​:51] <jnthn> m​: class A { has $!dummy; has $.a = "a"; method b { "aaa".subst(/​:my $ = say self.WHICH, $!a; ./, 'b') } }; A.new.b
[13​:42​:53] <+camelia> rakudo-moar 41574a​: OUTPUT«Cursor|140307760568368Cursor|140307760671904No such method 'gist' for invocant of type 'ParseShared'␤ in sub say at src/gen/m-CORE.setting​:13805␤ in method match at src/gen/m-CORE.setting​:6280␤ in method subst at src/gen/m-CORE.setting​:6355␤ in…»
[13​:42​:59] <jnthn> Wow.
[13​:43​:22] <jnthn> Yes, that's quite a mis-compile.
[13​:43​:36] <jnthn> I know what it's doing.
[13​:43​:47] <jnthn> I don't know we can make it DWLM though...
[13​:43​:57] <jnthn> Because self in a /.../ thing really is Cursor
[13​:44​:13] <FROGGS> L is for lizmat? :o)
[13​:44​:17] <jnthn> Yes.
[13​:44​:27] <FROGGS> true... and in a code somebody might really want the Cursor
[13​:44​:34] <jnthn> Right
[13​:44​:35] <lizmat> ok
[13​:44​:40] <jnthn> Well, it's clearer with tokens and so forth.
[13​:44​:56] <jnthn> All the same, we shouldn't compile it to stuff that looks up a random attribute on the wrong object. :)
[13​:45​:29] <lizmat> I guess it should at least complain about it being an attribute that doesn't exist on the Regex ?
[13​:45​:38] <jnthn> lizmat​: Yeah.
[13​:45​:44] <lizmat> so rakudobug it ?
[13​:45​:55] <jnthn> Should just not compile
[13​:46​:06] <jnthn> Yes, now we know the expected behavior, it's a resolvable ticket :)
[13​:46​:14] <jnthn> So go for it :)

@p6rt
Copy link
Author

p6rt commented Oct 3, 2014

From @masak

<TuxCM> http://pastebin.com/PpDKvrd9 <= I do not understand
<Ven> pmurias​: right -- I agree with that! I was just wondering why
angular of all the template systems
<moritz> TuxCM​: seems to be a scoping bug of sorts
<masak> m​: my $sep = "|"; class C { method foo { .say for
"foo|bar".split(/$sep/).map(*) } }; C.new.foo
<masak> m​: class C { has $.sep = "|"; method foo { .say for
"foo|bar".split(/$!sep/).map(
*) } }; C.new.foo
<camelia> rakudo-moar da3aae​: OUTPUT«foo|bar␤»
<masak> TuxCM​: interesting.
* masak submits rakudobug

@p6rt
Copy link
Author

p6rt commented Oct 3, 2014

From @pmichaud

On Fri, Oct 03, 2014 at 06​:48​:50AM -0700, Carl Mäsak wrote​:

<masak> m​: class C { has $.sep = "|"; method foo { .say for
"foo|bar".split(/$!sep/).map(~*) } }; C.new.foo
<camelia> rakudo-moar da3aae​: OUTPUT«foo|bar␤»
<masak> TuxCM​: interesting.
* masak submits rakudobug

14​:09 <pmichaud> m​: class C { has $.rx = 'bc'; method foo { say 'abcd' ~~ /$!rx/; } }; C.new.foo
14​:09 <camelia> rakudo-moar da3aae​: OUTPUT«Nil␤»
14​:10 <pmichaud> I'm guessing that regexes don't know how to interpolate $!vars
14​:10 <pmichaud> m​: class C { has $.rx = 'bc'; method foo { say $!rx; say 'abcd' ~~ /$!rx/; } }; C.new.foo
14​:10 <camelia> rakudo-moar da3aae​: OUTPUT«bc␤Nil␤»
14​:11 <moritz> or maybe the attribute lookup gets the cursor object as invocant?

Pm

@p6rt
Copy link
Author

p6rt commented Oct 3, 2014

The RT System itself - Status changed from 'new' to 'open'

@p6rt
Copy link
Author

p6rt commented Mar 11, 2015

From @jnthn

On Wed Jul 09 04​:48​:43 2014, elizabeth wrote​:

[13​:33​:31] <lizmat> m​: class A { has $.a = "a"; method b {
"aaa".subst(/$!a/,"b") }}; say A.new.a; say A.new.b # why isn't the
.subst working ?
[13​:33​:32] <+camelia> rakudo-moar 41574a​: OUTPUT«a␤aaa␤»
[13​:34​:31] <lizmat> m​: class A { has $.a = "a"; method b { my $x
= $!a; "aaa".subst(/$x/,"b") }}; say A.new.b # putting it into a
lexical first works
[13​:34​:33] <+camelia> rakudo-moar 41574a​: OUTPUT«baa␤»
[13​:34​:50] <lizmat> rakudobug?
[13​:35​:55] <lizmat> FROGGS, jnthn​: ^^^
<snip>
[13​:42​:59] <jnthn> Wow.
[13​:43​:22] <jnthn> Yes, that's quite a mis-compile.
[13​:43​:36] <jnthn> I know what it's doing.
[13​:43​:47] <jnthn> I don't know we can make it DWLM though...
[13​:43​:57] <jnthn> Because self in a /.../ thing really is
Cursor
[13​:44​:13] <FROGGS> L is for lizmat? :o)
[13​:44​:17] <jnthn> Yes.
[13​:44​:27] <FROGGS> true... and in a code somebody might really
want the Cursor
[13​:44​:34] <jnthn> Right
[13​:44​:35] <lizmat> ok
[13​:44​:40] <jnthn> Well, it's clearer with tokens and so forth.
[13​:44​:56] <jnthn> All the same, we shouldn't compile it to
stuff that looks up a random attribute on the wrong object. :)
[13​:45​:29] <lizmat> I guess it should at least complain about it
being an attribute that doesn't exist on the Regex ?
[13​:45​:38] <jnthn> lizmat​: Yeah.
[13​:45​:44] <lizmat> so rakudobug it ?
[13​:45​:55] <jnthn> Should just not compile
[13​:46​:06] <jnthn> Yes, now we know the expected behavior, it's
a resolvable ticket :)
[13​:46​:14] <jnthn> So go for it :)

And it now gives a decent error and a suggestion of what to do​:

$ perl6-m -e "class C { has $!a = 'a'; method m() { say 'a' ~~ /$!a/ } }; C.new.m"
===SORRY!=== Error while compiling -e
Attribute $!a not available inside of a regex, since regexes are methods on Cursor.
Consider storing the attribute in a lexical, and using that in the regex.
at -e​:1
------> $!a = 'a'; method m() { say 'a' ~~ /$!a<HERE>/ } }; C.new.m

Made it a typed exception, and tested it in S05-interpolation/regex-in-variable.t. Resolving.

/jnthn

@p6rt
Copy link
Author

p6rt commented Mar 11, 2015

The RT System itself - Status changed from 'new' to 'open'

@p6rt
Copy link
Author

p6rt commented Mar 11, 2015

@jnthn - Status changed from 'open' to 'resolved'

@p6rt p6rt closed this as completed Mar 11, 2015
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

1 participant