7375 – Regression(2.057): Invalid downcast permitted with derived/aliased template classes

D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 7375 - Regression(2.057): Invalid downcast permitted with derived/aliased template classes
Summary: Regression(2.057): Invalid downcast permitted with derived/aliased template c...
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 regression
Assignee: No Owner
URL:
Keywords: wrong-code
Depends on:
Blocks:
Reported: 2012年01月27日 00:22 UTC by Nick Sabalausky
Modified: 2012年07月08日 14:03 UTC (History)
3 users (show)

See Also:


Attachments
Add an attachment (proposed patch, testcase, etc.)

Note You need to log in before you can comment on or make changes to this issue.
Description Nick Sabalausky 2012年01月27日 00:22:38 UTC
a.d:
----------------
import b;
void main(string[] args)
{
	auto foo = getEleven();
	//auto dummy = cast(Derived!11)foo;
	assert(cast(Derived!22)foo is null);
}
alias DerivedAlias!22 X;
----------------
b.d:
----------------
module b;
Base getEleven()
{
	Base foo = new MoreDerived!11(null);
	return foo;
}
class Base
{
	Base a;
	this(Base a)
	{
		this.a = a;
	}
}
class Derived(int i) : Base
{
	this(Base a)
	{
		super(a);
	}
}
class MoreDerived(int i) : Derived!i
{
	this(Base a)
	{
		super(a);
	}
}
template DerivedAlias(int i)
{
	alias Derived!i DerivedAlias;
}
----------------
On 2.057, the assert fails. (It's fine on 2.056.)
If you uncomment the "dummy" line, the problem goes away (merely creating an alias for Derived!11 does not work). However, that's *not* a general workaround because in order to test what the runtime type is, you have to *already* know *all* the types it might be and attempt to downcast to each of them.
Comment 1 Nick Sabalausky 2012年01月27日 00:26:47 UTC
Sorry, I forgot to finish minimizing "b.d". The properly minimized "b.d" is:
--------------------
module b;
Base getEleven()
{
	Base foo = new MoreDerived!11();
	return foo;
}
class Base {}
class Derived(int i) : Base {}
class MoreDerived(int i) : Derived!i {}
template DerivedAlias(int i)
{
	alias Derived!i DerivedAlias;
}
--------------------
Comment 2 Walter Bright 2012年01月29日 22:16:19 UTC
A one file version:
-------------------
class A {}
class B(int i) : A {}
class C(int i) : B!i {}
template DerivedAlias(int i)
{
 alias B!i DerivedAlias;
}
alias DerivedAlias!22 X;
void main(string[] args)
{
 A foo = new C!11();
 //auto dummy = cast(B!11)foo;
 assert(cast(B!22)foo is null);
}
Comment 3 Walter Bright 2012年01月30日 01:20:02 UTC
The failure definitely happened between 2.056 and 2.057. Anyone care to run the git binary diff thing?
Comment 4 Walter Bright 2012年01月30日 02:02:19 UTC
Auch, I found it. In druntime/src/rt/cast_.d, the addition of a name compare saying the classes are the same in _d_isbaseof and _d_isbaseof2.
Not sure what the right fix is.
Comment 6 yebblies 2012年01月30日 08:14:09 UTC
I suspected is was that commit. (I remember seeing it when it was committed) Sorry I didn't get around to looking into it earlier.
I think the solution is to do a compare on the full mangled name of the class, but I can't see anywhere it's exposed in the classinfo so it would need to be added.
This should probably be delayed until after the release and the commit reverted.
Comment 7 Martin Nowak 2012年01月30日 09:39:05 UTC
This was introduced by pull #92
https://github.com/D-Programming-Language/druntime/commits/29f33bfdc0d36484e77ae8f369656720319f22e3.
This added classinfo.name comparison while searching base classes.
Comment 8 Martin Nowak 2012年01月30日 10:01:25 UTC
https://github.com/D-Programming-Language/druntime/pull/142 
Comment 9 github-bugzilla 2012年01月30日 11:12:29 UTC
Commits pushed to master at https://github.com/D-Programming-Language/dmd
https://github.com/D-Programming-Language/dmd/commit/3a6c3629f3185c2d65354d1f04c500eb737d49d9
fix Issue 7375 - Regression(2.057): Invalid downcast permitted with derived/aliased template classes
https://github.com/D-Programming-Language/dmd/commit/667ff95c935bc1cf743517b69d2dc421e43f9a51
fix Issue 7375 - Regression(2.057): Invalid downcast permitted with derived/aliased template classes
Comment 10 github-bugzilla 2012年01月30日 11:12:36 UTC
Commit pushed to dmd-1.x at https://github.com/D-Programming-Language/dmd
https://github.com/D-Programming-Language/dmd/commit/06a33d14f9ad137d86309d40180ca0e60f4edb74
fix Issue 7375 - Regression(2.057): Invalid downcast permitted with derived/aliased template classes
Comment 11 Walter Bright 2012年01月30日 11:17:53 UTC
I fixed this in the compiler, not the runtime.
Comment 12 github-bugzilla 2012年07月08日 14:03:31 UTC
Commit pushed to master at https://github.com/D-Programming-Language/druntime
https://github.com/D-Programming-Language/druntime/commit/b7bdcdce294729e48b56fa5d8feca5aa9a8cc253
Revert "Merge pull request #92 from shoo/fix7020"
This reverts commit 29f33bfdc0d36484e77ae8f369656720319f22e3, reversing
changes made to f11dd3e56476f75804809ecc14da2e65de3f335e.
fixes Issue 7375 


AltStyle によって変換されたページ (->オリジナル) /