Skip to content

compiler: make := bindings immutable and use tail pub tags - #212

Merged
dslsdzc merged 1 commit into
developfrom
immutable-bindings-tail-pub
Oct 26, 2026
Merged

dslsdzc merged 1 commit into
developfrom
immutable-bindings-tail-pub

Conversation

@dslsdzc

@dslsdzc dslsdzc commented Oct 26, 2026

Copy link
Copy Markdown
Owner

概要

按语言语法计划补齐两条被实现绕开的规则。

1. := / : T = 是不可变绑定

docs/superpowers/plans/2026-09-01-language-syntax-rewrite.md:140 明确:
x := 42 推断为不可变,x : ., mut = 0 才是可变写法。

  • 符号行增加 mut 位(per-binding,不新增全局名字表)。
  • EXPR_LET 注册时写入声明的 mut 位;顶层全局同理。
  • EXPR_ASSIGN 对标识符目标读取 mut 位:不可变 → TA04 Cannot assign to immutable variable,不再静默接受。
  • 参数、match 绑定、for 变量保留兼容默认可变(参数 mut 语法不在本批范围)。

全仓迁移(否则检查会正确地报错):

  • 470 个被重新赋值的 name := → name : ., mut =;
  • 40 个被重新赋值的显式 name : T = → name : T, mut =;
  • tests/**/*.py 内嵌 Core 测试夹具(单行/三引号)同步迁移;
  • 编译器自身、stdlib、suite/probes/examples 一并迁移。

2. 类型 pub 使用尾部标签

不再写 pub struct Bits32::

struct Bits32, pub:
enum Mode, pub:
interface Named, pub:
  • corec parser 原本已支持尾部标签;本批把 bootstrap parser 的
    struct / enum / interface / type alias 也补齐为同一语法。
  • stdlib 的公开类型全部改为尾部标签。
  • pub struct 前缀仅保留旧源兼容路径,文档不再推荐。

3. interface 边界

interface 现在和 struct/enum/type alias 一样携带声明行并写入导出表:
非 pub interface 跨库不可见;同库 interface 测试通过。

验证

  • selfhost-tests / bootstrap-tests / check 全绿
  • canary 5/5 不变(迁移只改绑定可变性位与标签,发射面零变化)
  • 新增/扩展判据:
    • test_pub_visibility:interface 同库使用;
    • 既有 test_bits / test_intmap / test_optional / test_multibranch_v10 /
      test_ccr_v7 / test_live_ranges / test_named_face / test_region_escape
      全部在不可变语义下重跑通过。

说明

  • 本批不引入参数 mut 语法;参数仍默认可赋值,避免把范围扩到调用约定。
  • test_lsp.py 的既有 6/10 本地失败与本批无关;pub/LSP 过滤由独立的
    test_lsp_visibility.py 覆盖并已挂 CI。

- `:=` and un-tagged `: T =` are immutable bindings, per the language
  syntax plan. Assigning to one now fails closed with TA04
  (Cannot assign to immutable variable). Parameters, match bindings and
  loop variables keep their compatible mutable default.
- Migrate the whole tree: 470 inferred declarations and 40 explicit
  declarations that are reassigned now use `: ., mut =` / `: T, mut =`.
  Test fixtures with inline Core sources are migrated the same way.
- Type declarations use tail labels: `struct S, pub:`, `enum E, pub:`,
  `interface I, pub:`. The bootstrap and corec parsers both accept the
  tail form; the prefix `pub struct` remains only as source compatibility.
- Interface types join the pub boundary: declaration line + export table
  visibility, with a same-library interface test.
- Docs updated. Canary 5/5 unchanged; selfhost/bootstrap/check green.
@dslsdzc
dslsdzc merged commit c96226b into develop Oct 26, 2026
4 checks passed
@dslsdzc
dslsdzc deleted the immutable-bindings-tail-pub branch October 26, 2026 20:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant