Skip to content

Commit

Permalink
add error for inconsistent public/export declarations (JuliaLang#53664)
Browse files Browse the repository at this point in the history
Co-authored-by: Ian Butterworth <[email protected]>
Co-authored-by: Lilith Orion Hafner <[email protected]>
3 people authored Mar 10, 2024
1 parent 5c7d244 commit 30450c3
Showing 4 changed files with 24 additions and 8 deletions.
9 changes: 6 additions & 3 deletions base/docs/basedocs.jl
Original file line number Diff line number Diff line change
@@ -64,9 +64,12 @@ kw"export"
public
`public` is used within modules to tell Julia which names are part of the
public API of the module . For example: `public foo` indicates that the name
`foo` is public, without making it available when [`using`](@ref)
the module. See the [manual section about modules](@ref modules) for details.
public API of the module. For example: `public foo` indicates that the name
`foo` is public, without making it available when [`using`](@ref) the module.
As [`export`](@ref) already indicates that a name is public, it is
unnecessary and an error to declare a name both as `public` and as `export`ed.
See the [manual section about modules](@ref modules) for details.
!!! compat "Julia 1.11"
The public keyword was added in Julia 1.11. Prior to this the notion
9 changes: 9 additions & 0 deletions src/module.c
Original file line number Diff line number Diff line change
@@ -665,6 +665,15 @@ JL_DLLEXPORT void jl_module_using(jl_module_t *to, jl_module_t *from)
JL_DLLEXPORT void jl_module_public(jl_module_t *from, jl_sym_t *s, int exported)
{
jl_binding_t *b = jl_get_module_binding(from, s, 1);
if (b->publicp) {
// check for conflicting declarations
if (b->exportp && !exported)
jl_errorf("cannot declare %s.%s public; it is already declared exported",
jl_symbol_name(from->name), jl_symbol_name(s));
if (!b->exportp && exported)
jl_errorf("cannot declare %s.%s exported; it is already declared public",
jl_symbol_name(from->name), jl_symbol_name(s));
}
b->publicp = 1;
b->exportp |= exported;
}
6 changes: 2 additions & 4 deletions stdlib/Profile/src/Profile.jl
Original file line number Diff line number Diff line change
@@ -23,8 +23,8 @@ Profiling support.
module Profile

global print
public @profile,
clear,
export @profile
public clear,
print,
fetch,
retrieve,
@@ -44,8 +44,6 @@ const nmeta = 4 # number of metadata fields per block (threadid, taskid, cpu_cyc
# deprecated functions: use `getdict` instead
lookup(ip::UInt) = lookup(convert(Ptr{Cvoid}, ip))

export @profile

"""
@profile
8 changes: 7 additions & 1 deletion test/reflection.jl
Original file line number Diff line number Diff line change
@@ -1185,14 +1185,20 @@ end
# marking a symbol as public should not "unexport" it
# https://github.com/JuliaLang/julia/issues/52812
module Mod52812
using Test
export a, b
public a
@test_throws ErrorException eval(Expr(:public, :a))
public c
@test_throws ErrorException eval(Expr(:export, :c))
export b
public c
end

@test Base.isexported(Mod52812, :a)
@test Base.isexported(Mod52812, :b)
@test Base.ispublic(Mod52812, :a)
@test Base.ispublic(Mod52812, :b)
@test Base.ispublic(Mod52812, :c) && !Base.isexported(Mod52812, :c)

@test Base.infer_return_type(code_lowered, (Any,)) == Vector{Core.CodeInfo}
@test Base.infer_return_type(code_lowered, (Any,Any)) == Vector{Core.CodeInfo}

0 comments on commit 30450c3

Please sign in to comment.