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

RFC: "quiet" types for easier-to-read stack traces #48444

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
2 changes: 2 additions & 0 deletions base/experimental.jl
Original file line number Diff line number Diff line change
Expand Up @@ -368,4 +368,6 @@ adding them to the global method table.
"""
:@MethodTable

macro quietparams() Expr(:meta, :quietparams) end

end
19 changes: 16 additions & 3 deletions base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -462,8 +462,12 @@ show(x) = show(stdout, x)
show_default(io::IO, @nospecialize(x)) = _show_default(io, inferencebarrier(x))

function _show_default(io::IO, @nospecialize(x))
t = typeof(x)
show(io, inferencebarrier(t)::DataType)
t = inferencebarrier(typeof(x))::DataType
if is_quiet_type(t)
show_type_name(io, t.name)
else
show(io, t)
end
print(io, '(')
nf = nfields(x)
nb = sizeof(x)::Int
Expand Down Expand Up @@ -559,6 +563,11 @@ function print_without_params(@nospecialize(x))
return isa(b, DataType) && b.name.wrapper === x
end

function is_quiet_type(@nospecialize(x))
b = unwrap_unionall(x)
return isa(b, DataType) && b.name.flags & 0x8 != 0
end

function io_has_tvar_name(io::IOContext, name::Symbol, @nospecialize(x))
for (key, val) in io.dict
if key === :unionall_env && val isa TypeVar && val.name === name && has_typevar(x, val)
Expand Down Expand Up @@ -1092,7 +1101,11 @@ function show_datatype(io::IO, x::DataType, wheres::Vector{TypeVar}=TypeVar[])
end
else
show_type_name(io, x.name)
show_typeparams(io, parameters, (unwrap_unionall(x.name.wrapper)::DataType).parameters, wheres)
if n > 0 && get(io, :backtrace, false)::Bool && is_quiet_type(x) && !has_free_typevars(x)
print(io, "{...}")
else
show_typeparams(io, parameters, (unwrap_unionall(x.name.wrapper)::DataType).parameters, wheres)
end
end
end

Expand Down
2 changes: 2 additions & 0 deletions src/ast.scm
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,8 @@

(define (make-decl n t) `(|::| ,n ,t))

(define (make-bool x) (if x '(true) '(false)))

(define (ssavalue? e)
(and (pair? e) (eq? (car e) 'ssavalue)))

Expand Down
5 changes: 4 additions & 1 deletion src/builtins.c
Original file line number Diff line number Diff line change
Expand Up @@ -1498,20 +1498,23 @@ JL_CALLABLE(jl_f_arrayset)

JL_CALLABLE(jl_f__structtype)
{
JL_NARGS(_structtype, 7, 7);
JL_NARGS(_structtype, 8, 8);
JL_TYPECHK(_structtype, module, args[0]);
JL_TYPECHK(_structtype, symbol, args[1]);
JL_TYPECHK(_structtype, simplevector, args[2]);
JL_TYPECHK(_structtype, simplevector, args[3]);
JL_TYPECHK(_structtype, simplevector, args[4]);
JL_TYPECHK(_structtype, bool, args[5]);
JL_TYPECHK(_structtype, long, args[6]);
JL_TYPECHK(_structtype, bool, args[7]);
jl_value_t *fieldnames = args[3];
jl_value_t *fieldattrs = args[4];
jl_datatype_t *dt = NULL;
dt = jl_new_datatype((jl_sym_t*)args[1], (jl_module_t*)args[0], NULL, (jl_svec_t*)args[2],
(jl_svec_t*)fieldnames, NULL, (jl_svec_t*)fieldattrs,
0, args[5]==jl_true ? 1 : 0, jl_unbox_long(args[6]));
if (args[7] == jl_true)
dt->name->quietparams = 1;
return dt->name->wrapper;
}

Expand Down
1 change: 1 addition & 0 deletions src/datatype.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ JL_DLLEXPORT jl_typename_t *jl_new_typename_in(jl_sym_t *name, jl_module_t *modu
tn->abstract = abstract;
tn->mutabl = mutabl;
tn->mayinlinealloc = 0;
tn->quietparams = 0;
tn->mt = NULL;
tn->partial = NULL;
tn->atomicfields = NULL;
Expand Down
9 changes: 5 additions & 4 deletions src/julia-syntax.scm
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,8 @@
x)))
fields)))
(attrs (reverse attrs))
(defs (filter (lambda (x) (not (or (effect-free? x) (eq? (car x) 'string)))) defs))
(meta (map cadr (filter meta? defs)))
(defs (filter (lambda (x) (not (or (effect-free? x) (eq? (car x) 'string) (meta? x)))) defs))
(locs (if (and (pair? fields0) (linenum? (car fields0)))
(list (car fields0))
'()))
Expand Down Expand Up @@ -989,7 +990,7 @@
(= ,name (call (core _structtype) (thismodule) (inert ,name) (call (core svec) ,@params)
(call (core svec) ,@(map quotify field-names))
(call (core svec) ,@attrs)
,mut ,min-initialized))
,mut ,min-initialized ,(make-bool (memq 'quietparams meta))))
(call (core _setsuper!) ,name ,super)
(if (isdefined (outerref ,name))
(block
Expand Down Expand Up @@ -3418,7 +3419,7 @@ f(x) = yt(x)
(= ,s (call (core _structtype) (thismodule) (inert ,name) (call (core svec) ,@P)
(call (core svec) ,@(map quotify fields))
(call (core svec))
(false) ,(length fields)))
(false) ,(length fields) (false)))
(call (core _setsuper!) ,s ,super)
(= (outerref ,name) ,s)
(call (core _typebody!) ,s (call (core svec) ,@types))
Expand All @@ -3432,7 +3433,7 @@ f(x) = yt(x)
(= ,s (call (core _structtype) (thismodule) (inert ,name) (call (core svec))
(call (core svec) ,@(map quotify fields))
(call (core svec))
(false) ,(length fields)))
(false) ,(length fields) (false)))
(call (core _setsuper!) ,s ,super)
(= (outerref ,name) ,s)
(call (core _typebody!) ,s
Expand Down
3 changes: 2 additions & 1 deletion src/julia.h
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,8 @@ typedef struct {
uint8_t abstract:1;
uint8_t mutabl:1;
uint8_t mayinlinealloc:1;
uint8_t _reserved:5;
uint8_t quietparams:1;
uint8_t _reserved:4;
uint8_t max_methods; // override for inference's max_methods setting (0 = no additional limit or relaxation)
} jl_typename_t;

Expand Down
13 changes: 13 additions & 0 deletions test/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2595,3 +2595,16 @@ end
ir = Core.Compiler.complete(compact)
verify_display(ir)
end

@testset "quiet type parameters" begin
struct HasQuietParams{A,B,C}
a::A
b::B
c::C
Base.Experimental.@quietparams
end
obj = HasQuietParams(1,2,:a)
@test repr(obj) == "$(@__MODULE__).HasQuietParams(1, 2, :a)"
trace = try; error(obj); catch; stacktrace(catch_backtrace()); end
@test contains(sprint(io->Base.show_backtrace(io, trace)), "HasQuietParams{...}")
end