Skip to content

Diagnostics: a failed Display bound cascades into an error inside embedded std/builtins.w; the ++-in-loop warning points at 1:1 in an implicit-main script #1447

Closed

Description

@ehartford

Summary

Two diagnostic-quality defects seen while working on #1430 (origin/main 1c76062, base stage2):

1. A user error cascades into a second error inside the embedded stdlib

echo abc | with -n 'let m = /(a)/.find(line); if m.is_some(): print(m.unwrap())'
error: type 'Match' does not implement trait 'Display' required by bound 'T: Display'
 --> <cli -n #1>:2:22
2 |  if m.is_some(): print(m.unwrap())
  |                      ^^^^^^^^^^^^^^^^^

error: unknown method 'to_str' for type '&Match'
 --> <embedded-std>/std/builtins.w:40:26
40 |         with_println_str(v.to_str())
  |                          ^^^^^^^^^^

The first error is the whole story; the second is print[T: Display]'s body checked with the failed instantiation. A failed bound should stop the instantiation, not report the generic body's consequences in std source the user never wrote.

2. The "++ inside a loop" warning points at 1:1

reorder.w (an implicit-main script with top-level var accumulators, a helper fn, and a for loop doing cur = cur ++ l ++ "\n"):

use std.fs
use std.process

let argv = args()
let text = read_file(argv.get(1)).unwrap()
var uses = ""
var types = ""
var rest = ""
var cur = ""
var cur_kind = 0
fn kind_of(l: &str) -> i32:
    if l.starts_with("use "): return 1
    if l.starts_with("type ") or l.starts_with("enum ") or l.starts_with("error "): return 2
    3
for l in text.split("\n"):
    let top = l.len() > 0 and not l.starts_with(" ")
    if top:
        if cur_kind == 1: uses = uses ++ cur
        else if cur_kind == 2: types = cur ++ types
        else: rest = rest ++ cur
        cur = ""
        cur_kind = kind_of(l)
    cur = cur ++ l ++ "\n"
print(uses ++ types ++ rest)
warning: string concatenation with ++ inside a loop repeatedly copies the accumulator; use StringBuilder or collect pieces and concatenate once
 --> reorder.w:1:1
1 |
  | ^^^^^^^^^^^^^^^^^^^^

The span is not the ++ (and the quoted line is empty although line 1 is use std.fs). A three-line script with the same loop and no helper fn does not warn at all, so the warning's span (and possibly its trigger) comes from the synthesized implicit-main body.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions