@@ -2857,7 +2857,7 @@ function is_invalid_func_name(ex)
28572857 return is_ccall_or_cglobal (name)
28582858end
28592859
2860- function expand_function_def (ctx, ex, docs, rewrite_call= identity, rewrite_body= identity)
2860+ function expand_function_def (ctx, ex, docs, rewrite_call= identity, rewrite_body= identity; doc_only = false )
28612861 @chk numchildren (ex) in (1 ,2 )
28622862 name = ex[1 ]
28632863 if numchildren (ex) == 1 && is_identifier_like (name)
@@ -3023,6 +3023,35 @@ function expand_function_def(ctx, ex, docs, rewrite_call=identity, rewrite_body=
30233023 end
30243024 end
30253025
3026+ if doc_only
3027+ # The (doc str (call ...)) form requires method signature lowering, but
3028+ # does not execute or define any method, so we can't use function_type.
3029+ # This is a bit of a messy case in the docsystem which we'll hopefully
3030+ # be able to delete at some point.
3031+ sig_stmts = SyntaxList (ctx)
3032+ @assert first_default != 1 && length (arg_types) >= 1
3033+ last_required = first_default === 0 ? length (arg_types) : first_default - 1
3034+ for i in last_required: length (arg_types)
3035+ push! (sig_stmts, @ast (ctx, ex, [K " curly" " Tuple" :: K"core" arg_types[2 : i]. .. ]))
3036+ end
3037+ sig_type = @ast ctx ex [K " where"
3038+ [K " curly" " Union" :: K"core" sig_stmts... ]
3039+ [K " _typevars" [K " block" typevar_names... ] [K " block" ]]
3040+ ]
3041+ out = @ast ctx docs [K " block"
3042+ typevar_stmts...
3043+ [K " call"
3044+ bind_static_docs!:: K"Value"
3045+ (kind (name) == K " ." ? name[1 ] : ctx. mod:: K"Value" )
3046+ name_str:: K"Symbol"
3047+ docs[1 ]
3048+ :: K"SourceLocation" (ex)
3049+ sig_type
3050+ ]
3051+ ]
3052+ return expand_forms_2 (ctx, out)
3053+ end
3054+
30263055 if ! isnothing (return_type)
30273056 ret_var = ssavar (ctx, return_type, " return_type" )
30283057 push! (body_stmts, @ast ctx return_type [K " =" ret_var return_type])
@@ -3083,10 +3112,11 @@ function expand_function_def(ctx, ex, docs, rewrite_call=identity, rewrite_body=
30833112 push! (method_stmts,
30843113 method_def_expr (ctx, ex, callex, method_table, main_typevar_names, arg_names,
30853114 arg_types, body, ret_var))
3115+
30863116 if ! isnothing (docs)
30873117 method_stmts[end ] = @ast ctx docs [K " block"
30883118 method_metadata := method_stmts[end ]
3089- @ast ctx docs [K " call"
3119+ [K " call"
30903120 bind_docs!:: K"Value"
30913121 doc_obj
30923122 docs[1 ]
@@ -4273,6 +4303,27 @@ function expand_module(ctx, ex::SyntaxTree)
42734303 ]
42744304end
42754305
4306+ # -------------------------------------------------------------------------------
4307+ # Expand docstring-annotated expressions
4308+
4309+ function expand_doc (ctx, ex, docex, mod= ctx. mod)
4310+ if kind (ex) in (K " Identifier" , K " ." )
4311+ expand_forms_2 (ctx, @ast ctx docex [K " call"
4312+ bind_static_docs!:: K"Value"
4313+ (kind (ex) === K " ." ? ex[1 ] : ctx. mod:: K"Value" )
4314+ (kind (ex) === K " ." ? ex[2 ] : ex). name_val:: K"Symbol"
4315+ docex[1 ]
4316+ :: K"SourceLocation" (ex)
4317+ Union{}:: K"Value"
4318+ ])
4319+ elseif is_eventually_call (ex)
4320+ expand_function_def (ctx, @ast (ctx, ex, [K " function" ex [K " block" ]]),
4321+ docex; doc_only= true )
4322+ else
4323+ expand_forms_2 (ctx, ex, docex)
4324+ end
4325+ end
4326+
42764327# -------------------------------------------------------------------------------
42774328# Desugaring's "big switch": expansion of some simple forms; dispatch to other
42784329# expansion functions for the rest.
@@ -4339,7 +4390,7 @@ function expand_forms_2(ctx::DesugaringContext, ex::SyntaxTree, docs=nothing)
43394390 expand_forms_2 (ctx, expand_compare_chain (ctx, ex))
43404391 elseif k == K " doc"
43414392 @chk numchildren (ex) == 2
4342- sig = expand_forms_2 (ctx, ex[2 ], ex)
4393+ expand_doc (ctx, ex[2 ], ex)
43434394 elseif k == K " for"
43444395 expand_forms_2 (ctx, expand_for (ctx, ex))
43454396 elseif k == K " comprehension"
0 commit comments