petdance/scratch — Issue #255: Get_class <namespace> confuses NSes that have same final name.
Author: petdance
Created: 2011-11-01 04:25:28+00:00
State: open

Having already created a class "Matcher", I was trying in NQP to create a related class Mimus::CallSignature::Matcher. (Note the last name.)

Far too many hours later, it appears that the get_class op on a namespace apparently returns invalid results when the namespace shares a last name with an already-extant class.

This code, inspired by the `get_parrotclass` method in P6object.pir, looks up (and tries to create, if needed) two classes with different namespace paths but the same class name (last name in the nsp), one called P and the other N::S::P :
{{{
.sub foo :main
.local pmc class_p
.local int addr_p

.local pmc class_nsp
.local int addr_nsp

class_p = newclass [ 'P' ]
addr_p = get_addr class_p

$S0 = 'N::S::P'	# Change to ..Q and it changes
$P0 = split '::', $S0

$P1 = get_hll_namespace $P0
unless null $P1 goto have_ns

die "Namespace was null"
have_ns:

class_nsp = get_class $P1
$P2 = class_nsp
addr_nsp = get_addr class_nsp

print "Addr of class 'P' is: "
say addr_p

print "Addr of 'N::S::P' is: "
say addr_nsp

unless addr_p == addr_nsp goto end
say "*** The addrs are the same. I think that's wrong."

goto end

got_class:
say "Got the class. I didn't see that coming."
end:
.end

.namespace ['N';'S';'P']

.sub bar :method
.return (1)
.end

.namespace ['N';'S';'Q']

.sub bar :method
.return (1)
.end
}}}

The effect of this seems to be that any code which relies on P6object cannot create classes that have the same name.

The workaround is, trivially, to make sure that class names are distinct. Anyone who has ever coded C89 should be okay.