Skip to content

Commit

Permalink
runtime: make setfield! error text consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
vtjnash committed Nov 2, 2018
1 parent ceec71e commit 27bbb8a
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 8 deletions.
2 changes: 1 addition & 1 deletion base/docs/basedocs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1319,7 +1319,7 @@ julia> a = 1//2
1//2
julia> setfield!(a, :num, 3);
ERROR: type Rational is immutable
ERROR: setfield! cannot change immutable struct
```
"""
setfield!
Expand Down
4 changes: 2 additions & 2 deletions src/builtins.c
Original file line number Diff line number Diff line change
Expand Up @@ -677,10 +677,10 @@ JL_CALLABLE(jl_f_setfield)
jl_type_error("setfield!", (jl_value_t*)jl_datatype_type, v);
jl_datatype_t *st = (jl_datatype_t*)vt;
if (!st->mutabl)
jl_errorf("type %s is immutable", jl_symbol_name(st->name->name));
jl_errorf("setfield! immutable struct of type %s cannot be changed", jl_symbol_name(st->name->name));
size_t idx;
if (jl_is_long(args[1])) {
idx = jl_unbox_long(args[1])-1;
idx = jl_unbox_long(args[1]) - 1;
if (idx >= jl_datatype_nfields(st))
jl_bounds_error(args[0], args[1]);
}
Expand Down
6 changes: 4 additions & 2 deletions src/cgutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2421,8 +2421,10 @@ static void emit_setfield(jl_codectx_t &ctx,
}
}
else {
// TODO: better error
emit_error(ctx, "type is immutable");
std::string msg = "setfield! immutable struct of type "
+ std::string(jl_symbol_name(sty->name->name))
+ " cannot be changed";
emit_error(ctx, msg);
}
}

Expand Down
6 changes: 3 additions & 3 deletions test/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1069,9 +1069,9 @@ let
strct = LoadError("yofile", 0, "bad")
@test nfields(strct) == 3 # sanity test
@test_throws BoundsError(strct, 10) getfield(strct, 10)
@test_throws ErrorException("type LoadError is immutable") setfield!(strct, 0, "")
@test_throws ErrorException("type LoadError is immutable") setfield!(strct, 4, "")
@test_throws ErrorException("type is immutable") setfield!(strct, :line, 0)
@test_throws ErrorException("setfield! immutable struct of type LoadError cannot be changed") setfield!(strct, 0, "")
@test_throws ErrorException("setfield! immutable struct of type LoadError cannot be changed") setfield!(strct, 4, "")
@test_throws ErrorException("setfield! immutable struct of type LoadError cannot be changed") setfield!(strct, :line, 0)
@test strct.file == "yofile"
@test strct.line === 0
@test strct.error == "bad"
Expand Down

0 comments on commit 27bbb8a

Please sign in to comment.