Skip to content

Commit

Permalink
Merge pull request #1692 from GaloisInc/issue_1691
Browse files Browse the repository at this point in the history
Fixes #1691 (and removes partial match warning)
  • Loading branch information
yav authored Jul 2, 2024
2 parents fbbbb8f + 128411e commit 623ea9b
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 52 deletions.
7 changes: 6 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
# next -- TBA

## Language changes

* Add implicit imports for non-anonymous modules defined by functor
instantiation. For details, see #1691.


## Bug fixes

* Fix #1685, which caused Cryptol to panic when given a local definition without
a type signature that uses numeric constraint guards.

# 3.1.0 -- 2024-02-05

## Language changes
Expand Down
39 changes: 21 additions & 18 deletions src/Cryptol/ModuleSystem/Renamer/ImplicitImports.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ module Cryptol.ModuleSystem.Renamer.ImplicitImports

import Data.List(partition)

import Cryptol.Utils.Ident(identIsNormal, packModName)
import Cryptol.Utils.Panic(panic)
import Cryptol.Parser.Position(Range)
import Cryptol.Utils.Ident(packModName)
import Cryptol.Parser.AST

{- | Add additional imports for modules nested withing this one -}
Expand All @@ -56,25 +57,28 @@ addImplicitNestedImports' decls =


processModule :: TopDecl PName -> ([TopDecl PName], [[Ident]])
processModule ~dcl@(DModule m) =
let NestedModule m1 = tlValue m
in
case mDef m1 of
NormalModule ds ->
let (childExs, ds1) = addImplicitNestedImports' ds
processModule dcl =
case dcl of
DModule m ->
let NestedModule m1 = tlValue m
mname = getIdent (thing (mName m1))
imps = map (mname :) ([] : childExs) -- this & nested
loc = srcRange (mName m1)
in ( DModule m { tlValue = NestedModule m1 { mDef = NormalModule ds1 } }
: map (mkImp loc) imps
, case tlExport m of
Public -> imps
Private -> []
)

FunctorInstance {} -> ([dcl], [])
InterfaceModule {} -> ([dcl], [])
in
case mDef m1 of
_ | not (identIsNormal mname) -> ([dcl],[])
NormalModule ds ->
let (childExs, ds1) = addImplicitNestedImports' ds
imps = map (mname :) ([] : childExs) -- this & nested
in ( DModule m { tlValue = NestedModule m1 { mDef = NormalModule ds1 } }
: map (mkImp loc) imps
, case tlExport m of
Public -> imps
Private -> []
)

FunctorInstance {} -> ([dcl, mkImp loc [mname]], [])
InterfaceModule {} -> ([dcl], [])
_ -> panic "processModule" ["Not a module"]



Expand Down Expand Up @@ -112,4 +116,3 @@ mkImp loc xs =
}



1 change: 1 addition & 0 deletions src/Cryptol/Parser/ParserUtils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1487,6 +1487,7 @@ desugarInstImport i inst =
where
imp = thing i
iname = mkUnqual
$ identAnonIfaceMod
$ mkIdent
$ "import of " <> nm <> " at " <> Text.pack (show (pp (srcRange i)))
where
Expand Down
16 changes: 11 additions & 5 deletions src/Cryptol/Utils/Ident.hs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ module Cryptol.Utils.Ident
, modParamIdent
, identAnonArg
, identAnonIfaceMod
, identAnontInstImport
, identIsNormal

-- * Namespaces
Expand Down Expand Up @@ -169,18 +170,17 @@ instance NFData ModName
modNameArg :: ModName -> ModName
modNameArg (ModName m fl) =
case fl of
NormalName -> ModName m AnonModArgName
AnonModArgName -> panic "modNameArg" ["Name is not normal"]
AnonIfaceModName -> panic "modNameArg" ["Name is not normal"]
NormalName -> ModName m AnonModArgName
_ -> panic "modNameArg" ["Name is not normal"]


-- | Change a normal module name to a module name to be used for an
-- anonnymous interface.
modNameIfaceMod :: ModName -> ModName
modNameIfaceMod (ModName m fl) =
case fl of
NormalName -> ModName m AnonIfaceModName
AnonModArgName -> panic "modNameIfaceMod" ["Name is not normal"]
AnonIfaceModName -> panic "modNameIfaceMod" ["Name is not normal"]
_ -> panic "modNameIfaceMod" ["Name is not normal"]

-- | This is used when we check that the name of a module matches the
-- file where it is defined.
Expand Down Expand Up @@ -351,6 +351,10 @@ identAnonArg (Ident b _ txt) = Ident b AnonModArgName txt
identAnonIfaceMod :: Ident -> Ident
identAnonIfaceMod (Ident b _ txt) = Ident b AnonIfaceModName txt

-- | Make an anonymous identifier for an instantiation in an import.
identAnontInstImport :: Ident -> Ident
identAnontInstImport (Ident b _ txt) = Ident b AnonInstImport txt

identIsNormal :: Ident -> Bool
identIsNormal (Ident _ mb _) = isNormal mb

Expand All @@ -360,6 +364,7 @@ identIsNormal (Ident _ mb _) = isNormal mb
data MaybeAnon = NormalName -- ^ Not an anonymous name.
| AnonModArgName -- ^ Anonymous module (from `where`)
| AnonIfaceModName -- ^ Anonymous interface (from `parameter`)
| AnonInstImport -- ^ Anonymous instance import
deriving (Eq,Ord,Show,Generic)

instance NFData MaybeAnon
Expand All @@ -371,6 +376,7 @@ maybeAnonText mb txt =
NormalName -> txt
AnonModArgName -> "`where` argument of " <> txt
AnonIfaceModName -> "`parameter` interface of " <> txt
AnonInstImport -> txt

isNormal :: MaybeAnon -> Bool
isNormal mb =
Expand Down
14 changes: 0 additions & 14 deletions tests/issues/issue1455_3.icry.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,6 @@ a : [4]
b : [8]
c : [m]
0x0d
Submodules
==========

From Inst3
----------

import of F at issue1455/G.cry:5:1--5:9

Type Synonyms
=============

Expand Down Expand Up @@ -276,9 +268,3 @@ Symbols
x : [4]
y : [8]

From Inst3::import of F at issue1455/G.cry:5:1--5:9
---------------------------------------------------

a : [4]
b : [8]

14 changes: 0 additions & 14 deletions tests/issues/issue1455_3.icry.stdout.mingw32
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,6 @@ a : [4]
b : [8]
c : [m]
0x0d
Submodules
==========

From Inst3
----------

import of F at issue1455\G.cry:5:1--5:9

Type Synonyms
=============

Expand Down Expand Up @@ -276,9 +268,3 @@ Symbols
x : [4]
y : [8]

From Inst3::import of F at issue1455\G.cry:5:1--5:9
---------------------------------------------------

a : [4]
b : [8]

11 changes: 11 additions & 0 deletions tests/issues/issue_1691.cry
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

submodule A where
x = 2

submodule F where
parameter
a: Integer

z = a + 2

submodule B = submodule F where a = 3
2 changes: 2 additions & 0 deletions tests/issues/issue_1691.icry
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:load issue_1691.cry
:t B::z
4 changes: 4 additions & 0 deletions tests/issues/issue_1691.icry.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Loading module Cryptol
Loading module Cryptol
Loading module Main
B::z : Integer

0 comments on commit 623ea9b

Please sign in to comment.