-
Notifications
You must be signed in to change notification settings - Fork 574
Fix incomplete type traversals #4155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
rhendric
merged 1 commit into
purescript:master
from
rhendric:rhendric/fix-type-traversals
May 15, 2022
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| * Fix incomplete type traversals | ||
|
|
||
| This corrects oversights in some compiler internals that are not known to be | ||
| the cause of any user-facing issues. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| {-# LANGUAGE TypeApplications #-} | ||
| module TestAst where | ||
|
|
||
| import Protolude hiding (Constraint, Type, (:+)) | ||
|
|
||
| import Control.Lens ((+~)) | ||
| import Control.Newtype (ala') | ||
| import Generic.Random | ||
| import Test.Hspec | ||
| import Test.QuickCheck | ||
|
|
||
| import Language.PureScript.Label | ||
| import Language.PureScript.Names | ||
| import Language.PureScript.PSString | ||
| import Language.PureScript.Types | ||
|
|
||
| spec :: Spec | ||
| spec = do | ||
| describe "Language.PureScript.Types" $ do | ||
| describe "everywhereOnTypes" $ do | ||
| everywhereOnTypesSpec everywhereOnTypes | ||
| describe "everywhereOnTypesM" $ do | ||
| everywhereOnTypesSpec $ ala' Identity everywhereOnTypesM | ||
| describe "everywhereOnTypesTopDownM" $ do | ||
| everywhereOnTypesSpec $ ala' Identity everywhereOnTypesTopDownM | ||
| describe "everythingOnTypes" $ do | ||
| everythingOnTypesSpec everythingOnTypes | ||
| describe "everythingWithContextOnTypes" $ do | ||
| everythingOnTypesSpec $ \f g -> everythingWithContextOnTypes () [] f $ \s -> (s, ) . g | ||
|
|
||
| everywhereOnTypesSpec :: ((Type Int -> Type Int) -> Type Int -> Type Int) -> Spec | ||
| everywhereOnTypesSpec everywhereOnTypesUnderTest = do | ||
| it "should visit each type once" $ | ||
| forAllShrink (genTypeAnnotatedWith (pure 0) (pure 1)) subterms $ \t -> | ||
| all (== 1) `isSatisfiedBy` everywhereOnTypesUnderTest (annForType +~ 1) t | ||
|
|
||
| everythingOnTypesSpec :: (([Int] -> [Int] -> [Int]) -> (Type Int -> [Int]) -> Type Int -> [Int]) -> Spec | ||
| everythingOnTypesSpec everythingOnTypesUnderTest = do | ||
| it "should visit each type once" $ | ||
| forAllShrink (genTypeAnnotatedWith (pure 1) (pure 0)) subterms $ \t -> | ||
| everythingOnTypesUnderTest (++) (pure . getAnnForType) t === | ||
| filter (== 1) (toList t) | ||
|
|
||
|
|
||
| infixr 0 `isSatisfiedBy` | ||
| isSatisfiedBy :: forall a p. Show a => Testable p => (a -> p) -> a -> Property | ||
| isSatisfiedBy = liftA2 counterexample show | ||
|
|
||
| genTypeAnnotatedWith :: forall a. Gen a -> Gen a -> Gen (Type a) | ||
| genTypeAnnotatedWith genTypeAnn genConstraintAnn = genType where | ||
| generatorEnvironment | ||
| = genConstraint | ||
| :+ maybeOf genConstraintData | ||
| :+ Label <$> genPSString | ||
| :+ genPSString | ||
| :+ genQualified (OpName @'TypeOpName) | ||
| :+ genQualified (ProperName @'ClassName) | ||
| :+ genQualified (ProperName @'TypeName) | ||
| :+ genSkolemScope | ||
| :+ maybeOf genSkolemScope | ||
| :+ genText | ||
| :+ listOf' (listOf' genText) | ||
| :+ maybeOf genText | ||
| :+ genType | ||
| :+ listOf' genType | ||
| :+ maybeOf genType | ||
| :+ genWildcardData | ||
|
|
||
| genConstraint :: Gen (Constraint a) | ||
| genConstraint = genericArbitraryUG (genConstraintAnn :+ generatorEnvironment) | ||
|
|
||
| genConstraintData :: Gen ConstraintData | ||
| genConstraintData = genericArbitraryUG generatorEnvironment | ||
|
|
||
| genQualified :: forall b. (Text -> b) -> Gen (Qualified b) | ||
| genQualified ctor = Qualified Nothing . ctor <$> genText | ||
|
|
||
| genSkolemScope :: Gen SkolemScope | ||
| genSkolemScope = SkolemScope <$> arbitrary | ||
|
|
||
| genType :: Gen (Type a) | ||
| genType = genericArbitraryRecG (genTypeAnn :+ generatorEnvironment) uniform `withBaseCase` (TypeVar <$> genTypeAnn <*> genText) | ||
|
|
||
| genWildcardData :: Gen WildcardData | ||
| genWildcardData = genericArbitraryUG genText | ||
|
|
||
| maybeOf :: forall b. Gen b -> Gen (Maybe b) | ||
| maybeOf = genericArbitraryUG | ||
|
|
||
| genText :: Gen Text | ||
| genText = pure "x" -- Feel free to make this random if it matters at some point. | ||
|
|
||
| genPSString :: Gen PSString | ||
| genPSString = pure "x" -- Ditto. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unexpected consequence #2: Also removing kinds from skolems when traversing a type looking for unknowns to report in an error message. As the comment says, this is because we never print the kinds from skolems; we drop them on the ground when converting them to
PrettyPrintType.purescript/src/Language/PureScript/Pretty/Types.hs
Line 73 in d2fb21b
This seems slightly less principled but still fairly practical; I feel 90% good about this. Only caveat is we might need to change this back if we do decide to print the kinds from skolems under some conditions (like when
--verbose-errorsis in use).