Skip to content

Commit

Permalink
Fix minor bug with exception replication
Browse files Browse the repository at this point in the history
An exception replication `exception <vid> = <longvid>` requires that
`<longvid>` have exception status, but MLton was allowing an
identifier with either (non-exn) constructor or exception status:

    exception E_NONE = NONE
    exception E_SOME = SOME

    fun f x =
       case x of
          E_NONE => ()
        | E_SOME () => ()

This program would compile without errors.  (Curiously, `E_NONE` and
`E_SOME` were added to the environment with exception status, but this
seems to have little effect on elaboration.)

Now, an exception replication properly checks that `<longvid>` has
exception status:

    Error: z.sml 1.20-1.23.
      Undefined exception: NONE.
    Error: z.sml 2.20-2.23.
      Undefined exception: SOME.
    Error: z.sml 7.7-7.12.
      Undefined constructor: E_SOME.
  • Loading branch information
MatthewFluet committed Aug 4, 2017
1 parent 77d5044 commit 1c89c42
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion mlton/elaborate/elaborate-core.fun
Original file line number Diff line number Diff line change
Expand Up @@ -2052,7 +2052,7 @@ fun elaborateDec (d, {env = E, nest}) =
val decs =
case EbRhs.node rhs of
EbRhs.Def c =>
(case Env.lookupLongcon (E, c) of
(case Env.lookupLongexn (E, c) of
NONE => decs
| SOME (exn', scheme) =>
let
Expand Down
17 changes: 17 additions & 0 deletions mlton/elaborate/elaborate-env.fun
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,10 @@ structure Vid =
| Exn c => SOME c
| _ => NONE

val deExn =
fn Exn c => SOME c
| _ => NONE

val class =
fn Con _ => Class.Con
| Exn _ => Class.Exn
Expand Down Expand Up @@ -708,6 +712,7 @@ structure Structure =
| SOME (vid, s) => Option.map (de vid, fn z => (z, s))
in
val peekCon = make (Ast.Vid.fromCon, Vid.deCon)
val peekExn = make (Ast.Vid.fromCon, Vid.deExn)
val peekVar = make (Ast.Vid.fromVar, Vid.deVar)
end

Expand Down Expand Up @@ -1774,6 +1779,12 @@ fun peekCon (T {vals, ...}, c: Ast.Con.t): (Con.t * Scheme.t) option =
NONE => NONE
| SOME (vid, s) => Option.map (Vid.deCon vid, fn c => (c, s))

fun peekExn (T {vals, ...}, c: Ast.Con.t): (Con.t * Scheme.t) option =
case NameSpace.peek (vals, Ast.Vid.fromCon c,
{markUse = fn (vid, _) => isSome (Vid.deExn vid)}) of
NONE => NONE
| SOME (vid, s) => Option.map (Vid.deExn vid, fn c => (c, s))

fun layoutLong (ids: Layout.t list) =
let
open Layout
Expand Down Expand Up @@ -1835,6 +1846,7 @@ in
val peekLongvar = make (Ast.Longvar.split, peekVar, Structure.peekVar)
val peekLongvid = make (Ast.Longvid.split, peekVid, Structure.peekVid)
val peekLongcon = make (Ast.Longcon.split, peekCon, Structure.peekCon)
val peekLongexn = make (Ast.Longcon.split, peekExn, Structure.peekExn)
end

(* ------------------------------------------------- *)
Expand Down Expand Up @@ -1893,6 +1905,11 @@ in
"constructor",
Ast.Longcon.region,
Ast.Longcon.layout)
val lookupLongexn =
make (peekLongexn,
"exception",
Ast.Longcon.region,
Ast.Longcon.layout)
val lookupLongstrid =
make (peekLongstrid,
"structure",
Expand Down
1 change: 1 addition & 0 deletions mlton/elaborate/elaborate-env.sig
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ signature ELABORATE_ENV =
val lookupBasid: t * Ast.Basid.t -> Basis.t option
val lookupFctid: t * Ast.Fctid.t -> FunctorClosure.t option
val lookupLongcon: t * Ast.Longcon.t -> (CoreML.Con.t * Scheme.t) option
val lookupLongexn: t * Ast.Longcon.t -> (CoreML.Con.t * Scheme.t) option
val lookupLongstrid: t * Ast.Longstrid.t -> Structure.t option
val lookupLongtycon: t * Ast.Longtycon.t -> TypeStr.t option
val lookupLongvar: t * Ast.Longvar.t -> (CoreML.Var.t * Scheme.t) option
Expand Down

0 comments on commit 1c89c42

Please sign in to comment.