plum

#treesitter#compiler#wasm

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

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


3efe50e — Peter John 2026-11-11T00:49:56+05:30
uniqueness: format a scan's failure message only when it is the first
Files changed (1) hide show
  1. compiler/core/src/uniqueness.rs +31 -22
compiler/core/src/uniqueness.rs CHANGED
@@ -1040,6 +1040,14 @@ impl<'a> Scan<'a> {
1040
1040
  }
1041
1041
  }
1042
1042
 
1043
+ /// `fail` with a message made only if it is the first: a scan meets many candidate
1044
+ /// failures, and formatting each one was most of the proof's time.
1045
+ fn failWith(&mut self, m: impl FnOnce() -> String) {
1046
+ if self.err.is_none() {
1047
+ self.err = Some(m());
1048
+ }
1049
+ }
1050
+
1043
1051
  fn bare(&self, e: &Expr) -> bool {
1044
1052
  isVar(e, self.v)
1045
1053
  }
@@ -1097,9 +1105,9 @@ impl<'a> Scan<'a> {
1097
1105
  }
1098
1106
 
1099
1107
  /// Walk `e` where a bare use of the variable is not allowed.
1100
- fn operand(&mut self, e: &Expr, live: bool, why: String) {
1108
+ fn operand(&mut self, e: &Expr, live: bool, why: impl FnOnce() -> String) {
1101
1109
  if self.bare(e) {
1102
- self.fail(why);
1110
+ self.failWith(why);
1103
1111
  } else {
1104
1112
  self.expr(e, false, live);
1105
1113
  }
@@ -1148,7 +1156,8 @@ impl<'a> Scan<'a> {
1148
1156
  let mut names = vec![];
1149
1157
  ps.iter().for_each(|p| crate::visit::patternBinders(p, &mut names));
1150
1158
  if names.iter().any(|n| n == self.v) {
1159
+ let v = self.v;
1151
- self.fail(format!("`{}` is rebound by a pattern", self.v));
1160
+ self.failWith(|| format!("`{v}` is rebound by a pattern"));
1152
1161
  }
1153
1162
  }
1154
1163
 
@@ -1194,7 +1203,7 @@ impl<'a> Scan<'a> {
1194
1203
  }
1195
1204
 
1196
1205
  fn stmt(&mut self, s: &Stmt, tail: bool, live: bool, depth: usize) {
1197
- let v = self.v.to_string();
1206
+ let v: &'a str = self.v;
1198
1207
  match &s.kind {
1199
1208
  StmtKind::Expr(e, _, _) => self.expr(e, tail, live),
1200
1209
  StmtKind::Return(Some(e)) => self.expr(e, true, false),
@@ -1209,11 +1218,11 @@ impl<'a> Scan<'a> {
1209
1218
  match self.boundDepth {
1210
1219
  None => self.boundDepth = Some(depth),
1211
1220
  Some(d) if d == depth => {}
1212
- Some(_) => self.fail(format!("`{v}` is shadowed in an inner scope")),
1221
+ Some(_) => self.failWith(|| format!("`{v}` is shadowed in an inner scope")),
1213
1222
  }
1214
1223
  }
1215
1224
  if !self.bindingOk(val, a.declare) {
1216
- self.fail(format!("`{v}` is bound to a value that may be shared"));
1225
+ self.failWith(|| format!("`{v}` is bound to a value that may be shared"));
1217
1226
  } else {
1218
1227
  self.expr(val, false, true);
1219
1228
  }
@@ -1224,18 +1233,18 @@ impl<'a> Scan<'a> {
1224
1233
  AssignTarget::Var(_) if !live && matches!(val.kind, ExprKind::Block(_)) => {
1225
1234
  self.expr(val, true, false)
1226
1235
  }
1227
- AssignTarget::Var(n) => self.operand(val, live, format!("`{v}` is also held by `{n}`")),
1236
+ AssignTarget::Var(n) => self.operand(val, live, || format!("`{v}` is also held by `{n}`")),
1228
1237
  AssignTarget::Field(o, f) => {
1229
1238
  if !self.bare(o) {
1230
1239
  self.expr(o, false, true);
1231
1240
  }
1232
- self.operand(val, live, format!("`{v}` is stored in field `{f}`"));
1241
+ self.operand(val, live, || format!("`{v}` is stored in field `{f}`"));
1233
1242
  }
1234
1243
  }
1235
1244
  } else {
1236
1245
  for t in &a.targets {
1237
1246
  match t {
1238
- AssignTarget::Var(n) if *n == v => self.fail(format!("`{v}` is rebound in a multi-assign")),
1247
+ AssignTarget::Var(n) if *n == v => self.failWith(|| format!("`{v}` is rebound in a multi-assign")),
1239
1248
  AssignTarget::Var(_) => {}
1240
1249
  AssignTarget::Field(o, _) => {
1241
1250
  if !self.bare(o) {
@@ -1245,7 +1254,7 @@ impl<'a> Scan<'a> {
1245
1254
  }
1246
1255
  }
1247
1256
  for val in &a.values {
1248
- self.operand(val, true, format!("`{v}` is also held through a multi-assign"));
1257
+ self.operand(val, true, || format!("`{v}` is also held through a multi-assign"));
1249
1258
  }
1250
1259
  }
1251
1260
  }
@@ -1275,7 +1284,7 @@ impl<'a> Scan<'a> {
1275
1284
  for (k, sub) in m.subjects.iter().enumerate() {
1276
1285
  if self.bare(sub) {
1277
1286
  if m.cases.iter().any(|c| matches!(c.patterns.get(k), Some(CasePattern::Name(_)))) {
1278
- self.fail(format!("`{v}` is also held by a pattern binding"));
1287
+ self.failWith(|| format!("`{v}` is also held by a pattern binding"));
1279
1288
  }
1280
1289
  // A downcast `T as o` moves the value into `o` only if `v` is dead in
1281
1290
  // that arm and after the match; otherwise `o` is a second holder.
@@ -1283,7 +1292,7 @@ impl<'a> Scan<'a> {
1283
1292
  matches!(c.patterns.get(k), Some(CasePattern::As { .. }))
1284
1293
  && (live || self.mentionsBlock(&c.body) || c.guard.as_ref().is_some_and(|g| self.mentions(g)))
1285
1294
  }) {
1286
- self.fail(format!("`{v}` is also held by a downcast binding"));
1295
+ self.failWith(|| format!("`{v}` is also held by a downcast binding"));
1287
1296
  }
1288
1297
  } else {
1289
1298
  self.expr(sub, false, live || inCases || m.subjects.iter().skip(k + 1).any(|x| self.mentions(x)));
@@ -1306,13 +1315,13 @@ impl<'a> Scan<'a> {
1306
1315
  }
1307
1316
 
1308
1317
  fn call(&mut self, c: &FnCall, live: bool) {
1309
- let v = self.v.to_string();
1318
+ let v: &'a str = self.v;
1310
1319
  let g = self.an.isFree(&c.name);
1311
1320
  let bareCount = c.args.iter().filter(|a| self.bare(argExpr(a))).count();
1312
1321
  // `@sameRef(v, w)` compares `v` by identity: had `v`'s cell been written in place,
1313
1322
  // `w` (its new version) would be the same object with other contents.
1314
1323
  if bareCount > 0 && c.name == crate::compile_builtins::SAME_REF_FN {
1315
- self.fail(format!("`{v}` is compared by identity (`@sameRef`), so it is never updated in place"));
1324
+ self.failWith(|| format!("`{v}` is compared by identity (`@sameRef`), so it is never updated in place"));
1316
1325
  return;
1317
1326
  }
1318
1327
  for (k, a) in c.args.iter().enumerate() {
@@ -1323,7 +1332,7 @@ impl<'a> Scan<'a> {
1323
1332
  && !lv
1324
1333
  && g.is_some_and(|g| self.an.paramPos(g, c, k).is_some_and(|j| self.an.handOk(g, j)));
1325
1334
  if !ok {
1326
- self.fail(format!("`{v}` is passed to `{}`, which may keep it or the caller reads it afterwards", c.name));
1335
+ self.failWith(|| format!("`{v}` is passed to `{}`, which may keep it or the caller reads it afterwards", c.name));
1327
1336
  }
1328
1337
  } else {
1329
1338
  self.expr(e, false, lv);
@@ -1335,7 +1344,7 @@ impl<'a> Scan<'a> {
1335
1344
  /// hand-over to every method named `m` (of the receiver's type, when known), so all of them
1336
1345
  /// must take that parameter uniquely.
1337
1346
  fn methodCall(&mut self, a: &AttributeExpr, c: &FnCall, live: bool) {
1338
- let v = self.v.to_string();
1347
+ let v: &'a str = self.v;
1339
1348
  let cands: Vec<usize> = self.an.cands(c);
1340
1349
  let recvBare = self.bare(&a.object);
1341
1350
  let bareCount = recvBare as usize + c.args.iter().filter(|x| self.bare(argExpr(x))).count();
@@ -1349,7 +1358,7 @@ impl<'a> Scan<'a> {
1349
1358
  let inArgs = c.args.iter().any(|x| self.mentions(argExpr(x)));
1350
1359
  if recvBare {
1351
1360
  if !readOnly && !handOver(self, None, live || inArgs) {
1352
- self.fail(format!("`{v}` is the receiver of `{}`, which may keep it or the caller reads it afterwards", c.name));
1361
+ self.failWith(|| format!("`{v}` is the receiver of `{}`, which may keep it or the caller reads it afterwards", c.name));
1353
1362
  }
1354
1363
  } else {
1355
1364
  self.expr(&a.object, false, live || inArgs);
@@ -1359,7 +1368,7 @@ impl<'a> Scan<'a> {
1359
1368
  let lv = live || c.args[k + 1..].iter().any(|y| self.mentions(argExpr(y)));
1360
1369
  if self.bare(e) {
1361
1370
  if !handOver(self, Some(k), lv) {
1362
- self.fail(format!("`{v}` is passed to method `{}`, which may keep it or the caller reads it afterwards", c.name));
1371
+ self.failWith(|| format!("`{v}` is passed to method `{}`, which may keep it or the caller reads it afterwards", c.name));
1363
1372
  }
1364
1373
  } else {
1365
1374
  self.expr(e, false, lv);
@@ -1370,7 +1379,7 @@ impl<'a> Scan<'a> {
1370
1379
  /// `tail`: the value of `e` is the result of the unit. `live`: the variable is mentioned after
1371
1380
  /// `e` has run.
1372
1381
  fn expr(&mut self, e: &Expr, tail: bool, live: bool) {
1373
- let v = self.v.to_string();
1382
+ let v: &'a str = self.v;
1374
1383
  match &e.kind {
1375
1384
  ExprKind::Var(n) if *n == v => {
1376
1385
  if !tail || live {
@@ -1399,7 +1408,7 @@ impl<'a> Scan<'a> {
1399
1408
  match &cc.spread {
1400
1409
  Some(s) if self.bare(s) => {
1401
1410
  if live {
1402
- self.fail(format!("`{v}` is read after the spread"));
1411
+ self.failWith(|| format!("`{v}` is read after the spread"));
1403
1412
  }
1404
1413
  }
1405
1414
  Some(s) => self.expr(s, false, live),
@@ -1415,7 +1424,7 @@ impl<'a> Scan<'a> {
1415
1424
  if self.bare(&fa.value) && !lv && !self.strict {
1416
1425
  continue;
1417
1426
  }
1418
- self.operand(&fa.value, lv, format!("`{v}` is stored in field `{}`", fa.name));
1427
+ self.operand(&fa.value, lv, || format!("`{v}` is stored in field `{}`", fa.name));
1419
1428
  }
1420
1429
  }
1421
1430
  ExprKind::Attribute(a) => match &a.attr {
@@ -1428,7 +1437,7 @@ impl<'a> Scan<'a> {
1428
1437
  },
1429
1438
  ExprKind::Closure(_) => {
1430
1439
  if self.mentions(e) {
1431
- self.fail(format!("`{v}` is captured by a closure"));
1440
+ self.failWith(|| format!("`{v}` is captured by a closure"));
1432
1441
  }
1433
1442
  }
1434
1443
  ExprKind::Binary(b) => {