[GHC] #11098: TH mishandles type variables that begin with an underscore
#11098: TH mishandles type variables that begin with an underscore -------------------------------------+------------------------------------- Reporter: goldfire | Owner: Type: bug | Status: new Priority: normal | Milestone: 8.0.1 Component: Template | Version: 7.10.2 Haskell | Keywords: | Operating System: Unknown/Multiple Architecture: | Type of failure: None/Unknown Unknown/Multiple | Test Case: | Blocked By: Blocking: | Related Tickets: Differential Rev(s): | Wiki Page: -------------------------------------+------------------------------------- The following GHCi exchange was reported [https://github.com/goldfirere /th-desugar/pull/36 here] (you need not follow the link to understand this ticket, though). {{{ Prelude Language.Haskell.TH> runQ [d|foo :: forall _a . _a -> _a;foo x = x|] [SigD foo_6 (ForallT [PlainTV _a_5] [] (AppT (AppT ArrowT (WildCardT (Just _a_5))) (WildCardT (Just _a_5)))),FunD foo_6 [Clause [VarP x_7] (NormalB (VarE x_7)) []]] }}} The problem is that the quoted expression uses no wildcards, yet the TH output does. That quoted expression just uses a type variable that begins with an underscore. Such an expression was allowed in Haskell long before wildcards. -- Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/11098> GHC <http://www.haskell.org/ghc/> The Glasgow Haskell Compiler
#11098: TH mishandles type variables that begin with an underscore -------------------------------------+------------------------------------- Reporter: goldfire | Owner: Type: bug | Status: new Priority: normal | Milestone: 8.0.1 Component: Template Haskell | Version: 7.10.2 Resolution: | Keywords: 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 jstolarek): * cc: jstolarek (added) Comment: I just tried with GHC 7.8.4, 7.10.1 and today's HEAD and I can't reproduce this behaviour: {{{ [killy@GLaDOS : /dane/projekty/ghc/ghc-tests] /dane/sandboxes/ghc/7.10.1/bin/ghci GHCi, version 7.10.1: http://www.haskell.org/ghc/ :? for help Prelude> :m + Language.Haskell.TH Prelude Language.Haskell.TH> :set -XTemplateHaskell Prelude Language.Haskell.TH> :set -XRankNTypes Prelude Language.Haskell.TH> runQ [d|foo :: forall _a . _a -> _a ; foo x = x|] [SigD foo_1 (ForallT [PlainTV _a_0] [] (AppT (AppT ArrowT (VarT _a_0)) (VarT _a_0))),FunD foo_1 [Clause [VarP x_2] (NormalB (VarE x_2)) []]] }}} Can you post your whole GHCi session and `~/.ghci`? Or perhaps this bug is specific to 7.10.2, but that would be surprising. -- Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/11098#comment:1> GHC <http://www.haskell.org/ghc/> The Glasgow Haskell Compiler
#11098: TH mishandles type variables that begin with an underscore -------------------------------------+------------------------------------- Reporter: goldfire | Owner: jstolarek Type: bug | Status: new Priority: normal | Milestone: 8.0.1 Component: Template Haskell | Version: 7.10.2 Resolution: | Keywords: 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 jstolarek): * owner: => jstolarek Comment: I just looked at the original report on th-desugar issue tracker. I see the problem now. Will look into it, though I think the problem is not in the TH implementation (might be wrong though). -- Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/11098#comment:2> GHC <http://www.haskell.org/ghc/> The Glasgow Haskell Compiler
#11098: TH mishandles type variables that begin with an underscore -------------------------------------+------------------------------------- Reporter: goldfire | Owner: jstolarek Type: bug | Status: new Priority: normal | Milestone: 8.0.1 Component: Template Haskell | Version: 7.10.2 Resolution: | Keywords: 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): The problem arises in the parser, where we assume that when named wild cards are enabled then any type variables that starts with an underscore is a wild card: {{{ | tyvar {% do { nwc <- namedWildCardsEnabled -- (See Note [Unit tuples]) ; let tv@(Unqual name) = unLoc $1 ; return $ if (startsWithUnderscore name && nwc) then (sL1 $1 (mkNamedWildCardTy tv)) else (sL1 $1 (HsTyVar tv)) } } }}} My idea for the solution is to unconditionally create an `HsTyVar` in the parser and then be smart during renaming: {{{#!hs rnHsTyKi isType doc (HsTyVar rdr_name) = do { dflags <- getDynFlags ; let is_wild_card_name = startsWithUnderscore (rdrNameOcc rdr_name) wild_cards_enabled = xopt Opt_NamedWildCards dflags ; is_in_scope <- isJust `fmap` lookupOccRn_maybe rdr_name ; if is_wild_card_name && not is_in_scope && wild_cards_enabled then rnHsTyKi isType doc (HsWildCardTy (NamedWildCard rdr_name)) else do { name <- rnTyVar isType rdr_name ; return (HsTyVar name, unitFV name) } } }}} That however does not work - the names of variables seem to be in scope even when they were not explicitly declared using a `forall`. So the question is: how do I detect if a variable was introduced using a `forall`? -- Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/11098#comment:3> GHC <http://www.haskell.org/ghc/> The Glasgow Haskell Compiler
#11098: TH mishandles type variables that begin with an underscore -------------------------------------+------------------------------------- Reporter: goldfire | Owner: jstolarek Type: bug | Status: new Priority: normal | Milestone: 8.0.1 Component: Template Haskell | Version: 7.10.2 Resolution: | Keywords: 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 darchon): Indeed, I was just about to comment that it is not just TH. When I compile {{{ {-# LANGUAGE PartialTypeSignatures, NamedWildCards, ExplicitForAll #-} module Bar where foo :: _a -> _a foo x = x bar :: forall _a . _a -> _a bar x = x }}} I get: {{{ $ ghc -fforce-recomp Bar.hs [1 of 1] Compiling Bar ( Bar.hs, Bar.o ) Bar.hs:4:8: warning: Found type wildcard '_a' standing for 't' Where: 't' is a rigid type variable bound by the inferred type of foo :: t -> t at Bar.hs:5:1 In the type signature for: foo :: _a -> _a Bar.hs:7:20: warning: Found type wildcard '_a' standing for 't0' Where: 't0' is an ambiguous type variable In the type signature for: bar :: forall _a. _a -> _a }}} -- Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/11098#comment:4> GHC <http://www.haskell.org/ghc/> The Glasgow Haskell Compiler
#11098: PartialTypeSignatures mishandles type variables that begin with an underscore -------------------------------------+------------------------------------- Reporter: goldfire | Owner: jstolarek Type: bug | Status: new Priority: normal | Milestone: 8.0.1 Component: Template Haskell | Version: 7.10.2 Resolution: | Keywords: 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 goldfire): * cc: thomasw (added) Comment: Good observations. Perhaps we should ask the partial type signatures folks. -- Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/11098#comment:5> GHC <http://www.haskell.org/ghc/> The Glasgow Haskell Compiler
#11098: PartialTypeSignatures mishandles type variables that begin with an underscore -------------------------------------+------------------------------------- Reporter: goldfire | Owner: Type: bug | Status: new Priority: normal | Milestone: 8.0.1 Component: Template Haskell | Version: 7.10.2 Resolution: | Keywords: 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 jstolarek): * owner: jstolarek => -- Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/11098#comment:6> GHC <http://www.haskell.org/ghc/> The Glasgow Haskell Compiler
#11098: PartialTypeSignatures mishandles type variables that begin with an underscore -------------------------------------+------------------------------------- Reporter: goldfire | Owner: msosn Type: bug | Status: new Priority: normal | Milestone: 8.0.1 Component: Template Haskell | Version: 7.10.2 Resolution: | Keywords: 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/11098#comment:7> GHC <http://www.haskell.org/ghc/> The Glasgow Haskell Compiler
#11098: PartialTypeSignatures mishandles type variables that begin with an underscore -------------------------------------+------------------------------------- Reporter: goldfire | Owner: msosn Type: bug | Status: new Priority: normal | Milestone: 8.0.1 Component: Template Haskell | Version: 7.10.2 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: T11098, | NamedWildcardsAsTyVars, | NamedWildcardExplicitForall Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Phab:D1576 Wiki Page: | -------------------------------------+------------------------------------- Changes (by msosn): * testcase: => T11098, NamedWildcardsAsTyVars, NamedWildcardExplicitForall * differential: => Phab:D1576 -- Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/11098#comment:8> GHC <http://www.haskell.org/ghc/> The Glasgow Haskell Compiler
#11098: PartialTypeSignatures mishandles type variables that begin with an underscore -------------------------------------+------------------------------------- Reporter: goldfire | Owner: msosn Type: bug | Status: new Priority: normal | Milestone: 8.0.1 Component: Template Haskell | Version: 7.10.2 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: T11098, | NamedWildcardsAsTyVars, | NamedWildcardExplicitForall Blocked By: | Blocking: Related Tickets: | 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/11098#comment:9> GHC <http://www.haskell.org/ghc/> The Glasgow Haskell Compiler
#11098: PartialTypeSignatures mishandles type variables that begin with an underscore -------------------------------------+------------------------------------- Reporter: goldfire | Owner: msosn Type: bug | Status: closed Priority: normal | Milestone: 8.0.1 Component: Template Haskell | Version: 7.10.2 Resolution: fixed | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: T11098, | NamedWildcardsAsTyVars, | NamedWildcardExplicitForall Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Phab:D1576 Wiki Page: | -------------------------------------+------------------------------------- Changes (by msosn): * status: new => closed * resolution: => fixed -- Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/11098#comment:10> GHC <http://www.haskell.org/ghc/> The Glasgow Haskell Compiler
#11098: PartialTypeSignatures mishandles type variables that begin with an underscore -------------------------------------+------------------------------------- Reporter: goldfire | Owner: msosn Type: bug | Status: closed Priority: normal | Milestone: 8.0.1 Component: Template Haskell | Version: 7.10.2 Resolution: fixed | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: T11098, | NamedWildcardsAsTyVars, | NamedWildcardExplicitForall Blocked By: | Blocking: Related Tickets: | 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/11098#comment:11> GHC <http://www.haskell.org/ghc/> The Glasgow Haskell Compiler
participants (1)
-
GHC