Skip to content

Commit 060035d

Browse files
nsajkofatteneder
andauthored
constrain the path argument of include functions to AbstractString (#55466)
Each `Module` defined with `module` automatically gets an `include` function with two methods. Each of those two methods takes a file path as its last argument. Even though the path argument is unconstrained by dispatch, it's documented as constrained with `::AbstractString`: https://docs.julialang.org/en/v1.11-dev/base/base/#include Furthermore, I think that any invocation of `include` with a non-`AbstractString` path will necessarily throw a `MethodError` eventually. Thus this change should be harmless. Adding the type constraint to the path argument is an improvement because any possible exception would be thrown earlier than before. Apart from modules defined with `module`, the same issue is present with the anonymous modules created by `evalfile`, which is also addressed. Sidenote: `evalfile` seems to be completely untested apart from the test added here. Co-authored-by: Florian <[email protected]>
1 parent a06a801 commit 060035d

File tree

4 files changed

+16
-4
lines changed

4 files changed

+16
-4
lines changed

base/loading.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2773,8 +2773,8 @@ function evalfile(path::AbstractString, args::Vector{String}=String[])
27732773
Expr(:toplevel,
27742774
:(const ARGS = $args),
27752775
:(eval(x) = $(Expr(:core, :eval))(__anon__, x)),
2776-
:(include(x) = $(Expr(:top, :include))(__anon__, x)),
2777-
:(include(mapexpr::Function, x) = $(Expr(:top, :include))(mapexpr, __anon__, x)),
2776+
:(include(x::AbstractString) = $(Expr(:top, :include))(__anon__, x)),
2777+
:(include(mapexpr::Function, x::AbstractString) = $(Expr(:top, :include))(mapexpr, __anon__, x)),
27782778
:(include($path))))
27792779
end
27802780
evalfile(path::AbstractString, args::Vector) = evalfile(path, String[args...])

src/jlfrontend.scm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,11 +211,11 @@
211211
(block
212212
,@loc
213213
(call (core eval) ,name ,x)))
214-
(= (call include ,x)
214+
(= (call include (:: ,x (top AbstractString)))
215215
(block
216216
,@loc
217217
(call (core _call_latest) (top include) ,name ,x)))
218-
(= (call include (:: ,mex (top Function)) ,x)
218+
(= (call include (:: ,mex (top Function)) (:: ,x (top AbstractString)))
219219
(block
220220
,@loc
221221
(call (core _call_latest) (top include) ,mex ,name ,x)))))

test/loading.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,17 @@ import .Foo28190.Libdl; import Libdl
793793
end
794794
end
795795

796+
@testset "`::AbstractString` constraint on the path argument to `include`" begin
797+
for m (NotPkgModule, evalfile("testhelpers/just_module.jl"))
798+
let i = m.include
799+
@test !applicable(i, (nothing,))
800+
@test !applicable(i, (identity, nothing,))
801+
@test !hasmethod(i, Tuple{Nothing})
802+
@test !hasmethod(i, Tuple{Function,Nothing})
803+
end
804+
end
805+
end
806+
796807
@testset "`Base.project_names` and friends" begin
797808
# Some functions in Pkg assumes that these tuples have the same length
798809
n = length(Base.project_names)

test/testhelpers/just_module.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@__MODULE__

0 commit comments

Comments
 (0)