Crash in out contract in interface
Timon Gehr
timon.gehr at gmx.ch
Wed Aug 24 06:58:01 PDT 2011
On 08/24/2011 03:35 PM, Timon Gehr wrote:
> On 08/24/2011 03:13 PM, Alex Rønne Petersen wrote:
>> Hi,
>>
>> This is an odd one. Consider the following code:
>>
>>> interface I
>>> {
>>> void foo()
>>> out { assert(bar()); }
>>> bool bar();
>>> }
>>>
>>> class C : I
>>> {
>>> void foo() {}
>>> bool bar() { return true; }
>>> }
>>>
>>> void main()
>>> {
>>> C c = new C();
>>> c.foo();
>>> }
>>
>> This will crash and burn when run:
>>
>>> object.Error: Access Violation
>>> ----------------
>>> 40DB64
>>> 40D9DB
>>> 402050
>>> 402074
>>> 4026F3
>>> 4022EF
>>> 4127B5
>>> ----------------
>>
>> I assume this is a bug, but has anyone encountered this before or have
>> any idea why exactly it happens when calling other interface methods in
>> an interface method's out contract? (It seems to work fine in the in
>> contract.)
>>
>> - Alex
>
> I think it actually makes a lot of sense. I.foo's out contract assumes
> an I this reference, but C.foo passes a C this reference without
> adjustment. What I don't quite get yet is what is the matter with in
> contracts on interface member functions. Not only do they not crash,
> apparently they are not even executed.
>
> Have you already filed a bug report?
>
>
>
>
>
This seems to confirm I was on the right path:
import std.stdio;
interface I
{
void foo()
out { writeln(typeid(this));}
bool bar();
final void qux(){ writeln(typeid(this)); }
}
class C : I
{
void foo() { }
bool bar() { return false; }
}
void main()
{
C c = new C();
c.qux(); // "modulename.I"
c.foo(); // "TypeInfo_Class", not sure why it is not modulename.C.
}
So, basically interfaces are the only place in D?/DMD where you can even
specify contracts without a function body and there they don't work. I
think the bug is not the only problem, actual contracts should /always/
be part of the function declaration and not require an implementation to
work. Everything else degrades contracts to simple syntactic sugar for
assertions.
More information about the Digitalmars-d
mailing list