Skip to content

Impossible message type created by Html.map #119

@alexgig

Description

@alexgig

Hi Evan,

I've come across an issue where an impossible message type is created during runtime which then causes a non-matching pattern in a case statement to run.

I've included a simple example below to illustrate the issue. It can be run using elm-reactor and both the Debug view and browser Console illustrate the issue.

The example is a simple 2 page app that allows a user to change from page A to B. When the page is changed to B, an onBlur event from page A is errantly tagged as BMsg ABlur which should be impossible. This impossible msg then triggers the last pattern in the BMsg case to be called since no match was found.

module Main exposing (..)

import Html exposing (Html, button, text)
import Html.Events exposing (onClick, onBlur)


main : Program Never Model Msg
main =
    Html.program
        { init = init
        , update = update
        , subscriptions = always Sub.none
        , view = view
        }


type alias Model =
    { page : Page }


type Page
    = A
    | B


type Msg
    = ShowPageB
    | AMsg AMsg
    | BMsg BMsg


type AMsg
    = AClick
    | ABlur


type BMsg
    = NoOp


init : ( Model, Cmd Msg )
init =
    { page = A } ! []


update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
    case Debug.log "msg" msg of
        AMsg msg ->
            case msg of
                AClick ->
                    update ShowPageB model

                ABlur ->
                    Debug.crash "Never gets here because ABlur message is mapped as `BMsg ABlur` instead of `AMsg ABlur`"

        ShowPageB ->
            ( { model | page = B }
            , Cmd.none
            )

        BMsg msg ->
            case msg of
                NoOp ->
                    Debug.log "Gets here because no match for `BMsg ABlur` so last match runs by default." <|
                        ( model, Cmd.none )


view : Model -> Html Msg
view model =
    case model.page of
        A ->
            button [ onClick AClick, onBlur ABlur ] [ text "Show Page B" ]
                |> Html.map AMsg

        B ->
            -- NOTE this is where Page A's on blur message is getting mis-tagged!!!
            text "Page B"
                |> Html.map BMsg

OS: macOS Sierra - Version 10.12.3
Browser: Google Chrome - Version 56.0.2924.87 (64-bit)
Elm: 0.18.0
elm-lang/html: 2.0.0

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