nitlang / nit

Nit language
http://nitlanguage.org
Apache License 2.0
238 stars 64 forks source link

phase: Introduction of new hook method #2828

Closed Delja closed 4 years ago

Delja commented 4 years ago

This pr introduces a new hook method in the execution of the process_nmodule_after_phases phases. This method makes it possible to analyze each module again after the execution of all phases in order to benefit from the information of the whole program.

This method is useful in the context of contracts. Example:

module b

redef class A
    redef fun foo
    is
        ensure(result > 2)
    do
        return 2
    end 
end

var a = new A
a.bar
module a

class A
    fun foo: Int do
        return 1
    end

    fun bar do
        self.foo
    end
end

Actually, when you call the foo property in the bar method, no contract is called, while a postcondition was added by refinement in the b module. In fact it was not possible to call the contract face because it was not yet introduced during the analysis of module a. It is now possible to re-analyze all the modules to make the redirection once all the information are known.

Delja commented 4 years ago

I used this method first, but there are two drawbacks:

I did the modifications, it seems good to me, depending on the tests result we will adapt.