Wiki

Ticket #243 (new defect)

Opened 18 months ago

Last modified 18 months ago

Invariants called too many times and in wrong order

Reported by: nevdelap Owned by:
Priority: major Milestone:
Component: Cobra Compiler Version: 0.8.0
Keywords: Cc:

Description

class A
	cue init
		base.init
		print 'inside init of A'

	def m1 as bool
		print 'inside invariant of A'
		return true

	invariant
		.m1

class B inherits A

	cue init
		base.init
		print 'inside init of B'

	def m2 as bool
		print 'inside invariant of B'
		return true

	invariant
		.m2

class P
	def main is shared
		B()

Actual result

inside init of A         (1)
inside invariant of A    (2)
inside init of B
inside invariant of A
inside invariant of A    (3)
inside invariant of B

(1) At the end of the base class constructor the object is not yet constructed and it's invariant need not hold and shouldn't be called.
(2) Additionally if the invariant is called at that point and includes calls to any methods overridden in the derived class they can fail because it hasn't constructed yet and its state is not ready to be used.
(3) Extra test of A's invariant just not needed. I guess it's because I've used calls to methods to show when it's in the invariants, but again, the object is not finished constructing so it need not call the invariant on the exit of that method. (It's finished constructing after the invariant check at the end of the most derived init, not before, so at any time before that, method calls, or whatever used in the init, the invariant shouldn't be called yet.)

Expected result

inside init of A
inside init of B
inside invariant of A
inside invariant of B

BTW: you can hit me up in chat to discuss, instead of us bouncing replies back and forth here. More productive, yeah? Cheers.

nevdelap(at)gmail(dot)com for *both* gmail chat & MSN, or nevdelap in yahoo.

Change History

Changed 18 months ago by nevdelap

(4) Use of methods or properties in the init is also very problematic, with or without inheritance, because the invariant is called prematurely in those cases too. The workaround is to comment out parts of the invariant.

Note: See TracTickets for help on using tickets.