[GHC] #10982: Warn about unused pattern variables in type families
#10982: Warn about unused pattern variables in type families -------------------------------------+------------------------------------- Reporter: jstolarek | Owner: Type: feature | Status: new request | Priority: normal | Milestone: Component: Compiler | Version: 7.11 Keywords: newcomer | Operating System: Unknown/Multiple Architecture: | Type of failure: None/Unknown Unknown/Multiple | Test Case: | Blocked By: Blocking: | Related Tickets: Differential Rev(s): | Wiki Page: -------------------------------------+------------------------------------- Now that we have wildcards in type families (#3699) I want to be warned about unused pattern variables in the same way I am warned about them at the term level. For example this should cause a warning that `b` is not used: {{{#!hs type family F a b type instance F a b = a }}} Prefixing unused variable with an underscore should suppress the warning: {{{#!hs type family F a b type instance F a _b = a }}} -- Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/10982> GHC <http://www.haskell.org/ghc/> The Glasgow Haskell Compiler
#10982: Warn about unused pattern variables in type families -------------------------------------+------------------------------------- Reporter: jstolarek | Owner: msosn Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: 7.11 Resolution: | Keywords: newcomer Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Changes (by msosn): * owner: => msosn -- Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/10982#comment:1> GHC <http://www.haskell.org/ghc/> The Glasgow Haskell Compiler
#10982: Warn about unused pattern variables in type families -------------------------------------+------------------------------------- Reporter: jstolarek | Owner: msosn Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: 7.11 Resolution: | Keywords: newcomer Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by msosn): I'm looking into how the NamedWildCards extension causes programs to be rejected. I noticed that with the extension enabled, using named wildcards as types outside the type signatures (where they behave as documented in Partial Type Signatures section of the user's guide) causes errors. The code below is accepted when the extension is disabled, because the identifiers starting with _ are parsed as ordinary type variables. From what I read on the ghc mailing list, the type families should also be accepted with the NamedWildCards on. If so, shouldn't the wildcards be treated as type variables in the other cases as well? {{{ {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE NamedWildCards #-} module FailsWithNWCsEnabled where type Synonym _a = _a -> _a -- "Unexpected type '_a' In the type declaration for 'Synonym'" data A a _b = ACon a a -- "Unexpected type '_b' In the data declaration for 'A'" data B _a b = BCon _a _a -- "Unexpected type '_a' In the data declaration for 'B'" type family C a b where C a _b = a -> a -- "Wildcard '_b' not allowed in a type pattern of family instance for 'C'" type family D a b where D _a b = _a -> _a -- "Wildcard '_a' not allowed in a type pattern of family instance for 'D'" -- "Wildcard '_a' not allowed in the declaration for type synonym 'D'" (twice) data family E a b data instance E a _b = ECon a a -- "Wildcard '_b' not allowed in a type pattern of family instance for 'E'" data family F a b data instance F _a b = FCon _a _a -- "Wildcard '_a' not allowed in a type pattern of family instance for 'F'" -- "Wildcard '_a' not allowed in the definition of data constructor 'FCon'" (twice) }}} I also wonder if the warnings in type families should be enabled with the -fwarn-unused-matches option or should I make a new flag? -- Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/10982#comment:2> GHC <http://www.haskell.org/ghc/> The Glasgow Haskell Compiler
#10982: Warn about unused pattern variables in type families -------------------------------------+------------------------------------- Reporter: jstolarek | Owner: msosn Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: 7.11 Resolution: | Keywords: newcomer Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by jstolarek): Replying to [comment:2 msosn]:
The code below is accepted when the extension is disabled, because the identifiers starting with _ are parsed as ordinary type variables. From what I read on the ghc mailing list, the type families should also be accepted with the NamedWildCards on. If so, shouldn't the wildcards be treated as type variables in the other cases as well? I think you're right. I mean if this code is valid when `NamedWildCards` are turned off, then it certainly should be valid when we enable `NamedWildCards`. After all named wild cards can't possibly appear in any of these declarations, so whenever a name that looks like a wildcard appears we can safely assume that it must be a type variable. Note however, that we only want to generate warnings for unused pattern variables in type families.
I also wonder if the warnings in type families should be enabled with the -fwarn-unused-matches option or should I make a new flag? I say we use the existing flag. If anyone complains that they want a separate flag for unused type-level patterns then we can add it.
-- Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/10982#comment:3> GHC <http://www.haskell.org/ghc/> The Glasgow Haskell Compiler
#10982: Warn about unused pattern variables in type families -------------------------------------+------------------------------------- Reporter: jstolarek | Owner: msosn Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: 7.11 Resolution: | Keywords: newcomer Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Phab:D1576 Wiki Page: | -------------------------------------+------------------------------------- Changes (by msosn): * differential: => Phab:D1576 -- Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/10982#comment:4> GHC <http://www.haskell.org/ghc/> The Glasgow Haskell Compiler
#10982: Warn about unused pattern variables in type families -------------------------------------+------------------------------------- Reporter: jstolarek | Owner: msosn Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: 7.11 Resolution: | Keywords: newcomer Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Phab:D1576 Wiki Page: | -------------------------------------+------------------------------------- Comment (by Jan Stolarek <jan.stolarek@…>): In [changeset:"be92c288839e0bfcf0b15e3bb4136d60f8ef2575/ghc" be92c288/ghc]: {{{ #!CommitTicketReference repository="ghc" revision="be92c288839e0bfcf0b15e3bb4136d60f8ef2575" Update hoopl submodule Hoopl changes required to implement #10982 }}} -- Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/10982#comment:5> GHC <http://www.haskell.org/ghc/> The Glasgow Haskell Compiler
#10982: Warn about unused pattern variables in type families -------------------------------------+------------------------------------- Reporter: jstolarek | Owner: msosn Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: 7.11 Resolution: | Keywords: newcomer Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Phab:D1576 Wiki Page: | -------------------------------------+------------------------------------- Comment (by simonpj): This is a bit fiddly. Here are some facts about the current implementation: * Named wildcards are part of the abstract syntax of types; see the `HsWildCardTy` constructor of `HsType` and the data type `HsTypes.WildCardInfo`. * If `-XNamedWildCards` is on, we make a `NamedWildCard` for any `_v` in a type; see `Parser.y` and the call to `mkNamedWildCardTy`. That puts a `NamedWildCard` in many places that it does not "belong", which is what gives rise to the fallout in comment:2 The Right Thing is probably not to put `HsWildCardTy` into types where they aren't allowed. Really they should only show up in `LHsSigWcType`. So I suppose that one simple thing would be to parse wildcards as `HsTyVar`, and then either * Make `mkLHsSigWcType` traverse the type and replace appropriate `HsTyVars` with `HsWildCardTy`, or * Make the renamer do this job. Probably the latter is best. Simon -- Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/10982#comment:6> GHC <http://www.haskell.org/ghc/> The Glasgow Haskell Compiler
#10982: Warn about unused pattern variables in type families -------------------------------------+------------------------------------- Reporter: jstolarek | Owner: msosn Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: 7.11 Resolution: | Keywords: newcomer Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: #11098 | Differential Rev(s): Phab:D1576 Wiki Page: | -------------------------------------+------------------------------------- Changes (by jstolarek): * related: => #11098 Comment: This looks like a manifestation of #11098. I tried fixing it by "making renamer do its job" but got stuck because I didn't know how to tell whether a type variable was introduced using a forall or not - see [ticket:11098#comment:3]. But this was before wildcard refactor patch (Phab:D1428). Now, looking at Note [HsType binders] in HsTypes, I think it is easy to tell whether a type variable was quantified explicitly or not. Michał, does this allow you to make progress? -- Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/10982#comment:7> GHC <http://www.haskell.org/ghc/> The Glasgow Haskell Compiler
#10982: Warn about unused pattern variables in type families -------------------------------------+------------------------------------- Reporter: jstolarek | Owner: msosn Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: 7.11 Resolution: | Keywords: newcomer Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: | UnusedTyVarWarnings, | UnusedTyVarWarningsNamedWCs Blocked By: | Blocking: Related Tickets: #11098 | Differential Rev(s): Phab:D1576 Wiki Page: | -------------------------------------+------------------------------------- Changes (by msosn): * testcase: => UnusedTyVarWarnings, UnusedTyVarWarningsNamedWCs -- Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/10982#comment:8> GHC <http://www.haskell.org/ghc/> The Glasgow Haskell Compiler
#10982: Warn about unused pattern variables in type families -------------------------------------+------------------------------------- Reporter: jstolarek | Owner: msosn Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: 7.11 Resolution: | Keywords: newcomer Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: | UnusedTyVarWarnings, | UnusedTyVarWarningsNamedWCs Blocked By: | Blocking: Related Tickets: #11098 | Differential Rev(s): Phab:D1576 Wiki Page: | -------------------------------------+------------------------------------- Comment (by Ben Gamari <ben@…>): In [changeset:"eb7796f13e701cce4e7d1d86f36c966aa17f1e9c/ghc" eb7796f/ghc]: {{{ #!CommitTicketReference repository="ghc" revision="eb7796f13e701cce4e7d1d86f36c966aa17f1e9c" Warn about unused type variables in type families The warnings are enabled with the flag -fwarn-unused-matches, the same one that enables warnings on the term level. Identifiers starting with an underscore are now always parsed as type variables. When the NamedWildCards extension is enabled, the renamer replaces those variables with named wildcards. An additional NameSet nwcs is added to LocalRdrEnv. It's used to keep names of the type variables that should be replaced with wildcards. While renaming HsForAllTy, when a name is explicitly bound it is removed from the nwcs NameSet. As a result, the renamer doesn't replace them in the quantifier body. (Trac #11098) Fixes #10982, #11098 Reviewers: alanz, bgamari, hvr, austin, jstolarek Reviewed By: jstolarek Subscribers: goldfire, mpickering, RyanGlScott, thomie Differential Revision: https://phabricator.haskell.org/D1576 GHC Trac Issues: #10982 }}} -- Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/10982#comment:9> GHC <http://www.haskell.org/ghc/> The Glasgow Haskell Compiler
#10982: Warn about unused pattern variables in type families -------------------------------------+------------------------------------- Reporter: jstolarek | Owner: msosn Type: feature request | Status: closed Priority: normal | Milestone: Component: Compiler | Version: 7.11 Resolution: fixed | Keywords: newcomer Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: | UnusedTyVarWarnings, | UnusedTyVarWarningsNamedWCs Blocked By: | Blocking: Related Tickets: #11098 | Differential Rev(s): Phab:D1576 Wiki Page: | -------------------------------------+------------------------------------- Changes (by bgamari): * status: new => closed * resolution: => fixed -- Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/10982#comment:10> GHC <http://www.haskell.org/ghc/> The Glasgow Haskell Compiler
#10982: Warn about unused pattern variables in type families -------------------------------------+------------------------------------- Reporter: jstolarek | Owner: msosn Type: feature request | Status: closed Priority: normal | Milestone: Component: Compiler | Version: 7.11 Resolution: fixed | Keywords: newcomer Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: | UnusedTyVarWarnings, | UnusedTyVarWarningsNamedWCs Blocked By: | Blocking: Related Tickets: #11098 | Differential Rev(s): Phab:D1576 Wiki Page: | -------------------------------------+------------------------------------- Comment (by Simon Peyton Jones <simonpj@…>): In [changeset:"575a98e4d245c1e60526ed6d6711d96cea08e9d2/ghc" 575a98e4/ghc]: {{{ #!CommitTicketReference repository="ghc" revision="575a98e4d245c1e60526ed6d6711d96cea08e9d2" Refactor named wildcards (again) Michal's work on #10982, #11098, refactored the handling of named wildcards by making them more like ordinary type variables. This patch takes the same idea to its logical conclusion, resulting in a much tidier, tighter implementation. Read Note [The wildcard story for types] in HsTypes. Changes: * Named wildcards are ordinary type variables, throughout * HsType no longer has a data constructor for named wildcards (was NamedWildCard in HsWildCardInfo). Named wildcards are simply HsTyVars * Similarly named wildcards disappear from Template Haskell * I refactored RnTypes to avoid polluting LocalRdrEnv with something as narrow as named wildcards. Instead the named wildcard set is carried in RnTyKiEnv. There is a submodule update for Haddock. }}} -- Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/10982#comment:11> GHC <http://www.haskell.org/ghc/> The Glasgow Haskell Compiler
participants (1)
-
GHC