Skip to content

Commit 6452535

Browse files
committed
rename expression heads for type definitions to match keywords
1 parent ae17198 commit 6452535

15 files changed

+101
-82
lines changed

NEWS.md

+11
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,17 @@ Language changes
3535
* Nested `if` expressions that arise from the keyword `elseif` now use `elseif`
3636
as their expression head instead of `if` ([#21774]).
3737

38+
* Parsed and lowered forms of type definitions have been synchronized with their
39+
new keywords ([#23157]). Expression heads are renamed as follows:
40+
41+
+ `type` => `struct`
42+
43+
+ `bitstype` => `primitive` (order of arguments is also reversed, to match syntax)
44+
45+
+ `composite_type` => `struct_type`
46+
47+
+ `bits_type` => `primitive_type`
48+
3849
Breaking changes
3950
----------------
4051

base/docs/Docs.jl

+6-6
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ function nameof(x::Expr, ismacro)
463463
if isexpr(x, :.)
464464
ismacro ? macroname(x) : x
465465
else
466-
n = isexpr(x, (:module, :type, :bitstype)) ? 2 : 1
466+
n = isexpr(x, (:module, :struct)) ? 2 : 1
467467
nameof(x.args[n], ismacro)
468468
end
469469
end
@@ -505,7 +505,7 @@ function metadata(__source__, __module__, expr, ismodule)
505505
else
506506
push!(args, Pair(:module, __module__))
507507
end
508-
if isexpr(expr, :type)
508+
if isexpr(expr, :struct)
509509
# Field docs for concrete types.
510510
fields = []
511511
tmp = nothing
@@ -685,11 +685,11 @@ function docm(source::LineNumberNode, mod::Module, meta, ex, define = true)
685685

686686
# Type definitions.
687687
#
688-
# type T ... end
689-
# abstract T
690-
# bitstype N T
688+
# struct T ... end
689+
# abstract type T end
690+
# primitive type T N end
691691
#
692-
isexpr(x, [:type, :abstract, :bitstype]) ? objectdoc(source, mod, meta, def, x) :
692+
isexpr(x, [:struct, :abstract, :primitive]) ? objectdoc(source, mod, meta, def, x) :
693693

694694
# "Bindings". Names that resolve to objects with different names, ie.
695695
#

base/show.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -901,13 +901,13 @@ function show_unquoted(io::IO, ex::Expr, indent::Int, prec::Int)
901901
print(io, "end")
902902

903903
# type declaration
904-
elseif head === :type && nargs==3
904+
elseif head === :struct && nargs==3
905905
show_block(io, args[1] ? Symbol("mutable struct") : Symbol("struct"), args[2], args[3], indent)
906906
print(io, "end")
907907

908-
elseif head === :bitstype && nargs == 2
908+
elseif head === :primitive && nargs == 2
909909
print(io, "primitive type ")
910-
show_list(io, reverse(args), ' ', indent)
910+
show_list(io, args, ' ', indent)
911911
print(io, " end")
912912

913913
elseif head === :abstract && nargs == 1

doc/src/devdocs/ast.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -516,8 +516,8 @@ end
516516
parses as:
517517

518518
```
519-
(type true (curly Foo (<: T S))
520-
(block (line 2) (:: x T)))
519+
(struct true (curly Foo (<: T S))
520+
(block (line 2) (:: x T)))
521521
```
522522

523523
The first argument is a boolean telling whether the type is mutable.

doc/src/devdocs/functions.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ The front end generates type declarations for all closures. Initially, this was
243243
generating normal type declarations. However, this produced an extremely large number of constructors,
244244
all of which were trivial (simply passing all arguments through to `new`). Since methods are partially
245245
ordered, inserting all of these methods is O(n^2), plus there are just too many of them to keep
246-
around. This was optimized by generating `composite_type` expressions directly (bypassing default
246+
around. This was optimized by generating `struct_type` expressions directly (bypassing default
247247
constructor generation), and using `new` directly to create closure instances. Not the prettiest
248248
thing ever, but you do what you gotta do.
249249

doc/src/devdocs/init.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ type description objects for the [built-in types defined in `julia.h`](https://g
4040
e.g.
4141

4242
```c
43-
jl_any_type = jl_new_abstracttype(jl_symbol("Any"), NULL, jl_null);
43+
jl_any_type = jl_new_abstracttype(jl_symbol("Any"), core, NULL, jl_emptysvec);
4444
jl_any_type->super = jl_any_type;
4545

46-
jl_type_type = jl_new_abstracttype(jl_symbol("Type"), jl_any_type, jl_null);
46+
jl_type_type = jl_new_abstracttype(jl_symbol("Type"), core, jl_any_type, jl_emptysvec);
4747

48-
jl_int32_type = jl_new_bitstype(jl_symbol("Int32"),
49-
jl_any_type, jl_null, 32);
48+
jl_int32_type = jl_new_primitivetype(jl_symbol("Int32"), core,
49+
jl_any_type, jl_emptysvec, 32);
5050
```
5151

5252
[`jl_init_tasks()`](https://github.com/JuliaLang/julia/blob/master/src/task.c) creates the `jl_datatype_t* jl_task_type`

src/ast.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ jl_sym_t *exc_sym; jl_sym_t *error_sym;
4444
jl_sym_t *new_sym; jl_sym_t *using_sym;
4545
jl_sym_t *const_sym; jl_sym_t *thunk_sym;
4646
jl_sym_t *anonymous_sym; jl_sym_t *underscore_sym;
47-
jl_sym_t *abstracttype_sym; jl_sym_t *bitstype_sym;
48-
jl_sym_t *compositetype_sym; jl_sym_t *foreigncall_sym;
47+
jl_sym_t *abstracttype_sym; jl_sym_t *primtype_sym;
48+
jl_sym_t *structtype_sym; jl_sym_t *foreigncall_sym;
4949
jl_sym_t *global_sym; jl_sym_t *list_sym;
5050
jl_sym_t *dot_sym; jl_sym_t *newvar_sym;
5151
jl_sym_t *boundscheck_sym; jl_sym_t *inbounds_sym;
@@ -414,8 +414,8 @@ void jl_init_frontend(void)
414414
underscore_sym = jl_symbol("_");
415415
amp_sym = jl_symbol("&");
416416
abstracttype_sym = jl_symbol("abstract_type");
417-
bitstype_sym = jl_symbol("bits_type");
418-
compositetype_sym = jl_symbol("composite_type");
417+
primtype_sym = jl_symbol("primitive_type");
418+
structtype_sym = jl_symbol("struct_type");
419419
toplevel_sym = jl_symbol("toplevel");
420420
dot_sym = jl_symbol(".");
421421
boundscheck_sym = jl_symbol("boundscheck");

src/builtins.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ JL_CALLABLE(jl_f_svec)
600600
return (jl_value_t*)t;
601601
}
602602

603-
// composite types ------------------------------------------------------------
603+
// struct operations ------------------------------------------------------------
604604

605605
JL_CALLABLE(jl_f_getfield)
606606
{

src/codegen.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -3859,8 +3859,8 @@ static jl_cgval_t emit_expr(jl_codectx_t &ctx, jl_value_t *expr)
38593859
ctx.builder.CreateCall(prepare_call(jltopeval_func), args);
38603860
return ghostValue(jl_void_type);
38613861
}
3862-
if (head == abstracttype_sym || head == compositetype_sym ||
3863-
head == bitstype_sym) {
3862+
if (head == abstracttype_sym || head == structtype_sym ||
3863+
head == primtype_sym) {
38643864
jl_errorf("type definition not allowed inside a local scope");
38653865
}
38663866
else {

src/interpreter.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ static jl_value_t *eval(jl_value_t *e, interpreter_state *s)
391391
JL_GC_POP();
392392
return (jl_value_t*)jl_nothing;
393393
}
394-
else if (ex->head == bitstype_sym) {
394+
else if (ex->head == primtype_sym) {
395395
if (inside_typedef)
396396
jl_error("cannot eval a new bits type definition while defining another type");
397397
jl_value_t *name = args[0];
@@ -439,7 +439,7 @@ static jl_value_t *eval(jl_value_t *e, interpreter_state *s)
439439
JL_GC_POP();
440440
return (jl_value_t*)jl_nothing;
441441
}
442-
else if (ex->head == compositetype_sym) {
442+
else if (ex->head == structtype_sym) {
443443
if (inside_typedef)
444444
jl_error("cannot eval a new data type definition while defining another type");
445445
jl_value_t *name = args[0];

src/julia-parser.scm

+3-3
Original file line numberDiff line numberDiff line change
@@ -1252,7 +1252,7 @@
12521252
(if (reserved-word? (peek-token s))
12531253
(error (string "invalid type name \"" (take-token s) "\"")))
12541254
(let ((sig (parse-subtype-spec s)))
1255-
(begin0 (list 'type (if mut? 'true 'false) sig (parse-block s))
1255+
(begin0 (list 'struct (if mut? 'true 'false) sig (parse-block s))
12561256
(expect-end s word))))
12571257

12581258
;; consume any number of line endings from a token stream
@@ -1400,7 +1400,7 @@
14001400
(begin (take-token s)
14011401
(let* ((spec (with-space-sensitive (parse-subtype-spec s)))
14021402
(nb (with-space-sensitive (parse-cond s))))
1403-
(begin0 (list 'bitstype nb spec)
1403+
(begin0 (list 'primitive spec nb)
14041404
(expect-end (take-lineendings s) "primitive type"))))))
14051405
;; deprecated type keywords
14061406
((type)
@@ -1414,7 +1414,7 @@
14141414
(spec (parse-subtype-spec s)))
14151415
(syntax-deprecation s (string "bitstype " (deparse nb) " " (deparse spec))
14161416
(string "primitive type " (deparse spec) " " (deparse nb) " end"))
1417-
(list 'bitstype nb spec)))
1417+
(list 'primitive spec nb)))
14181418
((typealias)
14191419
(let ((lhs (with-space-sensitive (parse-call s)))
14201420
(rhs (parse-where s parse-call)))

src/julia-syntax.scm

+42-34
Original file line numberDiff line numberDiff line change
@@ -887,9 +887,9 @@
887887
(global ,name) (const ,name)
888888
,@(map (lambda (v) `(local ,v)) params)
889889
,@(map (lambda (n v) (make-assignment n (bounds-to-TypeVar v))) params bounds)
890-
(composite_type ,name (call (core svec) ,@params)
891-
(call (core svec) ,@(map (lambda (x) `',x) field-names))
892-
,super (call (core svec) ,@field-types) ,mut ,min-initialized)))
890+
(struct_type ,name (call (core svec) ,@params)
891+
(call (core svec) ,@(map (lambda (x) `',x) field-names))
892+
,super (call (core svec) ,@field-types) ,mut ,min-initialized)))
893893
;; "inner" constructors
894894
(scope-block
895895
(block
@@ -925,7 +925,7 @@
925925
,@(map (lambda (n v) (make-assignment n (bounds-to-TypeVar v))) params bounds)
926926
(abstract_type ,name (call (core svec) ,@params) ,super))))))
927927

928-
(define (bits-def-expr n name params super)
928+
(define (primitive-type-def-expr n name params super)
929929
(receive
930930
(params bounds) (sparam-name-bounds params)
931931
`(block
@@ -934,7 +934,7 @@
934934
(block
935935
,@(map (lambda (v) `(local ,v)) params)
936936
,@(map (lambda (n v) (make-assignment n (bounds-to-TypeVar v))) params bounds)
937-
(bits_type ,name (call (core svec) ,@params) ,n ,super))))))
937+
(primitive_type ,name (call (core svec) ,@params) ,n ,super))))))
938938

939939
;; take apart a type signature, e.g. T{X} <: S{Y}
940940
(define (analyze-type-sig ex)
@@ -1188,7 +1188,7 @@
11881188
(else
11891189
(error "invalid macro definition"))))
11901190

1191-
(define (expand-type-def e)
1191+
(define (expand-struct-def e)
11921192
(let ((mut (cadr e))
11931193
(sig (caddr e))
11941194
(fields (cdr (cadddr e))))
@@ -1892,7 +1892,11 @@
18921892
'-> expand-arrow
18931893
'let expand-let
18941894
'macro expand-macro-def
1895-
'type expand-type-def
1895+
'struct expand-struct-def
1896+
'type
1897+
(lambda (e)
1898+
(syntax-deprecation #f ":type expression head" ":struct")
1899+
(expand-struct-def e))
18961900
'try expand-try
18971901
;; deprecated in 0.6
18981902
'typealias (lambda (e) (expand-typealias (cadr e) (caddr e)))
@@ -2084,11 +2088,15 @@
20842088

20852089
'bitstype
20862090
(lambda (e)
2087-
(let ((n (cadr e))
2088-
(sig (caddr e)))
2091+
(syntax-deprecation #f "Expr(:bitstype, nbits, name)" "Expr(:primitive, name, nbits)")
2092+
(expand-forms `(primitive ,(caddr e) ,(cadr e))))
2093+
'primitive
2094+
(lambda (e)
2095+
(let ((sig (cadr e))
2096+
(n (caddr e)))
20892097
(expand-forms
20902098
(receive (name params super) (analyze-type-sig sig)
2091-
(bits-def-expr n name params super)))))
2099+
(primitive-type-def-expr n name params super)))))
20922100

20932101
'comparison
20942102
(lambda (e) (expand-forms (expand-compare-chain (cdr e))))
@@ -2781,21 +2789,21 @@ f(x) = yt(x)
27812789
(() () 0 ())
27822790
(body (global ,name) (const ,name)
27832791
,@(map (lambda (p n) `(= ,p (call (core TypeVar) ',n (core Any)))) P names)
2784-
(composite_type ,name (call (core svec) ,@P)
2785-
(call (core svec) ,@(map (lambda (v) `',v) fields))
2786-
,super
2787-
(call (core svec) ,@types) false ,(length fields))
2792+
(struct_type ,name (call (core svec) ,@P)
2793+
(call (core svec) ,@(map (lambda (v) `',v) fields))
2794+
,super
2795+
(call (core svec) ,@types) false ,(length fields))
27882796
(return (null))))))))
27892797

27902798
(define (type-for-closure name fields super)
27912799
`((thunk (lambda ()
27922800
(() () 0 ())
27932801
(body (global ,name) (const ,name)
2794-
(composite_type ,name (call (core svec))
2795-
(call (core svec) ,@(map (lambda (v) `',v) fields))
2796-
,super
2797-
(call (core svec) ,@(map (lambda (v) '(core Any)) fields))
2798-
false ,(length fields))
2802+
(struct_type ,name (call (core svec))
2803+
(call (core svec) ,@(map (lambda (v) `',v) fields))
2804+
,super
2805+
(call (core svec) ,@(map (lambda (v) '(core Any)) fields))
2806+
false ,(length fields))
27992807
(return (null)))))))
28002808

28012809

@@ -2808,20 +2816,20 @@ f(x) = yt(x)
28082816
; `((global ,name)
28092817
; (const ,name)
28102818
; ,@(map (lambda (p n) `(= ,p (call (core TypeVar) ',n (core Any)))) P names)
2811-
; (composite_type ,name (call (core svec) ,@P)
2812-
; (call (core svec) ,@(map (lambda (v) `',v) fields))
2813-
; ,super
2814-
; (call (core svec) ,@types) false ,(length fields)))))
2819+
; (struct_type ,name (call (core svec) ,@P)
2820+
; (call (core svec) ,@(map (lambda (v) `',v) fields))
2821+
; ,super
2822+
; (call (core svec) ,@types) false ,(length fields)))))
28152823

28162824
;; ... and without parameters
28172825
;(define (type-for-closure name fields super)
28182826
; `((global ,name)
28192827
; (const ,name)
2820-
; (composite_type ,name (call (core svec))
2821-
; (call (core svec) ,@(map (lambda (v) `',v) fields))
2822-
; ,super
2823-
; (call (core svec) ,@(map (lambda (v) 'Any) fields))
2824-
; false ,(length fields))))
2828+
; (struct_type ,name (call (core svec))
2829+
; (call (core svec) ,@(map (lambda (v) `',v) fields))
2830+
; ,super
2831+
; (call (core svec) ,@(map (lambda (v) 'Any) fields))
2832+
; false ,(length fields))))
28252833

28262834

28272835
(define (vinfo:not-capt vi)
@@ -3634,25 +3642,25 @@ f(x) = yt(x)
36343642
((isdefined) (if tail (emit-return e) e))
36353643

36363644
;; top level expressions returning values
3637-
((abstract_type bits_type composite_type thunk toplevel module)
3645+
((abstract_type primitive_type struct_type thunk toplevel module)
36383646
(case (car e)
36393647
((abstract_type)
36403648
(let* ((para (compile (caddr e) break-labels #t #f))
36413649
(supe (compile (cadddr e) break-labels #t #f)))
36423650
(emit `(abstract_type ,(cadr e) ,para ,supe))))
3643-
((bits_type)
3651+
((primitive_type)
36443652
(let* ((para (compile (caddr e) break-labels #t #f))
36453653
(supe (compile (list-ref e 4) break-labels #t #f)))
3646-
(emit `(bits_type ,(cadr e) ,para ,(cadddr e) ,supe))))
3647-
((composite_type)
3654+
(emit `(primitive_type ,(cadr e) ,para ,(cadddr e) ,supe))))
3655+
((struct_type)
36483656
(let* ((para (compile (caddr e) break-labels #t #f))
36493657
(supe (compile (list-ref e 4) break-labels #t #f))
3650-
;; composite_type has an unconventional evaluation rule that
3658+
;; struct_type has an unconventional evaluation rule that
36513659
;; needs to do work around the evaluation of the field types,
36523660
;; so the field type expressions need to be kept in place as
36533661
;; much as possible. (part of issue #21923)
36543662
(ftys (compile (list-ref e 5) break-labels #t #f #f)))
3655-
(emit `(composite_type ,(cadr e) ,para ,(cadddr e) ,supe ,ftys ,@(list-tail e 6)))))
3663+
(emit `(struct_type ,(cadr e) ,para ,(cadddr e) ,supe ,ftys ,@(list-tail e 6)))))
36563664
(else
36573665
(emit e)))
36583666
(if tail (emit-return '(null)))

src/julia_internal.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -986,8 +986,8 @@ extern jl_sym_t *exc_sym; extern jl_sym_t *new_sym;
986986
extern jl_sym_t *compiler_temp_sym; extern jl_sym_t *foreigncall_sym;
987987
extern jl_sym_t *const_sym; extern jl_sym_t *thunk_sym;
988988
extern jl_sym_t *anonymous_sym; extern jl_sym_t *underscore_sym;
989-
extern jl_sym_t *abstracttype_sym; extern jl_sym_t *bitstype_sym;
990-
extern jl_sym_t *compositetype_sym;
989+
extern jl_sym_t *abstracttype_sym; extern jl_sym_t *primtype_sym;
990+
extern jl_sym_t *structtype_sym;
991991
extern jl_sym_t *global_sym; extern jl_sym_t *unused_sym;
992992
extern jl_sym_t *boundscheck_sym; extern jl_sym_t *inbounds_sym;
993993
extern jl_sym_t *copyast_sym; extern jl_sym_t *fastmath_sym;

0 commit comments

Comments
 (0)