[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

#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
participants (1)
-
GHC