Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

method overwrite warning #36565

Closed
wants to merge 1 commit into from
Closed

method overwrite warning #36565

wants to merge 1 commit into from

Conversation

daviehh
Copy link
Contributor

@daviehh daviehh commented Jul 7, 2020

Re: #15602 and #36037 (possibly others)

Currently, expressions like

function myfun(n::Int)
     if n < 3
        println("branch 1")
        doit() = 3
     else
        println("branch 1")
        doit() = 5
     end 
end

and

function do_test()
    function innerfunc()
        println(stderr, "innerfunc")
    end
    println(stderr, "pre-innerfunc")
    innerfunc()
    println(stderr, "post-innerfunc")

    # Overwrite innerfunc
    innerfunc() = nothing
    println("innerfunc is now overwritten")
end

do_test()

would "silently" overwrite the function, such that e.g. with the 1st example, a = myfun(1) would print branch 1 but a() returns 5 since method is silently overwritten, making such scenarios a bit difficult to debug.

A long-term solution could be to error in such cases, meanwhile, this PR set the default setting to warn about such overwrites, and add a function to flip the warning setting (and a convenience macro) to temporarily disable the warning when such overwrite is intentional.

Example:

julia> f(x) = 1
 f (generic function with 1 method)
julia> f(x) = x
WARNING: Method definition f(Any) in module Main at REPL[1]:1 overwritten at REPL[2]:1.
 f (generic function with 1 method)
julia> Base.@overwrite f(x) = x * 3
 f (generic function with 1 method)
julia> f(1)
 3

@KristofferC
Copy link
Sponsor Member

KristofferC commented Jul 7, 2020

The whole reason this setting was introduced was to not show them by default (#23002).

Call site overriding was thumbed down #23002 (comment).

@mbauman
Copy link
Sponsor Member

mbauman commented Jul 7, 2020

I would much rather this be done in the lowering for local functions only.

@daviehh
Copy link
Contributor Author

daviehh commented Jul 9, 2020

got it, thanks! closing for now

@daviehh daviehh closed this Jul 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants