From 3a922d4a22dbcbc2ab382503a931a7b967285a16 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Sun, 30 Aug 2026 23:01:59 +0200 Subject: [PATCH] Match MRI 4.0 Symbol#inspect quoting rules MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Symbol#inspect quoted many symbols that MRI prints bare. Rework the bare-vs-quoted predicate to mirror MRI's rb_enc_symname_type: a symbol prints bare when it is an operator method name, an identifier (optionally @/@@/$-prefixed, with the special-global-variable forms for $), or such an identifier with a class-appropriate trailing ? ! or =. Any byte >= 0x80 of a valid-UTF-8 body counts as an identifier character, so non-ASCII letters now print bare (:äöü, :测, :🦊, :±); a body that is not valid UTF-8 (a binary byte string) stays quoted. Also add a dedicated, stricter predicate (isSymLabel) for the Ruby 3.4+ hash short-form `name:`, which unlike Symbol#inspect quotes operator, prefixed, and attribute-setter names — fixing keys such as :+, :@x and :foo= that were wrongly printed as bare labels. core/symbol/inspect_spec: fail 35 -> 2 (remaining two need per-symbol encoding tracking for non-ASCII-compatible/dummy encodings). core/hash inspect "can be evaled" now passes. Verified byte-for-byte against ruby 4.0.x for the full spec table plus operator, global, attrset, prefix, non-ASCII and binary cases. Co-Authored-By: Claude Opus 4.8 --- internal/object/object.go | 217 +++++++++++++++--- internal/object/object_test.go | 130 +++++++++++ .../vm/symbol_inspect_conformance_test.go | 180 +++++++++++++++ 3 files changed, 496 insertions(+), 31 deletions(-) create mode 100644 internal/vm/symbol_inspect_conformance_test.go diff --git a/internal/object/object.go b/internal/object/object.go index b7b49489..dd100d2a 100644 --- a/internal/object/object.go +++ b/internal/object/object.go @@ -479,48 +479,203 @@ func (s Symbol) Inspect() string { } func (s Symbol) Truthy() bool { return true } -// plainOperatorSymbols are the operator names a symbol prints bare (`:+`). -var plainOperatorSymbols = map[string]bool{ - "+": true, "-": true, "*": true, "/": true, "%": true, "**": true, - "==": true, "===": true, "!=": true, "<": true, ">": true, "<=": true, - ">=": true, "<=>": true, "<<": true, ">>": true, "&": true, "|": true, - "^": true, "~": true, "!": true, "[]": true, "[]=": true, "+@": true, - "-@": true, "=~": true, -} +// symbol type classes, mirroring MRI's ID_* categories. They differ only in +// which trailing suffixes an identifier body may carry: a local or constant name +// admits a single `=` (attribute-setter), while a global/instance/class name +// admits neither `=` nor `?`/`!`. +const ( + symLocal = iota + symConst + symGlobal + symInstance + symClass +) // isPlainSymbol reports whether s prints as a bare `:name` rather than a quoted -// `:"…"`: an operator name, or an identifier (optionally @/@@/$-prefixed, with a -// single trailing ? ! or =). +// `:"…"`. It mirrors MRI's rb_enc_symname_type (a symbol prints bare exactly when +// that returns a type other than -1): an operator method name, or an identifier +// optionally prefixed by `@`/`@@`/`$` (with a special-global-variable form for +// `$`) and optionally suffixed by `?`/`!`/`=` as the class permits. Any byte +// >= 0x80 of a valid-UTF-8 body counts as an identifier character, so non-ASCII +// letters print bare (`:äöü`); a body that is not valid UTF-8 (e.g. a binary +// byte string) is always quoted. func isPlainSymbol(s string) bool { - if s == "" { + if s == "" || !utf8.ValidString(s) { return false } - if plainOperatorSymbols[s] { + e := len(s) + m := 0 + class := symLocal + switch c := s[0]; { + case c == '$': + if isSpecialGlobalName(s[1:]) { + return true + } + m, class = 1, symGlobal + case c == '@': + m, class = 1, symInstance + if m < e && s[m] == '@' { + m, class = 2, symClass + } + case c == '<': + m = 1 + if m < e { + switch s[m] { + case '<': + m++ + case '=': + m++ + if m < e && s[m] == '>' { + m++ + } + } + } + return m == e + case c == '>': + m = 1 + if m < e && (s[m] == '>' || s[m] == '=') { + m++ + } + return m == e + case c == '=': + m = 1 + if m >= e { + return false + } + switch s[m] { + case '~': + m++ + case '=': + m++ + if m < e && s[m] == '=' { + m++ + } + default: + return false + } + return m == e + case c == '*': + m = 1 + if m < e && s[m] == '*' { + m++ + } + return m == e + case c == '+' || c == '-': + m = 1 + if m < e && s[m] == '@' { + m++ + } + return m == e + case c == '|' || c == '^' || c == '&' || c == '/' || c == '%' || c == '~' || c == '`': + return e == 1 + case c == '[': + m = 1 + if m >= e || s[m] != ']' { + return false + } + m++ + if m < e && s[m] == '=' { + m++ + } + return m == e + case c == '!': + if e == 1 { + return true + } + return e == 2 && (s[1] == '=' || s[1] == '~') + default: + if c >= 'A' && c <= 'Z' { + class = symConst + } + } + // Identifier body (for a bare name, or following a @/@@/$ prefix). + if m >= e || !isSymIdentStart(s[m]) { + return false + } + for m++; m < e && isSymIdentChar(s[m]); m++ { + } + if m >= e { return true } - i := 0 - switch { - case strings.HasPrefix(s, "@@"): - i = 2 - case s[0] == '@' || s[0] == '$': - i = 1 + switch s[m] { + case '!', '?': + if class == symGlobal || class == symInstance || class == symClass { + return false + } + m++ + case '=': + if class != symLocal && class != symConst { + return false + } + m++ + default: + return false + } + return m == e +} + +// isSymLabel reports whether s prints as a bare hash label `name:` (Ruby 3.4+ +// hash inspect) rather than a quoted label `"name":`. This is stricter than +// isPlainSymbol: only a local or constant identifier with an optional single +// trailing `?` or `!` qualifies — operator names, `@`/`@@`/`$`-prefixed names, +// and attribute-setter (`=`) names are all quoted. +func isSymLabel(s string) bool { + if s == "" || !utf8.ValidString(s) || !isSymIdentStart(s[0]) { + return false + } + i := 1 + for ; i < len(s) && isSymIdentChar(s[i]); i++ { + } + if i == len(s) { + return true } - if i >= len(s) || !(isSymLetter(s[i]) || s[i] == '_') { + return i == len(s)-1 && (s[i] == '?' || s[i] == '!') +} + +// isSymIdentStart reports whether c may begin a symbol identifier body: an ASCII +// letter or underscore, or any byte of a (valid-UTF-8) multibyte character. +func isSymIdentStart(c byte) bool { + return c == '_' || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c >= 0x80 +} + +// isSymIdentChar reports whether c may continue a symbol identifier body: an +// identifier start, or an ASCII digit. +func isSymIdentChar(c byte) bool { + return isSymIdentStart(c) || (c >= '0' && c <= '9') +} + +// isSpecialGlobalName reports whether s (the text after a leading `$`) is one of +// MRI's special global-variable names, which print bare: a single punctuation +// character from a fixed set (`$~`, `$&`, `$'`, `$+`, `$0`, …), `$-` followed by +// exactly one identifier character (`$-w`), or a run of digits with no leading +// zero (`$1234`). It mirrors MRI's is_special_global_name. +func isSpecialGlobalName(s string) bool { + if s == "" { return false } - for i++; i < len(s); i++ { - c := s[i] - if isSymLetter(c) || (c >= '0' && c <= '9') || c == '_' { - continue + switch s[0] { + case '~', '*', '$', '?', '!', '@', '/', '\\', ';', ',', '.', '=', ':', + '<', '>', '"', '&', '`', '\'', '+', '0': + return len(s) == 1 + case '-': + if len(s) < 2 || !isSymIdentChar(s[1]) { + return false + } + if s[1] < 0x80 { + return len(s) == 2 } - // A trailing ? ! or = is allowed only as the final character. - return (c == '?' || c == '!' || c == '=') && i == len(s)-1 + _, sz := utf8.DecodeRuneInString(s[1:]) + return 1+sz == len(s) + default: + for i := 0; i < len(s); i++ { + if s[i] < '0' || s[i] > '9' { + return false + } + } + return true } - return true } -func isSymLetter(c byte) bool { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') } - // Array is a mutable, ordered list. It is a reference type (used as *Array), so // aliasing and in-place mutation (push, []=) behave as in Ruby. type Array struct { @@ -1023,9 +1178,9 @@ func (h *Hash) repr() string { // Ruby 4.0 (since 3.4) inspect: symbol keys use the label form // `name: value`; all other keys use `key => value` with spaces. if sym, ok := k.(Symbol); ok { - // A plain symbol key uses the bare label `name:`; one that needs - // quoting uses the quoted label `"name":`. - if isPlainSymbol(string(sym)) { + // A label-shaped symbol key uses the bare label `name:`; one that + // needs quoting uses the quoted label `"name":`. + if isSymLabel(string(sym)) { b.WriteString(string(sym) + ": " + v.Inspect()) } else { b.WriteString(NewString(string(sym)).Inspect() + ": " + v.Inspect()) diff --git a/internal/object/object_test.go b/internal/object/object_test.go index 8e2b0b39..bccea949 100644 --- a/internal/object/object_test.go +++ b/internal/object/object_test.go @@ -46,6 +46,136 @@ func TestToSAndInspect(t *testing.T) { } } +// TestSymbolInspectPredicate exercises every branch of the bare-vs-quoted +// decision in Symbol#inspect (isPlainSymbol / isSpecialGlobalName). Expected +// values were verified against MRI 4.0.x with a UTF-8 default external encoding. +func TestSymbolInspectPredicate(t *testing.T) { + tests := []struct { + name string + inspect string + }{ + // Identifiers, constants, trailing ? ! =. + {"fred", ":fred"}, + {"Bar", ":Bar"}, + {"_x", ":_x"}, + {"fred?", ":fred?"}, + {"fred!", ":fred!"}, + {"foo=", ":foo="}, + {"Foo=", ":Foo="}, + {"a=b", `:"a=b"`}, + {"foo?=", `:"foo?="`}, + // Instance / class / global names. + {"@ruby", ":@ruby"}, + {"@@ruby", ":@@ruby"}, + {"$ruby", ":$ruby"}, + {"$ruby!", `:"$ruby!"`}, + {"@ruby?", `:"@ruby?"`}, + {"@@ruby!", `:"@@ruby!"`}, + {"@x=", `:"@x="`}, + {"@", `:"@"`}, + {"@@", `:"@@"`}, + {"$", `:"$"`}, + // Special globals. + {"$-w", ":$-w"}, + {"$-ww", `:"$-ww"`}, + {"$-", `:"$-"`}, + {"$-ä", ":$-ä"}, + {"$+", ":$+"}, + {"$~", ":$~"}, + {"$_", ":$_"}, + {"$0", ":$0"}, + {"$00", `:"$00"`}, + {"$1234", ":$1234"}, + {"$ä", ":$ä"}, + // Operators. + {"+", ":+"}, + {"-", ":-"}, + {"*", ":*"}, + {"**", ":**"}, + {"***", `:"***"`}, + {"/", ":/"}, + {"%", ":%"}, + {"==", ":=="}, + {"===", ":==="}, + {"====", `:"===="`}, + {"=", `:"="`}, + {"=~", ":=~"}, + {"=>", `:"=>"`}, + {"<", ":<"}, + {"<=", ":<="}, + {"<=>", ":<=>"}, + {"<<", ":<<"}, + {">", ":>"}, + {">=", ":>="}, + {">>", ":>>"}, + {"&", ":&"}, + {"|", ":|"}, + {"^", ":^"}, + {"~", ":~"}, + {"~x", `:"~x"`}, + {"+@", ":+@"}, + {"-@", ":-@"}, + {"[]", ":[]"}, + {"[]=", ":[]="}, + {"[", `:"["`}, + {"`", ":`"}, + {"!", ":!"}, + {"!=", ":!="}, + {"!~", ":!~"}, + {"!x", `:"!x"`}, + // Non-ASCII identifiers print bare; punctuation/space/empty are quoted. + {"äöü", ":äöü"}, + {"测", ":测"}, + {"±", ":±"}, + {"a±b", ":a±b"}, + {"ä=", ":ä="}, + {"1abc", `:"1abc"`}, + {"foo bar", `:"foo bar"`}, + {"", `:""`}, + {"foo-bar", `:"foo-bar"`}, + // Invalid UTF-8 (binary) bodies are always quoted. + {"foo\xA4", `:"foo\xA4"`}, + } + for _, tc := range tests { + if got := Symbol(tc.name).Inspect(); got != tc.inspect { + t.Errorf("Symbol(%q).Inspect() = %q, want %q", tc.name, got, tc.inspect) + } + } +} + +// TestSymbolHashLabel exercises the stricter bare-vs-quoted label decision used +// by Hash#inspect for symbol keys (isSymLabel). Verified against MRI 4.0.x. +func TestSymbolHashLabel(t *testing.T) { + tests := []struct { + name string + inspect string + }{ + {"foo", "{foo: 1}"}, + {"Bar", "{Bar: 1}"}, + {"foo?", "{foo?: 1}"}, + {"foo!", "{foo!: 1}"}, + {"_x", "{_x: 1}"}, + {"äöü", "{äöü: 1}"}, + {"あ?", "{あ?: 1}"}, + {"foo=", `{"foo=": 1}`}, // attrset -> quoted + {"@x", `{"@x": 1}`}, // ivar -> quoted + {"$x", `{"$x": 1}`}, // gvar -> quoted + {"+", `{"+": 1}`}, // operator -> quoted + {"`", "{\"`\": 1}"}, + {"a?b", `{"a?b": 1}`}, // '?' not final -> quoted + {"a??", `{"a??": 1}`}, + {"0", `{"0": 1}`}, + {"foo\xA4", `{"foo\xA4": 1}`}, // invalid UTF-8 -> quoted + } + for _, tc := range tests { + h := NewHash() + h.Set(Symbol(tc.name), Integer(1)) + if got := h.Inspect(); got != tc.inspect { + t.Errorf("Hash{Symbol(%q):1}.Inspect() = %q, want %q", tc.name, got, tc.inspect) + } + } +} + func TestHashOps(t *testing.T) { h := NewHash() if h.Len() != 0 || h.repr() != "{}" { diff --git a/internal/vm/symbol_inspect_conformance_test.go b/internal/vm/symbol_inspect_conformance_test.go new file mode 100644 index 00000000..6b5fc468 --- /dev/null +++ b/internal/vm/symbol_inspect_conformance_test.go @@ -0,0 +1,180 @@ +package vm_test + +import "testing" + +// TestSymbolInspectConformance pins Symbol#inspect to MRI 4.0.x byte-for-byte. +// MRI prints a symbol bare (`:name`) when rb_enc_symname_type does not reject it, +// and quotes it (`:"…"`) otherwise. Every expected value here was produced by the +// reference interpreter (ruby 4.0.x, UTF-8 default external encoding). +func TestSymbolInspectConformance(t *testing.T) { + cases := []struct { + src string // a Ruby symbol literal + want string // its exact #inspect + }{ + // Plain identifiers and constants. + {`:fred`, `:fred`}, + {`:Bar`, `:Bar`}, + {`:Constant`, `:Constant`}, + {`:_under`, `:_under`}, + {`:foo_bar`, `:foo_bar`}, + + // Identifier with a single trailing ? ! or =. + {`:fred?`, `:fred?`}, + {`:fred!`, `:fred!`}, + {`:BAD!`, `:BAD!`}, + {`:_BAD!`, `:_BAD!`}, + {`:nil?`, `:nil?`}, + {`:foo=`, `:foo=`}, + {`:Foo=`, `:Foo=`}, + {`:"a=b"`, `:"a=b"`}, // '=' not final -> quoted + {`:"foo?="`, `:"foo?="`}, // '=' after '?' -> quoted + + // Instance / class / global variable names. + {`:@ruby`, `:@ruby`}, + {`:@@ruby`, `:@@ruby`}, + {`:$ruby`, `:$ruby`}, + {`:"$ruby!"`, `:"$ruby!"`}, // trailing ! on a global -> quoted + {`:"$ruby?"`, `:"$ruby?"`}, + {`:"@ruby!"`, `:"@ruby!"`}, + {`:"@ruby?"`, `:"@ruby?"`}, + {`:"@@ruby!"`, `:"@@ruby!"`}, + {`:"@@ruby?"`, `:"@@ruby?"`}, + {`:"@x="`, `:"@x="`}, // '=' on an ivar -> quoted + {`:"@@x="`, `:"@@x="`}, + {`:"$x="`, `:"$x="`}, + {`:"@"`, `:"@"`}, // bare @ -> quoted + {`:"@@"`, `:"@@"`}, // bare @@ -> quoted + {`:"$"`, `:"$"`}, // bare $ -> quoted + + // Special global-variable names. + {`:$-w`, `:$-w`}, + {`:"$-ww"`, `:"$-ww"`}, // more than one char after $- -> quoted + {`:"$-"`, `:"$-"`}, + {`:"$+"`, `:$+`}, + {`:"$~"`, `:$~`}, + {`:"$:"`, `:$:`}, + {`:"$?"`, `:$?`}, + {`:"$<"`, `:$<`}, + {`:"$_"`, `:$_`}, // $ + identifier '_' + {`:"$/"`, `:$/`}, + {`:"$$"`, `:$$`}, + {`:"$."`, `:$.`}, + {`:"$&"`, `:$&`}, + {`:"$@"`, `:$@`}, + {`:"$0"`, `:$0`}, + {`:"$00"`, `:"$00"`}, // leading zero, len>1 -> quoted + {`:"$012"`, `:"$012"`}, // leading zero -> quoted + {`:"$1234"`, `:$1234`}, // digits, no leading zero -> bare + {`:"$ä"`, `:$ä`}, // $ + non-ASCII identifier + + // Operator method names. + {`:+`, `:+`}, + {`:-`, `:-`}, + {`:*`, `:*`}, + {`:/`, `:/`}, + {`:%`, `:%`}, + {`:**`, `:**`}, + {`:"***"`, `:"***"`}, + {`:==`, `:==`}, + {`:===`, `:===`}, + {`:"===="`, `:"===="`}, + {`:"="`, `:"="`}, + {`:=~`, `:=~`}, + {`:"=>"`, `:"=>"`}, + {`:<`, `:<`}, + {`:<=`, `:<=`}, + {`:<=>`, `:<=>`}, + {`:"<<"`, `:<<`}, + {`:>`, `:>`}, + {`:>=`, `:>=`}, + {`:>>`, `:>>`}, + {`:&`, `:&`}, + {`:|`, `:|`}, + {`:^`, `:^`}, + {`:~`, `:~`}, + {`:"~x"`, `:"~x"`}, + {`:+@`, `:+@`}, + {`:-@`, `:-@`}, + {`:[]`, `:[]`}, + {`:[]=`, `:[]=`}, + {`:"["`, `:"["`}, + {":`", ":`"}, // backtick operator (bare) + {`:!`, `:!`}, + {`:"!="`, `:!=`}, + {`:"!~"`, `:!~`}, + {`:"!x"`, `:"!x"`}, // '!' + more -> quoted + + // Non-ASCII (valid UTF-8) identifiers print bare. + {`:äöü`, `:äöü`}, + {`:ê`, `:ê`}, + {`:测`, `:测`}, + {`:🦊`, `:🦊`}, + {`:"±"`, `:±`}, + {`:"²"`, `:²`}, + {`:"①"`, `:①`}, + {`:"a±b"`, `:a±b`}, + {`:"ä="`, `:ä=`}, + {`:"ä?"`, `:ä?`}, + + // Names that must be quoted (punctuation, spaces, leading digit, empty). + {`:"1abc"`, `:"1abc"`}, + {`:"3d"`, `:"3d"`}, + {`:"9"`, `:"9"`}, + {`:"foo bar"`, `:"foo bar"`}, + {`:"*foo"`, `:"*foo"`}, + {`:"foo "`, `:"foo "`}, + {`:" foo"`, `:" foo"`}, + {`:" "`, `:" "`}, + {`:"foo-bar"`, `:"foo-bar"`}, + {`:"++"`, `:"++"`}, + {`:"||"`, `:"||"`}, + {`:","`, `:","`}, + {`:"."`, `:"."`}, + {`:".."`, `:".."`}, + {`:"::"`, `:"::"`}, + {`:""`, `:""`}, + } + for _, c := range cases { + got := eval(t, "puts "+c.src+".inspect") + if got != c.want+"\n" { + t.Errorf("%s.inspect = %q, want %q", c.src, got, c.want+"\n") + } + } +} + +// TestSymbolHashLabelConformance pins the Ruby 3.4+ hash short-form (`name:`) +// used when a Symbol is a hash key. It is stricter than Symbol#inspect: only a +// local/constant identifier with an optional single trailing ? or ! prints as a +// bare label; everything else uses the quoted label form `"name":`. +func TestSymbolHashLabelConformance(t *testing.T) { + cases := []struct { + key string + want string + }{ + {`:foo`, `{foo: 1}`}, + {`:Bar`, `{Bar: 1}`}, + {`:foo?`, `{foo?: 1}`}, + {`:foo!`, `{foo!: 1}`}, + {`:BAD!`, `{BAD!: 1}`}, + {`:_x`, `{_x: 1}`}, + {`:äöü`, `{äöü: 1}`}, + {`:"あ?"`, `{あ?: 1}`}, // identifier + trailing ? -> bare label + {`:foo=`, `{"foo=": 1}`}, // attrset -> quoted label + {`:"@x"`, `{"@x": 1}`}, // ivar -> quoted + {`:"@@x"`, `{"@@x": 1}`}, + {`:"$x"`, `{"$x": 1}`}, + {`:+`, `{"+": 1}`}, // operator -> quoted + {`:[]`, `{"[]": 1}`}, + {":`", "{\"`\": 1}"}, + {`:!`, `{"!": 1}`}, + {`:"0"`, `{"0": 1}`}, + {`:"a?b"`, `{"a?b": 1}`}, // '?' not final -> quoted + {`:"a??"`, `{"a??": 1}`}, + } + for _, c := range cases { + got := eval(t, "p({"+c.key+" => 1})") + if got != c.want+"\n" { + t.Errorf("{%s => 1}.inspect = %q, want %q", c.key, got, c.want+"\n") + } + } +}