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

fix #36104, hack to allow accessing incomplete struct via dot syntax #36111

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions base/boot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,14 @@ Unsigned(x::Union{Float32, Float64, Bool}) = UInt(x)
Integer(x::Integer) = x
Integer(x::Union{Float32, Float64}) = Int(x)

# During definition of struct type `B`, if an `A.B` expression refers to
# the eventual global name of the struct, then return the partially-initialized
# type object.
# TODO: remove. This is a shim for backwards compatibility.
function struct_name_shim(@nospecialize(x), name::Symbol, mod::Module, @nospecialize(t))
return x === mod ? t : getfield(x, name)
end

# Binding for the julia parser, called as
#
# Core._parse(text, filename, offset, options)
Expand Down
17 changes: 16 additions & 1 deletion src/julia-syntax.scm
Original file line number Diff line number Diff line change
Expand Up @@ -903,7 +903,22 @@
(call (core svec) ,@(map quotify field-names))
,mut ,min-initialized))
(call (core _setsuper!) ,name ,super)
(call (core _typebody!) ,name (call (core svec) ,@field-types))
(call (core _typebody!) ,name
(call (core svec)
;; Replace A.B expressions with references to the local new struct `B`
;; if A refers to the enclosing module. TODO: Remove. This is a
;; compatibility shim (issue #36104).
,@(map (lambda (x)
(expr-replace (lambda (y)
(and (length= y 3) (eq? (car y) '|.|)
(or (equal? (caddr y) `(quote ,name))
(equal? (caddr y) `(inert ,name)))))
x
(lambda (y)
`(call (core struct_name_shim)
,(cadr y) ,(caddr y)
(thismodule) ,name))))
field-types)))
(if (&& (isdefined (outerref ,name))
(call (core _equiv_typedef) (outerref ,name) ,name))
(null)
Expand Down
7 changes: 7 additions & 0 deletions src/utils.scm
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@
(any (lambda (y) (expr-contains-p p y filt))
(cdr expr))))))

(define (expr-replace p expr repl)
(cond ((p expr) (repl expr))
((and (pair? expr) (not (quoted? expr)))
(cons (car expr)
(map (lambda (x) (expr-replace p x repl)) (cdr expr))))
(else expr)))

;; find all subexprs satisfying `p`, applying `key` to each one
(define (expr-find-all p expr key (filt (lambda (x) #t)))
(if (filt expr)
Expand Down
8 changes: 8 additions & 0 deletions test/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7221,3 +7221,11 @@ struct AVL35416{K,V}
avl:: Union{Nothing,Node35416{AVL35416{K,V},<:K,<:V}}
end
@test AVL35416(Node35416{AVL35416{Integer,AbstractString},Int,String}()) isa AVL35416{Integer,AbstractString}

# issue #36104
module M36104
struct T36104
v::Vector{M36104.T36104}
end
end
@test fieldtypes(M36104.T36104) == (Vector{M36104.T36104},)