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

Doc system cannot document first expression generated by macro #12705

Closed
mauro3 opened this issue Aug 19, 2015 · 7 comments
Closed

Doc system cannot document first expression generated by macro #12705

mauro3 opened this issue Aug 19, 2015 · 7 comments
Assignees
Labels
docsystem The documentation building system

Comments

@mauro3
Copy link
Contributor

mauro3 commented Aug 19, 2015

I'm pretty sure this used to work:

julia> macro mm()
           esc(quote
               g(x) = 4
               f(x) = 3
               end
               )
       end

julia> "d" @mm
ERROR: Invalid doc expression begin  # none, line 3:
    g(x) = begin  # none, line 3:
            4
        end # line 4:
    f(x) = begin  # none, line 4:
            3
        end
end

(@with_kw generates a type and the a bunch of other stuff)
I would expect that "d" gets attached to g, i.e. the first expression generated by the macro. I think this used to work. At least below used to work with an about 8 days old master (this is how I got to this) but now errors with the same error:

using Parameters

"d"
@with_kw immutable MT1
    r::Int = 4
    c = "sdaf"
end
@MichaelHatherly
Copy link
Member

I think this used to work. At least below used to work with an about 8 days old master

@doc would probably have been documenting the last expression in the block if it was encountering a Expr(:block, ...).

I guess we could go with the behaviour of attaching the doc to the first expression in a block. There's some related discussion here about this kind of thing.

Alternatively you could allow your macro to take a string as an argument and then use @doc internally to attach the documentation to the exact object you want:

@with_kw """
...
""" immutable MT1
    r::Int = 4
    c = "sdaf"
end

though it's not quite as elegant.

@kshyatt kshyatt added the docsystem The documentation building system label Aug 20, 2015
@mauro3
Copy link
Contributor Author

mauro3 commented Aug 20, 2015

Yes you're right, what I should have written: it used to work in the sense that no error was thrown. I didn't actually check to which expression the doc was attached.

Your alternative suggestion would mean that all macro writers would also have to deal with documentation, not ideal. I think attaching to the first bit would be the best/logical thing to do.

@MichaelHatherly
Copy link
Member

I think attaching to the first bit would be the best/logical thing to do.

Would that cover all macros? Are there times when some setup code is needed prior to the main definition? ie.

macro foo(...)
    quote
        # some setup code
        # ...
        $(def)
    end
end

@one-more-minute's Expr(:meta, :doc) idea would probably be the most general in that case.

@mauro3
Copy link
Contributor Author

mauro3 commented Aug 20, 2015

It would cover all macros I made which I would like to use documentation with (a grand total of 3...).

Linked in the issue you linked above I found where I discussed this before (I couldn't find it anymore): #12000 (comment). With that PR it was possible to document the return value of a block, which doesn't work anymore:

julia> "asdf" begin
       x=1
       f(t) = 5
       end

ERROR: Invalid doc expression begin  # none, line 2:
    x = 1 # line 3:
    f(t) = begin  # none, line 3:
            5
        end
end

although single statement blocks work:

julia> "asdf" begin
       x=1
       end
  asdf

So how about, instead documenting the return value of a block/macro expansion, as was possible (I think) after PR #12000 . That would work for me. It would be a bit fiddly with methods but that is ok (for now).

@MichaelHatherly
Copy link
Member

although single statement blocks work:

That's due to unblock which is used in docm.

instead documenting the return value of a block/macro expansion

That wouldn't allow for documenting methods, types, or constants correctly I think, since if the convention was to document the value of the block then, for example:

julia> macro t()
           quote
               # ...
               type T
               end
           end
       end

julia> typeof(@t())
Void

julia> macro f()
           quote
               # ...
               f() = ()
           end
       end

julia> typeof(@f())
Function

julia> macro c()
           quote
               # ...
               const c = ()
           end
       end

julia> typeof(@c())
Tuple{}

@doc would just be getting the value of the block, which has lost some information about what's actually being documented.

@mauro3
Copy link
Contributor Author

mauro3 commented Aug 20, 2015

If documenting the return value, one would have to be explicit to return what needs to be documented:

macro t()
    quote
        # ...
        type T
        end
        T
    end
end

which would work fine for everything except methods. I would be fine with this.

@MichaelHatherly
Copy link
Member

explicit to return what needs to be documented

Yeah, that would work. @one-more-minute are you still against documenting arbitrary blocks? Or we could try to get the Expr(:meta, :doc) idea working. I put together a quick prototype this morning. Seems to work alright.

@MikeInnes MikeInnes self-assigned this Aug 20, 2015
mauro3 added a commit to mauro3/Parameters.jl that referenced this issue Sep 2, 2015
Also, a test is in place to be activated after fix of
JuliaLang/julia#12705

[ci skip]
mauro3 added a commit to mauro3/Parameters.jl that referenced this issue Sep 2, 2015
Also, a test is in place to be activated after fix of
JuliaLang/julia#12705

[ci skip]
MichaelHatherly added a commit to MichaelHatherly/julia that referenced this issue Sep 7, 2015
This adds a `@__doc__` macro, not exported currently, for use by macro authors
that what their macros to hook into the docsystem properly.

Usage:

    macro example(f)
        quote
            $(f)() = 0
            @__doc__ $(f)(x) = 1
            $(f)(x, y) = 2
        end |> esc
    end

    "Docs for `g(x)` only."
    @example g

will attach the docstring to the 1-arg method only.

Fixes JuliaLang#12705.
MichaelHatherly added a commit to MichaelHatherly/julia that referenced this issue Sep 8, 2015
This adds a `@__doc__` macro, not exported currently, for use by macro authors
that want their macros to hook into the docsystem properly.

Usage:

    macro example(f)
        quote
            $(f)() = 0
            @__doc__ $(f)(x) = 1
            $(f)(x, y) = 2
        end |> esc
    end

    "Docs for `g(x)` only."
    @example g

will attach the docstring to the 1-arg method only.

Fixes JuliaLang#12705.
MichaelHatherly added a commit to MichaelHatherly/julia that referenced this issue Sep 9, 2015
This adds a `@__doc__` macro, not exported currently, for use by macro authors
that want their macros to hook into the docsystem properly.

Usage:

    macro example(f)
        quote
            $(f)() = 0
            @__doc__ $(f)(x) = 1
            $(f)(x, y) = 2
        end |> esc
    end

    "Docs for `g(x)` only."
    @example g

will attach the docstring to the 1-arg method only.

Fixes JuliaLang#12705.
MichaelHatherly added a commit to MichaelHatherly/julia that referenced this issue Sep 10, 2015
This adds a `@__doc__` macro, not exported currently, for use by macro authors
that want their macros to hook into the docsystem properly.

Usage:

    macro example(f)
        quote
            $(f)() = 0
            @__doc__ $(f)(x) = 1
            $(f)(x, y) = 2
        end |> esc
    end

    "Docs for `g(x)` only."
    @example g

will attach the docstring to the 1-arg method only.

Fixes JuliaLang#12705.
MichaelHatherly added a commit to MichaelHatherly/julia that referenced this issue Sep 15, 2015
This adds a `@__doc__` macro, not exported currently, for use by macro authors
that want their macros to hook into the docsystem properly.

Usage:

    macro example(f)
        quote
            $(f)() = 0
            @__doc__ $(f)(x) = 1
            $(f)(x, y) = 2
        end |> esc
    end

    "Docs for `g(x)` only."
    @example g

will attach the docstring to the 1-arg method only.

Also improve invalid doc expression error message by displaying the
unexpanded form of the expression instead of its expansion.

Enable docstrings for `at-enum` using the newly added macro.

Fixes JuliaLang#12705.
tkelman pushed a commit that referenced this issue Sep 30, 2015
This adds a `@__doc__` macro, not exported currently, for use by macro authors
that want their macros to hook into the docsystem properly.

Usage:

    macro example(f)
        quote
            $(f)() = 0
            @__doc__ $(f)(x) = 1
            $(f)(x, y) = 2
        end |> esc
    end

    "Docs for `g(x)` only."
    @example g

will attach the docstring to the 1-arg method only.

Also improve invalid doc expression error message by displaying the
unexpanded form of the expression instead of its expansion.

Enable docstrings for `at-enum` using the newly added macro.

Fixes #12705.

(cherry picked from commit 0b9ef7d)
ref #13006
skumagai pushed a commit to skumagai/julia that referenced this issue Oct 9, 2015
This adds a `@__doc__` macro, not exported currently, for use by macro authors
that want their macros to hook into the docsystem properly.

Usage:

    macro example(f)
        quote
            $(f)() = 0
            @__doc__ $(f)(x) = 1
            $(f)(x, y) = 2
        end |> esc
    end

    "Docs for `g(x)` only."
    @example g

will attach the docstring to the 1-arg method only.

Also improve invalid doc expression error message by displaying the
unexpanded form of the expression instead of its expansion.

Enable docstrings for `at-enum` using the newly added macro.

Fixes JuliaLang#12705.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docsystem The documentation building system
Projects
None yet
Development

No branches or pull requests

4 participants