-
Notifications
You must be signed in to change notification settings - Fork 182
Fix spurious T0029 when a field reaches an ATF through a type synonym #1028
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
Open
nanavati
wants to merge
12
commits into
B-Lang-org:main
Choose a base branch
from
nanavati:claude/t0029-atf-synonym-regression-mhlsbj
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
2d7b7b4
Fix #890 by expanding type functions in tiExpl
krame505 c30964f
Remove unneeded context
krame505 78c6fea
Update expected result
krame505 5578a94
Re-enable expanding type synonyms in tiField1
krame505 204cf6f
Revert "Re-enable expanding type synonyms in tiField1"
krame505 bfb0ee6
Add Julie's minimal example as a test case
krame505 6d332e4
For now, only expand synonyms in ctxRedCQType' when expTFun actually …
krame505 890be1f
Revert irrelevant changes to bsc.bluetcl test expected outputs that a…
krame505 faaf1bc
Address PR #916 review: expand type-function expansion tests
krame505 95cce71
Fix spurious T0029 when a field reaches an ATF through a type synonym
nanavati 4560905
Detect unmatchable ATFs hidden behind type synonyms in instance heads
nanavati eb08b72
Address PR #1028 review
nanavati 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
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,64 @@ | ||
| // A variant of CExtendSynonym that puts a type function (here a user-defined | ||
| // ATF, 'IdW') into the id-type synonyms, so that expanding the synonyms in the | ||
| // instance head *does* reveal a type function. That forces the conditional | ||
| // synonym expansion in 'CtxRed.ctxRedCQType'' to fire (unlike CExtendSynonym, | ||
| // where the plain synonyms are left unexpanded), which re-introduces the | ||
| // dangling 'addr_size' parameter and makes typecheck fail with T0035. | ||
| // | ||
| // This is the case that is *still broken* -- see the XXX in ctxRedCQType' and | ||
| // GitHub Issue #311 (and bsc-contrib PR 46, which worked around the analogous | ||
| // AMBA_TLM failure by adding explicit method types). CExtendATFExplicit.bsv | ||
| // shows that adding explicit types makes it compile, which suggests the | ||
| // problem is one of inference ordering rather than anything fundamental. | ||
| // The same failure occurs with the builtin type function SizeOf in place of | ||
| // the ATF 'IdW'. | ||
|
|
||
| function Bit#(m) zExtend(Bit#(n) value) | ||
| provisos(Add#(n,m,k)); | ||
| Bit#(k) out = zeroExtend(value); | ||
| if (valueOf(m) == 0) | ||
| return ?; | ||
| else | ||
| return out[valueOf(m) - 1:0]; | ||
| endfunction | ||
|
|
||
| function a cExtend(b value) | ||
| provisos(Bits#(a, sa), Bits#(b, sb)); | ||
| let out = unpack(zExtend(pack(value))); | ||
| return out; | ||
| endfunction | ||
|
|
||
| // A user-defined ATF: IdW#(Bit#(n)) = n (i.e. it plays the role of SizeOf). | ||
| typeclass HasIdW#(type t, numeric type n) dependencies (t determines n); | ||
| type IdW#(type t) = n; | ||
| endtypeclass | ||
|
|
||
| instance HasIdW#(Bit#(n), n); | ||
| endinstance | ||
|
|
||
| `define TLM_PRM_DCL numeric type id_size, \ | ||
| numeric type addr_size | ||
|
|
||
| `define TLM_PRM id_size, \ | ||
| addr_size | ||
|
|
||
| typedef Bit#(IdW#(Bit#(id_size))) TLMId#(`TLM_PRM_DCL); | ||
| typedef Bit#(IdW#(Bit#(id_size))) AxiId#(`TLM_PRM_DCL); | ||
|
|
||
| typedef struct { | ||
| AxiId#(`TLM_PRM) id; | ||
| } AxiAddrCmd#(`TLM_PRM_DCL); | ||
|
|
||
| function TLMId#(`TLM_PRM) fromAxiId(AxiId#(`TLM_PRM) id); | ||
| return cExtend(id); | ||
| endfunction | ||
|
|
||
| typeclass BusPayload#(type a, type b) dependencies(a determines b); | ||
| function b getId(a payload); | ||
| endtypeclass | ||
|
|
||
| instance BusPayload#(AxiAddrCmd#(`TLM_PRM), TLMId#(`TLM_PRM)); | ||
| function getId(payload); | ||
| return fromAxiId(payload.id); | ||
| endfunction | ||
| endinstance |
55 changes: 55 additions & 0 deletions
55
testsuite/bsc.typechecker/primtcons/CExtendATFExplicit.bsv
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,55 @@ | ||
| // As CExtendATF.bsv, but with an explicit type on the 'getId' method. With | ||
| // the explicit type, typecheck succeeds despite the ATF-triggered synonym | ||
| // expansion in the instance head. This is the workaround used in bsc-contrib | ||
| // PR 46, and it shows that the CExtendATF failure is not fundamental (see the | ||
| // XXX in CtxRed.ctxRedCQType' and GitHub Issue #311). | ||
|
|
||
| function Bit#(m) zExtend(Bit#(n) value) | ||
| provisos(Add#(n,m,k)); | ||
| Bit#(k) out = zeroExtend(value); | ||
| if (valueOf(m) == 0) | ||
| return ?; | ||
| else | ||
| return out[valueOf(m) - 1:0]; | ||
| endfunction | ||
|
|
||
| function a cExtend(b value) | ||
| provisos(Bits#(a, sa), Bits#(b, sb)); | ||
| let out = unpack(zExtend(pack(value))); | ||
| return out; | ||
| endfunction | ||
|
|
||
| // A user-defined ATF: IdW#(Bit#(n)) = n (i.e. it plays the role of SizeOf). | ||
| typeclass HasIdW#(type t, numeric type n) dependencies (t determines n); | ||
| type IdW#(type t) = n; | ||
| endtypeclass | ||
|
|
||
| instance HasIdW#(Bit#(n), n); | ||
| endinstance | ||
|
|
||
| `define TLM_PRM_DCL numeric type id_size, \ | ||
| numeric type addr_size | ||
|
|
||
| `define TLM_PRM id_size, \ | ||
| addr_size | ||
|
|
||
| typedef Bit#(IdW#(Bit#(id_size))) TLMId#(`TLM_PRM_DCL); | ||
| typedef Bit#(IdW#(Bit#(id_size))) AxiId#(`TLM_PRM_DCL); | ||
|
|
||
| typedef struct { | ||
| AxiId#(`TLM_PRM) id; | ||
| } AxiAddrCmd#(`TLM_PRM_DCL); | ||
|
|
||
| function TLMId#(`TLM_PRM) fromAxiId(AxiId#(`TLM_PRM) id); | ||
| return cExtend(id); | ||
| endfunction | ||
|
|
||
| typeclass BusPayload#(type a, type b) dependencies(a determines b); | ||
| function b getId(a payload); | ||
| endtypeclass | ||
|
|
||
| instance BusPayload#(AxiAddrCmd#(`TLM_PRM), TLMId#(`TLM_PRM)); | ||
| function TLMId#(`TLM_PRM) getId(AxiAddrCmd#(`TLM_PRM) payload); | ||
| return fromAxiId(payload.id); | ||
| endfunction | ||
| endinstance |
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,55 @@ | ||
| // Reduced version of the bsc-contrib AMBA_TLM library (see bsc-contrib PR 46). | ||
| // Check that we can use type synonyms in instance heads. The synonyms 'TLMId' | ||
| // and 'AxiId' drop the 'addr_size' parameter; expanding them fully in the | ||
| // instance head would leave 'addr_size' dangling and break unification. This | ||
| // exercises the (now conditional) synonym expansion in CtxRed.ctxRedCQType' -- | ||
| // see the XXX there and GitHub Issue #311. Because these synonyms contain no | ||
| // type function, the conditional expansion leaves them alone, so this compiles; | ||
| // bsc-contrib PR 46 added explicit method types to work around the earlier | ||
| // (unconditional-expansion) failure, and those are no longer needed here. | ||
| // CExtendATF.bsv shows a variant that *does* still fail, because it puts a type | ||
| // function in the synonyms and so triggers the expansion. | ||
|
|
||
| function Bit#(m) zExtend(Bit#(n) value) | ||
| provisos(Add#(n,m,k)); | ||
| Bit#(k) out = zeroExtend(value); | ||
| if (valueOf(m) == 0) | ||
| return ?; | ||
| else | ||
| return out[valueOf(m) - 1:0]; | ||
| endfunction | ||
|
|
||
| function a cExtend(b value) | ||
| provisos(Bits#(a, sa), Bits#(b, sb)); | ||
|
|
||
| let out = unpack(zExtend(pack(value))); | ||
|
|
||
| return out; | ||
| endfunction | ||
|
|
||
| `define TLM_PRM_DCL numeric type id_size, \ | ||
| numeric type addr_size | ||
|
|
||
| `define TLM_PRM id_size, \ | ||
| addr_size | ||
|
|
||
| typedef Bit#(id_size) TLMId#(`TLM_PRM_DCL); | ||
| typedef Bit#(id_size) AxiId#(`TLM_PRM_DCL); | ||
|
|
||
| typedef struct { | ||
| AxiId#(`TLM_PRM) id; | ||
| } AxiAddrCmd#(`TLM_PRM_DCL); | ||
|
|
||
| function TLMId#(`TLM_PRM) fromAxiId(AxiId#(`TLM_PRM) id); | ||
| return cExtend(id); | ||
| endfunction | ||
|
|
||
| typeclass BusPayload#(type a, type b) dependencies(a determines b); | ||
| function b getId(a payload); | ||
| endtypeclass | ||
|
|
||
| instance BusPayload#(AxiAddrCmd#(`TLM_PRM), TLMId#(`TLM_PRM)); | ||
| function getId(payload); | ||
| return fromAxiId(payload.id); | ||
| endfunction | ||
| endinstance |
20 changes: 20 additions & 0 deletions
20
testsuite/bsc.typechecker/primtcons/ExpSizeOf_FieldSyn_BS.bs
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,20 @@ | ||
| package ExpSizeOf_FieldSyn_BS where | ||
|
|
||
| -- Classic syntax version of ExpSizeOf_FieldSyn. | ||
| -- A struct with a field whose type uses SizeOf through a synonym chain. | ||
|
|
||
| type T t = UInt (S t) | ||
| type S t = SizeOf t | ||
|
|
||
| struct Node dataType = | ||
| dat :: dataType | ||
| unused :: T dataType | ||
| deriving (Bits, Eq) | ||
|
|
||
| {-# synthesize sysExpSizeOf_FieldSyn_BS #-} | ||
| sysExpSizeOf_FieldSyn_BS :: Module Empty | ||
| sysExpSizeOf_FieldSyn_BS = module | ||
| r_node :: Reg (Node (UInt 3)) <- mkRegU | ||
| r_data :: Reg (UInt 3) <- mkRegU | ||
| rules | ||
| when True ==> r_data := r_node.dat |
12 changes: 12 additions & 0 deletions
12
testsuite/bsc.typechecker/primtcons/ExpSizeOf_FieldSyn_Minimal.bs
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,12 @@ | ||
| package ExpSizeOf_FieldSyn_Minimal where | ||
|
|
||
| -- Minimal reproduction: a struct with a field type that uses SizeOf | ||
| -- through a type synonym. Without expanding synonyms in ctxRedCQType, the | ||
| -- needed Bits context is not injected into the derived Generic instance, and | ||
| -- that instance then fails to typecheck -- i.e. the failure is a compile | ||
| -- error, so compile_pass is a sufficient regression guard here. | ||
|
|
||
| type S t = SizeOf t | ||
|
|
||
| struct Foo a = | ||
| x :: Bit (S a) |
15 changes: 15 additions & 0 deletions
15
testsuite/bsc.typechecker/primtcons/ExpSizeOf_FieldSyn_Unbound_NoCtx.bs
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,15 @@ | ||
| package ExpSizeOf_FieldSyn_Unbound_NoCtx where | ||
|
|
||
| -- A struct field whose type uses SizeOf through a synonym, where the type | ||
| -- variable is *not* a parameter of the struct. Because the variable is | ||
| -- unbound in the struct type, the field's implicit Bits context cannot be | ||
| -- satisfied by the struct itself, so the field must declare it explicitly. | ||
| -- Here it does not, so typecheck reports the missing context (T0030). | ||
| -- (GitHub Issues #890 and #310: the field's context requirement is not | ||
| -- wrongly exempted here, despite the XXX in TCheck.tiField1.) | ||
|
|
||
| type T t = UInt (S t) | ||
| type S t = SizeOf t | ||
|
|
||
| struct MyS = | ||
| sfield :: T dataType |
11 changes: 11 additions & 0 deletions
11
testsuite/bsc.typechecker/primtcons/ExpSizeOf_FieldSyn_Unbound_WithCtx.bs
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,11 @@ | ||
| package ExpSizeOf_FieldSyn_Unbound_WithCtx where | ||
|
|
||
| -- As ExpSizeOf_FieldSyn_Unbound_NoCtx, but the field declares the Bits | ||
| -- context that its SizeOf-using type requires, so typecheck succeeds. | ||
| -- (GitHub Issues #890 and #310) | ||
|
|
||
| type T t = UInt (S t) | ||
| type S t = SizeOf t | ||
|
|
||
| struct MyS = | ||
| sfield :: (Bits dataType sz) => T dataType |
19 changes: 19 additions & 0 deletions
19
testsuite/bsc.typechecker/primtcons/ExpSizeOf_FieldSyn_Use_NoCtx.bs
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,19 @@ | ||
| package ExpSizeOf_FieldSyn_Use_NoCtx where | ||
|
|
||
| -- Reading a struct field whose type uses SizeOf through a synonym chain | ||
| -- requires a Bits context on the struct's type parameter. The field type | ||
| -- 'T a' expands to 'UInt (SizeOf a)', and reading the field during typecheck | ||
| -- introduces the implicit 'Bits a' context. Here it is not provided, so | ||
| -- typecheck reports the missing context (T0030), confirming that the field's | ||
| -- context requirement is handled during typecheck. (GitHub Issue #890) | ||
|
|
||
| type T t = UInt (S t) | ||
| type S t = SizeOf t | ||
|
|
||
| struct Node dataType = | ||
| dat :: dataType | ||
| wide :: T dataType | ||
| deriving (Bits, Eq) | ||
|
|
||
| fnNeedsCtx :: Node a -> Bool | ||
| fnNeedsCtx n = n.wide == 0 |
12 changes: 12 additions & 0 deletions
12
testsuite/bsc.typechecker/primtcons/ExpSizeOf_FieldSyn_Use_NoCtx.bs.bsc-out.expected
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,12 @@ | ||
| checking package dependencies | ||
| compiling ExpSizeOf_FieldSyn_Use_NoCtx.bs | ||
| Error: "ExpSizeOf_FieldSyn_Use_NoCtx.bs", line 18, column 0: (T0030) | ||
| The contexts for this expression are too general. | ||
| Given type: | ||
| ExpSizeOf_FieldSyn_Use_NoCtx.Node a -> Prelude.Bool | ||
| The following contexts are needed: | ||
| Prelude.Bits a a__ | ||
| Introduced at the following locations: | ||
| "ExpSizeOf_FieldSyn_Use_NoCtx.bs", line 15, column 10 | ||
| The type variables are from the following positions: | ||
| "a__" at "ExpSizeOf_FieldSyn_Use_NoCtx.bs", line 15, column 10 |
15 changes: 15 additions & 0 deletions
15
testsuite/bsc.typechecker/primtcons/ExpSizeOf_FieldSyn_Use_WithCtx.bs
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,15 @@ | ||
| package ExpSizeOf_FieldSyn_Use_WithCtx where | ||
|
|
||
| -- As ExpSizeOf_FieldSyn_Use_NoCtx, but with the explicit Bits context that | ||
| -- reading the field requires, so typecheck succeeds. (GitHub Issue #890) | ||
|
|
||
| type T t = UInt (S t) | ||
| type S t = SizeOf t | ||
|
|
||
| struct Node dataType = | ||
| dat :: dataType | ||
| wide :: T dataType | ||
| deriving (Bits, Eq) | ||
|
|
||
| fnNeedsCtx :: (Bits a sa) => Node a -> Bool | ||
| fnNeedsCtx n = n.wide == 0 |
12 changes: 12 additions & 0 deletions
12
testsuite/bsc.typechecker/primtcons/ExpSizeOf_Let_NoCtx.bs
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,12 @@ | ||
| package ExpSizeOf_Let_NoCtx where | ||
|
|
||
| -- A let binding whose type annotation contains SizeOf requires a Bits | ||
| -- context: expanding the SizeOf in the declared type introduces an implicit | ||
| -- Bits context, which must be provided by an explicit context on the | ||
| -- enclosing definition. Here none is given, so typecheck reports the | ||
| -- missing context (T0030). (GitHub Issue #890) | ||
|
|
||
| foo :: a -> String | ||
| foo _ = let x :: Bit (SizeOf a) | ||
| x = _ | ||
| in printType $ typeOf $ x |
12 changes: 12 additions & 0 deletions
12
testsuite/bsc.typechecker/primtcons/ExpSizeOf_Let_NoCtx.bs.bsc-out.expected
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,12 @@ | ||
| checking package dependencies | ||
| compiling ExpSizeOf_Let_NoCtx.bs | ||
| Error: "ExpSizeOf_Let_NoCtx.bs", line 9, column 0: (T0030) | ||
| The contexts for this expression are too general. | ||
| Given type: | ||
| a -> Prelude.String | ||
| The following contexts are needed: | ||
| Prelude.Bits a a__ | ||
| Introduced at the following locations: | ||
| "ExpSizeOf_Let_NoCtx.bs", line 10, column 22 | ||
| The type variables are from the following positions: | ||
| "a__" at "ExpSizeOf_Let_NoCtx.bs", line 10, column 22 |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.