Pattern matching/extractor object weirdness

41 views
Skip to first unread message

Olivier Pernet

unread,
Feb 11, 2011, 2:06:44 PM2/11/11
to scala-l...@googlegroups.com
Hi all,

I'm running into a strange issue involving pattern matching and an
extractor object.

object SymbolMatcher {
def unapply(tree: Tree): Option[String] = tree match {
case Apply(_, StringLit(s)::Nil) if tree.symbol == symbolApplyMethod =>
Some(s)
case _ =>
None
}
}
}

...
arg match {
case Apply(_, List(SymbolMatcher(label), arg)) =>
(Some(label), getType(arg))
//case Apply(_,StringLit(label)::Nil) if arg.symbol == symbolApplyMethod =>
case SymbolMatcher(label) =>
println("normal")
(Some(label), None)
case arg => arg match {
case SymbolMatcher(label) =>
println("strange...")
(Some(label), None)
case x =>
(None, getType(arg))
}
}

The pattern goes into the "strange..." branch. However, if I use the
commented out line instead of case SymbolMatcher(label), the pattern
goes into the "normal" branch.
Am I right to think that this isn't normal?

Olivier

Paul Phillips

unread,
Feb 11, 2011, 2:22:35 PM2/11/11
to scala-l...@googlegroups.com, Olivier Pernet
It's nice how the subject line alone is enough to fill me with dread.

On 2/11/11 11:06 AM, Olivier Pernet wrote:
> I'm running into a strange issue involving pattern matching and an
> extractor object.

I'm sure it's the famous (for some of us) 1697/2337.

http://lampsvn.epfl.ch/trac/scala/ticket/1697
"Compiler generates wrong code from some code using extractor"
https://lampsvn.epfl.ch/trac/scala/ticket/2337
"Transition from pattern match based on extractor with a typed parameter
in unapply() skips next case"

> Am I right to think that this isn't normal?

Unfortunately it depends on how we define normalcy.

Kevin Wright

unread,
Feb 11, 2011, 2:29:02 PM2/11/11
to scala-l...@googlegroups.com, Olivier Pernet

What do you mean "we"? You already know most of us know better by now and will just defer to your definition! :)

(eventually, anyway...)

Olivier Pernet

unread,
Feb 11, 2011, 2:32:10 PM2/11/11
to Paul Phillips, scala-l...@googlegroups.com
On Fri, Feb 11, 2011 at 19:22, Paul Phillips <pa...@improving.org> wrote:
> It's nice how the subject line alone is enough to fill me with dread.
>
> On 2/11/11 11:06 AM, Olivier Pernet wrote:
>>
>> I'm running into a strange issue involving pattern matching and an
>> extractor object.
>
> I'm sure it's the famous (for some of us) 1697/2337.
>
> http://lampsvn.epfl.ch/trac/scala/ticket/1697
> "Compiler generates wrong code from some code using extractor"
> https://lampsvn.epfl.ch/trac/scala/ticket/2337
> "Transition from pattern match based on extractor with a typed parameter in
> unapply() skips next case"

Right, this does look a lot like my problem.
Do you have an idea of what's going on there?

Olivier

Paul Phillips

unread,
Feb 11, 2011, 3:09:00 PM2/11/11
to Olivier Pernet, scala-l...@googlegroups.com
On 2/11/11 11:32 AM, Olivier Pernet wrote:
> Right, this does look a lot like my problem.
> Do you have an idea of what's going on there?

Here, this will get you started.

http://permalink.gmane.org/gmane.comp.lang.scala.internals/2752

http://scala-programming-language.1934581.n4.nabble.com/Possible-scala-partial-function-pattern-matching-bug-td3029632.html#none

There is little doubt in my mind at this point that this bug will be
gone after I rewrite the matcher and not before, because we're years
into this now and there's still no sign of anyone who can help me with
the pattern matcher. So it has to be me, and I'm done working it from
the inside.

Olivier Pernet

unread,
Feb 11, 2011, 4:59:15 PM2/11/11
to Paul Phillips, scala-l...@googlegroups.com
Thanks, this makes more sense now. Have you started your pattern
matching redesign? I'd be curious to see what all the requirements are
- I guess I could work it out from the spec, but I don't know much
about the environment the code has to fit in.

Do you think I would be safe from the bug if I wrote all my extractors
to take Any as a parameter and do the type check inside the extractor?

Olivier

Paul Phillips

unread,
Feb 11, 2011, 5:26:18 PM2/11/11
to Olivier Pernet, scala-l...@googlegroups.com
On 2/11/11 1:59 PM, Olivier Pernet wrote:
> Thanks, this makes more sense now. Have you started your pattern
> matching redesign? I'd be curious to see what all the requirements are
> - I guess I could work it out from the spec, but I don't know much
> about the environment the code has to fit in.

I have started it. It's the kind of thing I will need to focus on for a
while though, and I'm not sure when that's going to be a viable thing
for me to do.

> Do you think I would be safe from the bug if I wrote all my extractors
> to take Any as a parameter and do the type check inside the extractor?

I am fairly sure that leaves you safe from this bug. This does not
imply there aren't others.

Olivier Pernet

unread,
Feb 11, 2011, 5:46:21 PM2/11/11
to Paul Phillips, scala-l...@googlegroups.com
On Fri, Feb 11, 2011 at 22:26, Paul Phillips <pa...@improving.org> wrote:
> On 2/11/11 1:59 PM, Olivier Pernet wrote:
>>
>> Thanks, this makes more sense now. Have you started your pattern
>> matching redesign? I'd be curious to see what all the requirements are
>> - I guess I could work it out from the spec, but I don't know much
>> about the environment the code has to fit in.
>
> I have started it.  It's the kind of thing I will need to focus on for a
> while though, and I'm not sure when that's going to be a viable thing for me
> to do.

Do you have anything online about it? I'd like to help, but I'd need
to learn a lot first I'm sure...

>> Do you think I would be safe from the bug if I wrote all my extractors
>> to take Any as a parameter and do the type check inside the extractor?
>
> I am fairly sure that leaves you safe from this bug.  This does not imply
> there aren't others.

Actually, I just tried, and the bug was still there.

Olivier

Paul Phillips

unread,
Feb 11, 2011, 6:40:19 PM2/11/11
to Olivier Pernet, scala-l...@googlegroups.com
On 2/11/11 2:46 PM, Olivier Pernet wrote:
> Do you have anything online about it? I'd like to help, but I'd need
> to learn a lot first I'm sure...

I'd like to put a lot of my work in progress online one of these days,
but it isn't yet. Hang onto that interest.

Reply all
Reply to author
Forward
0 new messages