File contents 诊断修复 MoonBit 错误
触发条件
用户遇到编译错误、警告或运行时异常 时激活。
Top 10 常见错误速查
错误码
含义
最常见原因
修复
E0015
unused_mut
声明了 mut 但未重新赋值绑定
改为 let
E0029
unused_package
存在其他编译错误导致 import 看似未使用
修复底层错误
E3002
Parse error
语法格式错误(mut self/for 解构/match 逗号)
检查具体语法
E4021
unbound variable
Trait 方法内未限定的标识符
用 Trait::method()
E4036
read-only type
外部包无法构造 pub struct
改为 pub(all)
E4051
unused_mut (warn)
同 E0015
改为 let
E4087
immutable field
字段缺少 mut 声明
添加 mut
E4145
sealed trait
无法实现 pub trait
改为 pub(open)trait
E4014
type mismatch
类型不匹配
检查解构/返回类型
Wxxx
Warning
各种警告
对照具体 warning 处理
执行步骤
Step 1: 获取完整错误列表
moon check # 获取所有编译错误
moon fmt # 自动修复格式问题
Step 2: 定位第一个真正错误
总是从第一个真正的错误开始修复
后续错误可能是级联产生的伪错误
区分 Error vs Warning(Warning 不阻断编译但不应忽略)
Step 3: 查询错误码
对照上表匹配错误码
详细信息见 references/error-codes.md
无匹配错误码?→ 可能是新版本新增,搜索官方文档
Step 4: 应用修复方案
按错误码对应的修复方式操作:
mut 相关 (E0015/E4051/E4087) : 检查是否真的需要 let mut
可见性相关 (E4036/E4145) : 检查 pub vs pub(all), trait vs pub(open)trait
语法相关 (E3002) : 检查 mut self / for 解构 / match 分支格式
作用域相关 (E4021) : Trait 方法内用完全限定名
Step 5: 重新验证
moon check # 确认错误消除
moon test # 确保测试仍通过
循环 Step 2-5 直到 Exit code 0。
八大陷阱快速修复
#
陷阱
❌ 错误
✅ 修复
1
mut self
fn method(mut self)
内部 let s = self
2
for 元组解构
for (a,b) in arr
for p in arr { match p { (a,b) => ... }}
3
Match 逗号分支
A => f(); B => g(),
A => { f() } B => { g() }
4
构造只读类型
直接构造 pub struct
改 pub(all) struct
5
实现密封 Trait
实现 pub trait
改 pub(open)trait
6
Trait unbound
other_method(self)
@pkg::Trait::other_method(self)
7
Option 冗余通配
match o { Some(x)=>x; None=>(); _=>() }
删除 _ 分支
8
变量名混淆
(f, w) => use w.0
(nid, _) => use nid.0
详细知识
🔗 references/error-codes.md — 完整错误码速查与诊断工作流
🔗 references/pitfalls.md — 常见陷阱深度解析
🔗 references/type-system.md — 可见性系统详解
🔗 references/generics-traits-methods.md — Part 4.5 可变性语义
1 --- 2 name: 3-debug-errors 3 description: 诊断修复 MoonBit 错误 4 --- 5 6 # 诊断修复 MoonBit 错误 7 8 ## 触发条件 9 用户遇到**编译错误、警告或运行时异常**时激活。 10 11 ## Top 10 常见错误速查 12 13 | 错误码 | 含义 | 最常见原因 | 修复 | 14 |--------|------|-----------|------| 15 | **E0015** | unused_mut | 声明了 mut 但未重新赋值绑定 | 改为 `let` | 16 | **E0029** | unused_package | 存在其他编译错误导致 import 看似未使用 | 修复底层错误 | 17 | **E3002** | Parse error | 语法格式错误(mut self/for 解构/match 逗号)| 检查具体语法 | 18 | **E4021** | unbound variable | Trait 方法内未限定的标识符 | 用 `Trait::method()` | 19 | **E4036** | read-only type | 外部包无法构造 `pub struct` | 改为 `pub(all)` | 20 | **E4051** | unused_mut (warn) | 同 E0015 | 改为 `let` | 21 | **E4087** | immutable field | 字段缺少 `mut` 声明 | 添加 `mut` | 22 | **E4145** | sealed trait | 无法实现 `pub trait` | 改为 `pub(open)trait` | 23 | **E4014** | type mismatch | 类型不匹配 | 检查解构/返回类型 | 24 | **Wxxx** | Warning | 各种警告 | 对照具体 warning 处理 | 25 26 ## 执行步骤 27 28 ### Step 1: 获取完整错误列表 29 ```bash 30 moon check # 获取所有编译错误 31 moon fmt # 自动修复格式问题 32 ``` 33 34 ### Step 2: 定位第一个真正错误 35 - **总是从第一个真正的错误开始修复** 36 - 后续错误可能是级联产生的伪错误 37 - 区分 Error vs Warning(Warning 不阻断编译但不应忽略) 38 39 ### Step 3: 查询错误码 40 - 对照上表匹配错误码 41 - 详细信息见 `references/error-codes.md` 42 - 无匹配错误码?→ 可能是新版本新增,搜索官方文档 43 44 ### Step 4: 应用修复方案 45 按错误码对应的修复方式操作: 46 - **mut 相关 (E0015/E4051/E4087)**: 检查是否真的需要 `let mut` 47 - **可见性相关 (E4036/E4145)**: 检查 pub vs pub(all), trait vs pub(open)trait 48 - **语法相关 (E3002)**: 检查 mut self / for 解构 / match 分支格式 49 - **作用域相关 (E4021)**: Trait 方法内用完全限定名 50 51 ### Step 5: 重新验证 52 ```bash 53 moon check # 确认错误消除 54 moon test # 确保测试仍通过 55 ``` 56 57 循环 Step 2-5 直到 Exit code 0。 58 59 ## 八大陷阱快速修复 60 61 | # | 陷阱 | ❌ 错误 | ✅ 修复 | 62 |---|------|---------|---------| 63 | 1 | mut self | `fn method(mut self)` | 内部 `let s = self` | 64 | 2 | for 元组解构 | `for (a,b) in arr` | `for p in arr { match p { (a,b) => ... }}` | 65 | 3 | Match 逗号分支 | `A => f(); B => g(),` | `A => { f() } B => { g() }` | 66 | 4 | 构造只读类型 | 直接构造 pub struct | 改 pub(all) struct | 67 | 5 | 实现密封 Trait | 实现 pub trait | 改 pub(open)trait | 68 | 6 | Trait unbound | `other_method(self)` | `@pkg::Trait::other_method(self)` | 69 | 7 | Option 冗余通配 | `match o { Some(x)=>x; None=>(); _=>() }` | 删除 `_` 分支 | 70 | 8 | 变量名混淆 | `(f, w) => use w.0` | `(nid, _) => use nid.0` | 71 72 ## 详细知识 73 🔗 `references/error-codes.md` — 完整错误码速查与诊断工作流 74 🔗 `references/pitfalls.md` — 常见陷阱深度解析 75 🔗 `references/type-system.md` — 可见性系统详解 76 🔗 `references/generics-traits-methods.md` — Part 4.5 可变性语义
morning-start/agent-skills/tree/main/lang/moonbit/skills/3-debug-errors commit c776f8b1e4
Frequently asked questions How do I install the 3 Debug Errors skill? Run npx skillmds@latest add morning-start/3-debug-errors in your terminal (requires Node.js), paste this page's agent-chat prompt into Claude, Cursor, or any MCP-connected agent, or download the SKILL.md file and copy it into your agent's skills directory.
What does the 3 Debug Errors skill do? 诊断修复 MoonBit 错误 It is listed under Coding & Dev Tools on SkillMD.
Is 3 Debug Errors safe to use? This skill has not completed SkillMD's automated safety review yet. SkillMD never runs a skill's scripts for you; review the SKILL.md before installing.
Which AI agents work with 3 Debug Errors? This skill is tagged as working with Claude Code, Claude.ai, OpenAI Codex. SKILL.md is an open format, so most agents that read a skills directory can load it too.
Is 3 Debug Errors free to use? Yes. Installing skills from SkillMD is free, and the skill stays under its author's original license.
Who published 3 Debug Errors? morning-start (@morning-start) published this skill. Their other Agent Skills are listed on their SkillMD profile.