asteroid icon indicating copy to clipboard operation
asteroid copied to clipboard

Empty "do" statement list causes student and redundant pattern detector confusion

Open davidhbrown-uri opened this issue 1 year ago • 1 comments

The following code is a simplification of a conceptual error I've seen in student code when learning multiple dispatch:

function foo with bar do
    with baz do 0 
end

Awkwardly, asteroid reports "error: list index out of range" on the second line of the function. This is not particularly informative as there are no lists in the asteroid code.

Turning on Python exceptions with -e shows that this actually came from:

.../asteroid/walk.py, line 1994, in check_redundancy
    first_line_h = LINE_LIST[0]

Turning off the redundant pattern detector with -r lets the function work with the empty statement list (in Asteroid 2.0.2), returning none. Here's a more useful variation on the theme:

load system io.
function foo with 0 do
    with 1 do "not none"
end
io@println(tostring(foo(0))).
io@println(tostring(foo(1))).

(With the -r flag, prints none and not none.)

So, it does seem to be a legal program, just one which confuses the pattern redundancy detector, and that error confuses students.

I haven't looked enough at the codebase to know how difficult it would be to issue a warning if a program's AST includes an empty statement list, but that could be helpful.

davidhbrown-uri avatar Oct 11 '24 21:10 davidhbrown-uri

We need to fix Asteroid's grammar to disallow with clauses with no statements.

As a workaround, simply put the 'noop' statement -- a single period.

load system io.
function foo with 0 do .  <<<
    with 1 do "not none"
end
io@println(tostring(foo(0))).
io@println(tostring(foo(1))).

lutzhamel avatar Oct 12 '24 11:10 lutzhamel