plum

#treesitter#compiler#wasm

git clone https://git.pyrossh.dev/plum

A statically typed, imperative programming language inspired by rust, python


db94535 — Peter John 2026-11-11T00:29:16+05:30
native: every compiler-implemented @native must be declared in std, tested
compiler/core/src/native.rs CHANGED
@@ -40,7 +40,7 @@ pub enum NativeOp {
40
40
  pub enum Native {
41
41
  /// One wasm instruction.
42
42
  Op(NativeOp),
43
- /// A method or static of `Array<T>` (`init`, `get`, `set`, ...): wasm-gc array
43
+ /// A method or static of `Array<T>` (`init`, `at`, `set`, ...): wasm-gc array
44
44
  /// instructions.
45
45
  Array,
46
46
  /// `str.typeName<T>()`: the name of the type `T`, a string literal per instantiation.
@@ -84,6 +84,13 @@ const TABLE: &[(&str, Native)] = &[
84
84
  ("@memLoadU8", Native::Mem),
85
85
  ];
86
86
 
87
+ /// The qualified names of every `@native` function the compiler implements. Each one that is
88
+ /// not a built-in (`@name`) must be declared in std: a test (`everyNativeIsDeclaredInStd`)
89
+ /// fails when std renames, moves to another module or drops one the table still names.
90
+ pub fn implemented() -> impl Iterator<Item = &'static str> {
91
+ TABLE.iter().map(|(n, _)| *n)
92
+ }
93
+
87
94
  /// Whether `f`'s whole body is `@native`.
88
95
  pub fn isNative(f: &Fn) -> bool {
89
96
  match &f.body {
compiler/core/tests/core/native_test.rs CHANGED
@@ -170,3 +170,35 @@ fn builtinDeclarationsAreNative() {
170
170
  let again = s.items.iter().filter(|i| matches!(i, Item::Fn(f) if f.name.starts_with('@'))).count();
171
171
  assert_eq!(again, 4);
172
172
  }
173
+
174
+ /// Every `@native` function the compiler implements (but the built-ins it declares itself)
175
+ /// is declared `@native` in std under the same qualified name, and every one std declares is
176
+ /// implemented: renaming std's `math.plum` (its functions become `<new>.sqrt`), renaming a
177
+ /// method, or replacing a native body leaves a table entry nothing declares, and fails here.
178
+ #[test]
179
+ fn everyNativeIsDeclaredInStd() {
180
+ let root = plum_test_support::repoRoot();
181
+ let mut declared = std::collections::BTreeSet::new();
182
+ let mut files = vec![root.join("std")];
183
+ while let Some(p) = files.pop() {
184
+ if p.is_dir() {
185
+ files.extend(fs::read_dir(&p).unwrap().map(|e| e.unwrap().path()));
186
+ continue;
187
+ }
188
+ if p.extension().is_none_or(|x| x != "plum") {
189
+ continue;
190
+ }
191
+ let mut s = plum_test_support::parseRawSource(&fs::read_to_string(&p).unwrap());
192
+ for item in &mut s.items {
193
+ if let Item::Fn(f) = item {
194
+ if plum_core::native::isNative(f) {
195
+ f.file = p.display().to_string();
196
+ assert!(native(f).is_some(), "{}: `{}` is @native but not implemented", p.display(), qualifiedName(f));
197
+ declared.insert(qualifiedName(f));
198
+ }
199
+ }
200
+ }
201
+ }
202
+ let missing: Vec<&str> = plum_core::native::implemented().filter(|n| !n.starts_with('@') && !declared.contains(*n)).collect();
203
+ assert!(missing.is_empty(), "implemented but declared nowhere in std: {missing:?} (std declares {declared:?})");
204
+ }