syntax_test.go 144 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484
  1. // Copyright 2022 The Ebiten Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package shader_test
  15. import (
  16. "fmt"
  17. "strings"
  18. "testing"
  19. "github.com/hajimehoshi/ebiten/v2/internal/shader"
  20. "github.com/hajimehoshi/ebiten/v2/internal/shaderir"
  21. )
  22. func compileToIR(src []byte) (*shaderir.Program, error) {
  23. return shader.Compile(src, "Vertex", "Fragment", 0)
  24. }
  25. func TestSyntaxShadowing(t *testing.T) {
  26. if _, err := compileToIR([]byte(`package main
  27. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  28. var dstPos vec4
  29. return dstPos
  30. }
  31. `)); err == nil {
  32. t.Errorf("error must be non-nil but was nil")
  33. }
  34. }
  35. func TestSyntaxDuplicatedVariables(t *testing.T) {
  36. if _, err := compileToIR([]byte(`package main
  37. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  38. var foo vec4
  39. var foo vec4
  40. return foo
  41. }
  42. `)); err == nil {
  43. t.Errorf("error must be non-nil but was nil")
  44. }
  45. if _, err := compileToIR([]byte(`package main
  46. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  47. var foo, foo vec4
  48. return foo
  49. }
  50. `)); err == nil {
  51. t.Errorf("error must be non-nil but was nil")
  52. }
  53. if _, err := compileToIR([]byte(`package main
  54. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  55. var foo vec4
  56. foo := vec4(0)
  57. return foo
  58. }
  59. `)); err == nil {
  60. t.Errorf("error must be non-nil but was nil")
  61. }
  62. if _, err := compileToIR([]byte(`package main
  63. func Foo() (vec4, vec4) {
  64. return vec4(0), vec4(0)
  65. }
  66. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  67. foo, foo := Foo()
  68. return foo
  69. }
  70. `)); err == nil {
  71. t.Errorf("error must be non-nil but was nil")
  72. }
  73. if _, err := compileToIR([]byte(`package main
  74. func foo() {
  75. var x int
  76. var y float
  77. var x vec2
  78. _ = x
  79. _ = y
  80. }
  81. `)); err == nil {
  82. t.Error("compileToIR must return an error but did not")
  83. }
  84. }
  85. func TestSyntaxDuplicatedFunctions(t *testing.T) {
  86. if _, err := compileToIR([]byte(`package main
  87. func Foo() {
  88. }
  89. func Foo() {
  90. }
  91. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  92. return vec4(0)
  93. }
  94. `)); err == nil {
  95. t.Errorf("error must be non-nil but was nil")
  96. }
  97. }
  98. func TestSyntaxNoNewVariables(t *testing.T) {
  99. if _, err := compileToIR([]byte(`package main
  100. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  101. _ := 1
  102. return vec4(0)
  103. }
  104. `)); err == nil {
  105. t.Errorf("error must be non-nil but was nil")
  106. }
  107. if _, err := compileToIR([]byte(`package main
  108. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  109. _, _ := 1, 1
  110. return vec4(0)
  111. }
  112. `)); err == nil {
  113. t.Errorf("error must be non-nil but was nil")
  114. }
  115. if _, err := compileToIR([]byte(`package main
  116. func Foo() (int, int) {
  117. return 1, 1
  118. }
  119. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  120. _, _ := Foo()
  121. return vec4(0)
  122. }
  123. `)); err == nil {
  124. t.Errorf("error must be non-nil but was nil")
  125. }
  126. if _, err := compileToIR([]byte(`package main
  127. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  128. a, _ := 1, 1
  129. _ = a
  130. return vec4(0)
  131. }
  132. `)); err != nil {
  133. t.Error(err)
  134. }
  135. if _, err := compileToIR([]byte(`package main
  136. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  137. _, a := 1, 1
  138. _ = a
  139. return vec4(0)
  140. }
  141. `)); err != nil {
  142. t.Error(err)
  143. }
  144. }
  145. func TestSyntaxWrongReturn(t *testing.T) {
  146. if _, err := compileToIR([]byte(`package main
  147. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  148. return 0.0
  149. }
  150. `)); err == nil {
  151. t.Errorf("error must be non-nil but was nil")
  152. }
  153. if _, err := compileToIR([]byte(`package main
  154. func Foo() (float, float) {
  155. return 0
  156. }
  157. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  158. return vec4(0)
  159. }
  160. `)); err == nil {
  161. t.Errorf("error must be non-nil but was nil")
  162. }
  163. if _, err := compileToIR([]byte(`package main
  164. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  165. }
  166. `)); err == nil {
  167. t.Errorf("error must be non-nil but was nil")
  168. }
  169. if _, err := compileToIR([]byte(`package main
  170. func Foo() float {
  171. }
  172. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  173. return vec4(0)
  174. }
  175. `)); err == nil {
  176. t.Errorf("error must be non-nil but was nil")
  177. }
  178. }
  179. func TestSyntaxMultipleValueReturn(t *testing.T) {
  180. if _, err := compileToIR([]byte(`package main
  181. func Foo() (float, float) {
  182. return 0.0
  183. }
  184. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  185. return vec4(0)
  186. }
  187. `)); err == nil {
  188. t.Errorf("error must be non-nil but was nil")
  189. }
  190. if _, err := compileToIR([]byte(`package main
  191. func Foo() float {
  192. return 0.0, 0.0
  193. }
  194. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  195. return vec4(0)
  196. }
  197. `)); err == nil {
  198. t.Errorf("error must be non-nil but was nil")
  199. }
  200. if _, err := compileToIR([]byte(`package main
  201. func Foo() (float, float, float) {
  202. return 0.0, 0.0
  203. }
  204. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  205. return vec4(0)
  206. }
  207. `)); err == nil {
  208. t.Errorf("error must be non-nil but was nil")
  209. }
  210. if _, err := compileToIR([]byte(`package main
  211. func Foo() (float, float) {
  212. return 0.0, 0.0
  213. }
  214. func Foo2() (float, float, float) {
  215. return Foo()
  216. }
  217. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  218. return vec4(0)
  219. }
  220. `)); err == nil {
  221. t.Errorf("error must be non-nil but was nil")
  222. }
  223. if _, err := compileToIR([]byte(`package main
  224. func Foo() (float, float, float) {
  225. return 0.0, 0.0, 0.0
  226. }
  227. func Foo2() (float, float, float) {
  228. return Foo()
  229. }
  230. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  231. return vec4(0.0)
  232. }
  233. `)); err != nil {
  234. t.Error(err)
  235. }
  236. }
  237. func TestSyntaxInit(t *testing.T) {
  238. if _, err := compileToIR([]byte(`package main
  239. func init() {
  240. }
  241. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  242. return vec4(0)
  243. }
  244. `)); err == nil {
  245. t.Errorf("error must be non-nil but was nil")
  246. }
  247. }
  248. func TestSyntaxUnsupportedSyntax(t *testing.T) {
  249. if _, err := compileToIR([]byte(`package main
  250. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  251. x := func() {
  252. }
  253. _ = x
  254. return vec4(0)
  255. }
  256. `)); err == nil {
  257. t.Errorf("error must be non-nil but was nil")
  258. }
  259. if _, err := compileToIR([]byte(`package main
  260. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  261. go func() {
  262. }()
  263. return vec4(0)
  264. }
  265. `)); err == nil {
  266. t.Errorf("error must be non-nil but was nil")
  267. }
  268. if _, err := compileToIR([]byte(`package main
  269. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  270. ch := make(chan int)
  271. _ = ch
  272. return vec4(0)
  273. }
  274. `)); err == nil {
  275. t.Errorf("error must be non-nil but was nil")
  276. }
  277. if _, err := compileToIR([]byte(`package main
  278. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  279. x := 1i
  280. _ = x
  281. return vec4(0)
  282. }
  283. `)); err == nil {
  284. t.Errorf("error must be non-nil but was nil")
  285. }
  286. if _, err := compileToIR([]byte(`package main
  287. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  288. var x [4]float
  289. y := x[1:2]
  290. _ = y
  291. return vec4(0)
  292. }
  293. `)); err == nil {
  294. t.Errorf("error must be non-nil but was nil")
  295. }
  296. if _, err := compileToIR([]byte(`package main
  297. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  298. var x [4]float
  299. y := x[1:2:3]
  300. _ = y
  301. return vec4(0)
  302. }
  303. `)); err == nil {
  304. t.Errorf("error must be non-nil but was nil")
  305. }
  306. }
  307. func TestSyntaxForbidAssigningSpecialVariables(t *testing.T) {
  308. if _, err := compileToIR([]byte(`package main
  309. var U vec4
  310. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  311. U = vec4(0)
  312. return vec4(0)
  313. }
  314. `)); err == nil {
  315. t.Errorf("error must be non-nil but was nil")
  316. }
  317. if _, err := compileToIR([]byte(`package main
  318. var U vec4
  319. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  320. U.x = 0
  321. return vec4(0)
  322. }
  323. `)); err == nil {
  324. t.Errorf("error must be non-nil but was nil")
  325. }
  326. if _, err := compileToIR([]byte(`package main
  327. var U [2]vec4
  328. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  329. U[0] = vec4(0)
  330. return vec4(0)
  331. }
  332. `)); err == nil {
  333. t.Errorf("error must be non-nil but was nil")
  334. }
  335. if _, err := compileToIR([]byte(`package main
  336. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  337. srcPos = vec2(0)
  338. return vec4(0)
  339. }
  340. `)); err == nil {
  341. t.Errorf("error must be non-nil but was nil")
  342. }
  343. if _, err := compileToIR([]byte(`package main
  344. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  345. srcPos.x = 0
  346. return vec4(0)
  347. }
  348. `)); err == nil {
  349. t.Errorf("error must be non-nil but was nil")
  350. }
  351. }
  352. func TestSyntaxBoolLiteral(t *testing.T) {
  353. if _, err := compileToIR([]byte(`package main
  354. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  355. true := vec4(0)
  356. return true
  357. }
  358. `)); err != nil {
  359. t.Error(err)
  360. }
  361. }
  362. func TestSyntaxUnusedVariable(t *testing.T) {
  363. if _, err := compileToIR([]byte(`package main
  364. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  365. x := 0
  366. return vec4(0)
  367. }
  368. `)); err == nil {
  369. t.Errorf("error must be non-nil but was nil")
  370. }
  371. if _, err := compileToIR([]byte(`package main
  372. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  373. x := 0
  374. x = 1
  375. return vec4(0)
  376. }
  377. `)); err == nil {
  378. t.Errorf("error must be non-nil but was nil")
  379. }
  380. if _, err := compileToIR([]byte(`package main
  381. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  382. x := vec4(0)
  383. x.x = 1
  384. return vec4(0)
  385. }
  386. `)); err != nil {
  387. t.Error(err)
  388. }
  389. // Increment statement treats a variable 'used'.
  390. // https://go.dev/play/p/2RuYMrSLjt3
  391. if _, err := compileToIR([]byte(`package main
  392. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  393. x := 0
  394. x++
  395. return vec4(0)
  396. }
  397. `)); err != nil {
  398. t.Error(err)
  399. }
  400. if _, err := compileToIR([]byte(`package main
  401. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  402. var a int
  403. return vec4(0)
  404. }
  405. `)); err == nil {
  406. t.Errorf("error must be non-nil but was nil")
  407. }
  408. if _, err := compileToIR([]byte(`package main
  409. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  410. var a, b int
  411. return vec4(0)
  412. }
  413. `)); err == nil {
  414. t.Errorf("error must be non-nil but was nil")
  415. }
  416. // Issue #2848
  417. if _, err := compileToIR([]byte(`package main
  418. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  419. var floats [4]float
  420. for i := 0; i < 3; i++ {
  421. j := i + 1
  422. floats[j] = float(i)
  423. }
  424. return vec4(floats[0], floats[1], floats[2], floats[3])
  425. }
  426. `)); err != nil {
  427. t.Error(err)
  428. }
  429. }
  430. func TestSyntaxBlankLhs(t *testing.T) {
  431. if _, err := compileToIR([]byte(`package main
  432. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  433. x := _
  434. _ = x
  435. return vec4(0)
  436. }
  437. `)); err == nil {
  438. t.Errorf("error must be non-nil but was nil")
  439. }
  440. if _, err := compileToIR([]byte(`package main
  441. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  442. var x int = _
  443. _ = x
  444. return vec4(0)
  445. }
  446. `)); err == nil {
  447. t.Errorf("error must be non-nil but was nil")
  448. }
  449. if _, err := compileToIR([]byte(`package main
  450. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  451. x := 1
  452. x = _
  453. _ = x
  454. return vec4(0)
  455. }
  456. `)); err == nil {
  457. t.Errorf("error must be non-nil but was nil")
  458. }
  459. if _, err := compileToIR([]byte(`package main
  460. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  461. x := 1 + _
  462. _ = x
  463. return vec4(0)
  464. }
  465. `)); err == nil {
  466. t.Errorf("error must be non-nil but was nil")
  467. }
  468. if _, err := compileToIR([]byte(`package main
  469. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  470. _++
  471. return vec4(0)
  472. }
  473. `)); err == nil {
  474. t.Errorf("error must be non-nil but was nil")
  475. }
  476. if _, err := compileToIR([]byte(`package main
  477. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  478. _ += 1
  479. return vec4(0)
  480. }
  481. `)); err == nil {
  482. t.Errorf("error must be non-nil but was nil")
  483. }
  484. if _, err := compileToIR([]byte(`package main
  485. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  486. _.x = 1
  487. return vec4(0)
  488. }
  489. `)); err == nil {
  490. t.Errorf("error must be non-nil but was nil")
  491. }
  492. }
  493. func TestSyntaxDuplicatedVarsAndConstants(t *testing.T) {
  494. if _, err := compileToIR([]byte(`package main
  495. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  496. var a = 0
  497. const a = 0
  498. _ = a
  499. return vec4(0)
  500. }
  501. `)); err == nil {
  502. t.Errorf("error must be non-nil but was nil")
  503. }
  504. if _, err := compileToIR([]byte(`package main
  505. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  506. const a = 0
  507. var a = 0
  508. _ = a
  509. return vec4(0)
  510. }
  511. `)); err == nil {
  512. t.Errorf("error must be non-nil but was nil")
  513. }
  514. if _, err := compileToIR([]byte(`package main
  515. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  516. const a = 0
  517. const a = 0
  518. _ = a
  519. return vec4(0)
  520. }
  521. `)); err == nil {
  522. t.Errorf("error must be non-nil but was nil")
  523. }
  524. if _, err := compileToIR([]byte(`package main
  525. const U0 = 0
  526. var U0 float
  527. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  528. return vec4(a)
  529. }
  530. `)); err == nil {
  531. t.Errorf("error must be non-nil but was nil")
  532. }
  533. if _, err := compileToIR([]byte(`package main
  534. var U0 float
  535. const U0 = 0
  536. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  537. return vec4(a)
  538. }
  539. `)); err == nil {
  540. t.Errorf("error must be non-nil but was nil")
  541. }
  542. }
  543. func TestSyntaxUnmatchedArgs(t *testing.T) {
  544. if _, err := compileToIR([]byte(`package main
  545. func Foo() {
  546. }
  547. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  548. Foo(1)
  549. return dstPos
  550. }
  551. `)); err == nil {
  552. t.Errorf("error must be non-nil but was nil")
  553. }
  554. if _, err := compileToIR([]byte(`package main
  555. func Foo(x float) {
  556. }
  557. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  558. Foo()
  559. return dstPos
  560. }
  561. `)); err == nil {
  562. t.Errorf("error must be non-nil but was nil")
  563. }
  564. if _, err := compileToIR([]byte(`package main
  565. func Foo(x, y float) {
  566. }
  567. func Bar() (float, float, float) {
  568. return 0, 1
  569. }
  570. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  571. Foo(Bar())
  572. return dstPos
  573. }
  574. `)); err == nil {
  575. t.Errorf("error must be non-nil but was nil")
  576. }
  577. if _, err := compileToIR([]byte(`package main
  578. func Foo(x, y, z float) {
  579. }
  580. func Bar() (float, float) {
  581. return 0, 1
  582. }
  583. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  584. Foo(Bar())
  585. return dstPos
  586. }
  587. `)); err == nil {
  588. t.Errorf("error must be non-nil but was nil")
  589. }
  590. }
  591. // Issue #1898
  592. func TestSyntaxMeaninglessSentence(t *testing.T) {
  593. if _, err := compileToIR([]byte(`package main
  594. var Time float
  595. var ScreenSize vec2
  596. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  597. dstPos
  598. return dstPos
  599. }`)); err == nil {
  600. t.Errorf("error must be non-nil but was nil")
  601. }
  602. if _, err := compileToIR([]byte(`package main
  603. var Time float
  604. var ScreenSize vec2
  605. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  606. vec2(dstPos)
  607. return dstPos
  608. }`)); err == nil {
  609. t.Errorf("error must be non-nil but was nil")
  610. }
  611. }
  612. // Issue #1947
  613. func TestSyntaxOperatorMod(t *testing.T) {
  614. if _, err := compileToIR([]byte(`package main
  615. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  616. a := 2.0 % 0.5
  617. _ = a
  618. return vec4(0)
  619. }`)); err == nil {
  620. t.Errorf("error must be non-nil but was nil")
  621. }
  622. if _, err := compileToIR([]byte(`package main
  623. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  624. // If both are constants, both must be an integer!
  625. a := 2.0 % 1.0
  626. _ = a
  627. return vec4(0)
  628. }`)); err == nil {
  629. t.Errorf("error must be non-nil but was nil")
  630. }
  631. if _, err := compileToIR([]byte(`package main
  632. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  633. a := int(2) % 0.5
  634. _ = a
  635. return vec4(0)
  636. }`)); err == nil {
  637. t.Errorf("error must be non-nil but was nil")
  638. }
  639. if _, err := compileToIR([]byte(`package main
  640. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  641. a := int(2) % 1.0
  642. _ = a
  643. return vec4(0)
  644. }`)); err != nil {
  645. t.Error(err)
  646. }
  647. if _, err := compileToIR([]byte(`package main
  648. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  649. a := 2.0
  650. b := 0.5
  651. _ = a % b
  652. return vec4(0)
  653. }`)); err == nil {
  654. t.Errorf("error must be non-nil but was nil")
  655. }
  656. if _, err := compileToIR([]byte(`package main
  657. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  658. a := 2
  659. b := 0.5
  660. _ = a % b
  661. return vec4(0)
  662. }`)); err == nil {
  663. t.Errorf("error must be non-nil but was nil")
  664. }
  665. if _, err := compileToIR([]byte(`package main
  666. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  667. a := 2.5
  668. b := 1
  669. _ = a % b
  670. return vec4(0)
  671. }`)); err == nil {
  672. t.Errorf("error must be non-nil but was nil")
  673. }
  674. if _, err := compileToIR([]byte(`package main
  675. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  676. a := 2
  677. b := 1
  678. _ = a % b
  679. return vec4(0)
  680. }`)); err != nil {
  681. t.Error(err)
  682. }
  683. if _, err := compileToIR([]byte(`package main
  684. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  685. a := 2
  686. _ = a % 1
  687. return vec4(0)
  688. }`)); err != nil {
  689. t.Error(err)
  690. }
  691. if _, err := compileToIR([]byte(`package main
  692. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  693. // If only one of two is a consntant, the constant can be a float.
  694. a := 2
  695. _ = a % 1.0
  696. return vec4(0)
  697. }`)); err != nil {
  698. t.Error(err)
  699. }
  700. if _, err := compileToIR([]byte(`package main
  701. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  702. a := 1
  703. _ = 2 % a
  704. return vec4(0)
  705. }`)); err != nil {
  706. t.Error(err)
  707. }
  708. if _, err := compileToIR([]byte(`package main
  709. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  710. // If only one of two is a consntant, the constant can be a float.
  711. a := 1
  712. _ = 2.0 % a
  713. return vec4(0)
  714. }`)); err != nil {
  715. t.Error(err)
  716. }
  717. if _, err := compileToIR([]byte(`package main
  718. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  719. a := 2
  720. a %= 1
  721. _ = a
  722. return vec4(0)
  723. }`)); err != nil {
  724. t.Error(err)
  725. }
  726. if _, err := compileToIR([]byte(`package main
  727. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  728. a := 2
  729. a %= 1.0
  730. _ = a
  731. return vec4(0)
  732. }`)); err != nil {
  733. t.Error(err)
  734. }
  735. if _, err := compileToIR([]byte(`package main
  736. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  737. a := 2
  738. a %= 0.5
  739. _ = a
  740. return vec4(0)
  741. }`)); err == nil {
  742. t.Errorf("error must be non-nil but was nil")
  743. }
  744. if _, err := compileToIR([]byte(`package main
  745. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  746. a := 2.0
  747. a %= 1
  748. _ = a
  749. return vec4(0)
  750. }`)); err == nil {
  751. t.Errorf("error must be non-nil but was nil")
  752. }
  753. }
  754. func TestSyntaxOperatorAssign(t *testing.T) {
  755. if _, err := compileToIR([]byte(`package main
  756. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  757. a := 1.0
  758. a += 2
  759. _ = a
  760. return vec4(0)
  761. }`)); err != nil {
  762. t.Error(err)
  763. }
  764. if _, err := compileToIR([]byte(`package main
  765. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  766. a := 1.0
  767. a += 2.0
  768. _ = a
  769. return vec4(0)
  770. }`)); err != nil {
  771. t.Error(err)
  772. }
  773. if _, err := compileToIR([]byte(`package main
  774. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  775. a := 1.0
  776. a += 2.1
  777. _ = a
  778. return vec4(0)
  779. }`)); err != nil {
  780. t.Error(err)
  781. }
  782. if _, err := compileToIR([]byte(`package main
  783. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  784. a := 1
  785. a += 2
  786. _ = a
  787. return vec4(0)
  788. }`)); err != nil {
  789. t.Error(err)
  790. }
  791. if _, err := compileToIR([]byte(`package main
  792. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  793. a := 1
  794. a += 2.0
  795. _ = a
  796. return vec4(0)
  797. }`)); err != nil {
  798. t.Error(err)
  799. }
  800. if _, err := compileToIR([]byte(`package main
  801. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  802. a := 1
  803. a += 2.1
  804. _ = a
  805. return vec4(0)
  806. }`)); err == nil {
  807. t.Errorf("error must be non-nil but was nil")
  808. }
  809. if _, err := compileToIR([]byte(`package main
  810. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  811. var x float = true
  812. _ = x
  813. return vec4(0)
  814. }
  815. `)); err == nil {
  816. t.Errorf("error must be non-nil but was nil")
  817. }
  818. if _, err := compileToIR([]byte(`package main
  819. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  820. var x bool = true
  821. _ = x
  822. return vec4(0)
  823. }
  824. `)); err != nil {
  825. t.Error(err)
  826. }
  827. if _, err := compileToIR([]byte(`package main
  828. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  829. var x int = 1.0
  830. _ = x
  831. return vec4(0)
  832. }
  833. `)); err != nil {
  834. t.Error(err)
  835. }
  836. }
  837. // Issue #1963
  838. func TestSyntaxOperatorVecAndNumber(t *testing.T) {
  839. if _, err := compileToIR([]byte(`package main
  840. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  841. a := vec2(1) + 2
  842. return a.xxyy
  843. }`)); err != nil {
  844. t.Error(err)
  845. }
  846. if _, err := compileToIR([]byte(`package main
  847. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  848. a := ivec2(1) + 2
  849. return vec4(a.xxyy)
  850. }`)); err != nil {
  851. t.Error(err)
  852. }
  853. if _, err := compileToIR([]byte(`package main
  854. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  855. a := vec2(1) + 2.1
  856. return a.xxyy
  857. }`)); err != nil {
  858. t.Error(err)
  859. }
  860. if _, err := compileToIR([]byte(`package main
  861. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  862. a := ivec2(1) + 2.1
  863. return vec4(a.xxyy)
  864. }`)); err == nil {
  865. t.Errorf("error must be non-nil but was nil")
  866. }
  867. if _, err := compileToIR([]byte(`package main
  868. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  869. a := vec2(1) % 2
  870. return a.xxyy
  871. }`)); err == nil {
  872. t.Errorf("error must be non-nil but was nil")
  873. }
  874. if _, err := compileToIR([]byte(`package main
  875. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  876. a := ivec2(1) % 2
  877. return vec4(a.xxyy)
  878. }`)); err != nil {
  879. t.Error(err)
  880. }
  881. if _, err := compileToIR([]byte(`package main
  882. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  883. a := vec2(1) % 2.1
  884. return a.xxyy
  885. }`)); err == nil {
  886. t.Errorf("error must be non-nil but was nil")
  887. }
  888. if _, err := compileToIR([]byte(`package main
  889. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  890. a := ivec2(1) % 2.1
  891. return vec4(a.xxyy)
  892. }`)); err == nil {
  893. t.Errorf("error must be non-nil but was nil")
  894. }
  895. if _, err := compileToIR([]byte(`package main
  896. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  897. a := vec2(1)
  898. a += 2
  899. return a.xxyy
  900. }`)); err != nil {
  901. t.Error(err)
  902. }
  903. if _, err := compileToIR([]byte(`package main
  904. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  905. a := ivec2(1)
  906. a += 2
  907. return vec4(a.xxyy)
  908. }`)); err != nil {
  909. t.Error(err)
  910. }
  911. if _, err := compileToIR([]byte(`package main
  912. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  913. a := vec2(1)
  914. a += 2.1
  915. return a.xxyy
  916. }`)); err != nil {
  917. t.Error(err)
  918. }
  919. if _, err := compileToIR([]byte(`package main
  920. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  921. a := ivec2(1)
  922. a += 2.1
  923. return vec4(a.xxyy)
  924. }`)); err == nil {
  925. t.Errorf("error must be non-nil but was nil")
  926. }
  927. if _, err := compileToIR([]byte(`package main
  928. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  929. a := vec2(1)
  930. a %= 2
  931. return a.xxyy
  932. }`)); err == nil {
  933. t.Errorf("error must be non-nil but was nil")
  934. }
  935. if _, err := compileToIR([]byte(`package main
  936. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  937. a := ivec2(1)
  938. a %= 2
  939. return vec4(a.xxyy)
  940. }`)); err != nil {
  941. t.Error(err)
  942. }
  943. if _, err := compileToIR([]byte(`package main
  944. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  945. a := vec2(1)
  946. a %= 2.1
  947. return a.xxyy
  948. }`)); err == nil {
  949. t.Errorf("error must be non-nil but was nil")
  950. }
  951. if _, err := compileToIR([]byte(`package main
  952. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  953. a := ivec2(1)
  954. a %= 2.1
  955. return vec4(a.xxyy)
  956. }`)); err == nil {
  957. t.Errorf("error must be non-nil but was nil")
  958. }
  959. }
  960. // Issue #1971
  961. func TestSyntaxOperatorMultiply(t *testing.T) {
  962. // Note: mat + float is allowed in GLSL but not in Metal.
  963. cases := []struct {
  964. stmt string
  965. err bool
  966. }{
  967. {stmt: "a := 1 * vec2(2); _ = a", err: false},
  968. {stmt: "a := int(1) * vec2(2); _ = a", err: true},
  969. {stmt: "a := 1.0 * vec2(2); _ = a", err: false},
  970. {stmt: "a := 1.1 * vec2(2); _ = a", err: false},
  971. {stmt: "a := 1 + vec2(2); _ = a", err: false},
  972. {stmt: "a := int(1) + vec2(2); _ = a", err: true},
  973. {stmt: "a := 1.0 / vec2(2); _ = a", err: false},
  974. {stmt: "a := 1.1 / vec2(2); _ = a", err: false},
  975. {stmt: "a := 1.0 + vec2(2); _ = a", err: false},
  976. {stmt: "a := 1.1 + vec2(2); _ = a", err: false},
  977. {stmt: "a := 1 * vec3(2); _ = a", err: false},
  978. {stmt: "a := 1.0 * vec3(2); _ = a", err: false},
  979. {stmt: "a := 1.1 * vec3(2); _ = a", err: false},
  980. {stmt: "a := 1 * vec4(2); _ = a", err: false},
  981. {stmt: "a := 1.0 * vec4(2); _ = a", err: false},
  982. {stmt: "a := 1.1 * vec4(2); _ = a", err: false},
  983. {stmt: "a := 1 * ivec2(2); _ = a", err: false},
  984. {stmt: "a := int(1) * ivec2(2); _ = a", err: false},
  985. {stmt: "a := 1.0 * ivec2(2); _ = a", err: false},
  986. {stmt: "a := 1.1 * ivec2(2); _ = a", err: true},
  987. {stmt: "a := 1 + ivec2(2); _ = a", err: false},
  988. {stmt: "a := int(1) + ivec2(2); _ = a", err: false},
  989. {stmt: "a := 1.0 / ivec2(2); _ = a", err: false},
  990. {stmt: "a := 1.1 / ivec2(2); _ = a", err: true},
  991. {stmt: "a := 1.0 + ivec2(2); _ = a", err: false},
  992. {stmt: "a := 1.1 + ivec2(2); _ = a", err: true},
  993. {stmt: "a := 1 * ivec3(2); _ = a", err: false},
  994. {stmt: "a := 1.0 * ivec3(2); _ = a", err: false},
  995. {stmt: "a := 1.1 * ivec3(2); _ = a", err: true},
  996. {stmt: "a := 1 * ivec4(2); _ = a", err: false},
  997. {stmt: "a := 1.0 * ivec4(2); _ = a", err: false},
  998. {stmt: "a := 1.1 * ivec4(2); _ = a", err: true},
  999. {stmt: "a := 1 * mat2(2); _ = a", err: false},
  1000. {stmt: "a := 1.0 * mat2(2); _ = a", err: false},
  1001. {stmt: "a := float(1.0) / mat2(2); _ = a", err: true},
  1002. {stmt: "a := 1.0 / mat2(2); _ = a", err: true},
  1003. {stmt: "a := float(1.0) + mat2(2); _ = a", err: true},
  1004. {stmt: "a := 1.0 + mat2(2); _ = a", err: true},
  1005. {stmt: "a := 1 * mat3(2); _ = a", err: false},
  1006. {stmt: "a := 1.0 * mat3(2); _ = a", err: false},
  1007. {stmt: "a := 1 * mat4(2); _ = a", err: false},
  1008. {stmt: "a := 1.0 * mat4(2); _ = a", err: false},
  1009. {stmt: "a := vec2(1) * 2; _ = a", err: false},
  1010. {stmt: "a := vec2(1) * 2.0; _ = a", err: false},
  1011. {stmt: "a := vec2(1) * 2.1; _ = a", err: false},
  1012. {stmt: "a := vec2(1) / 2.0; _ = a", err: false},
  1013. {stmt: "a := vec2(1) / 2.1; _ = a", err: false},
  1014. {stmt: "a := vec2(1) + 2.0; _ = a", err: false},
  1015. {stmt: "a := vec2(1) + 2.1; _ = a", err: false},
  1016. {stmt: "a := vec2(1) * int(2); _ = a", err: true},
  1017. {stmt: "a := vec2(1) * vec2(2); _ = a", err: false},
  1018. {stmt: "a := vec2(1) + vec2(2); _ = a", err: false},
  1019. {stmt: "a := vec2(1) * vec3(2); _ = a", err: true},
  1020. {stmt: "a := vec2(1) * vec4(2); _ = a", err: true},
  1021. {stmt: "a := vec2(1) * ivec2(2); _ = a", err: true},
  1022. {stmt: "a := vec2(1) + ivec2(2); _ = a", err: true},
  1023. {stmt: "a := vec2(1) * ivec3(2); _ = a", err: true},
  1024. {stmt: "a := vec2(1) * ivec4(2); _ = a", err: true},
  1025. {stmt: "a := vec2(1) * mat2(2); _ = a", err: false},
  1026. {stmt: "a := vec2(1) + mat2(2); _ = a", err: true},
  1027. {stmt: "a := vec2(1) * mat3(2); _ = a", err: true},
  1028. {stmt: "a := vec2(1) * mat4(2); _ = a", err: true},
  1029. {stmt: "a := ivec2(1) * 2; _ = a", err: false},
  1030. {stmt: "a := ivec2(1) * 2.0; _ = a", err: false},
  1031. {stmt: "a := ivec2(1) * 2.1; _ = a", err: true},
  1032. {stmt: "a := ivec2(1) / 2.0; _ = a", err: false},
  1033. {stmt: "a := ivec2(1) / 2.1; _ = a", err: true},
  1034. {stmt: "a := ivec2(1) + 2.0; _ = a", err: false},
  1035. {stmt: "a := ivec2(1) + 2.1; _ = a", err: true},
  1036. {stmt: "a := ivec2(1) * int(2); _ = a", err: false},
  1037. {stmt: "a := ivec2(1) * vec2(2); _ = a", err: true},
  1038. {stmt: "a := ivec2(1) + vec2(2); _ = a", err: true},
  1039. {stmt: "a := ivec2(1) * vec3(2); _ = a", err: true},
  1040. {stmt: "a := ivec2(1) * vec4(2); _ = a", err: true},
  1041. {stmt: "a := ivec2(1) * ivec2(2); _ = a", err: false},
  1042. {stmt: "a := ivec2(1) + ivec2(2); _ = a", err: false},
  1043. {stmt: "a := ivec2(1) * ivec3(2); _ = a", err: true},
  1044. {stmt: "a := ivec2(1) * ivec4(2); _ = a", err: true},
  1045. {stmt: "a := ivec2(1) * mat2(2); _ = a", err: true},
  1046. {stmt: "a := ivec2(1) + mat2(2); _ = a", err: true},
  1047. {stmt: "a := ivec2(1) * mat3(2); _ = a", err: true},
  1048. {stmt: "a := ivec2(1) * mat4(2); _ = a", err: true},
  1049. {stmt: "a := mat2(1) * 2; _ = a", err: false},
  1050. {stmt: "a := mat2(1) * 2.0; _ = a", err: false},
  1051. {stmt: "a := mat2(1) * 2.1; _ = a", err: false},
  1052. {stmt: "a := mat2(1) / 2.0; _ = a", err: false},
  1053. {stmt: "a := mat2(1) / 2.1; _ = a", err: false},
  1054. {stmt: "a := mat2(1) / float(2); _ = a", err: false},
  1055. {stmt: "a := mat2(1) * int(2); _ = a", err: true},
  1056. {stmt: "a := mat2(1) + 2.0; _ = a", err: true},
  1057. {stmt: "a := mat2(1) + 2.1; _ = a", err: true},
  1058. {stmt: "a := mat2(1) + float(2); _ = a", err: true},
  1059. {stmt: "a := mat2(1) * vec2(2); _ = a", err: false},
  1060. {stmt: "a := mat2(1) + vec2(2); _ = a", err: true},
  1061. {stmt: "a := mat2(1) * vec3(2); _ = a", err: true},
  1062. {stmt: "a := mat2(1) * vec4(2); _ = a", err: true},
  1063. {stmt: "a := mat2(1) * ivec2(2); _ = a", err: true},
  1064. {stmt: "a := mat2(1) + ivec2(2); _ = a", err: true},
  1065. {stmt: "a := mat2(1) * ivec3(2); _ = a", err: true},
  1066. {stmt: "a := mat2(1) * ivec4(2); _ = a", err: true},
  1067. {stmt: "a := mat2(1) * mat2(2); _ = a", err: false},
  1068. {stmt: "a := mat2(1) / mat2(2); _ = a", err: true},
  1069. {stmt: "a := mat2(1) * mat3(2); _ = a", err: true},
  1070. {stmt: "a := mat2(1) * mat4(2); _ = a", err: true},
  1071. }
  1072. for _, c := range cases {
  1073. _, err := compileToIR([]byte(fmt.Sprintf(`package main
  1074. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  1075. %s
  1076. return dstPos
  1077. }`, c.stmt)))
  1078. if err == nil && c.err {
  1079. t.Errorf("%s must return an error but does not", c.stmt)
  1080. } else if err != nil && !c.err {
  1081. t.Errorf("%s must not return nil but returned %v", c.stmt, err)
  1082. }
  1083. }
  1084. }
  1085. // Issue: #2755
  1086. func TestSyntaxOperatorShift(t *testing.T) {
  1087. cases := []struct {
  1088. stmt string
  1089. err bool
  1090. }{
  1091. {stmt: "a := 1 << 2; _ = a", err: false},
  1092. {stmt: "a := 1 << 2.0; _ = a", err: false},
  1093. {stmt: "a := 1.0 << 2; _ = a", err: false},
  1094. {stmt: "a := 1.0 << 2.0; _ = a", err: false},
  1095. {stmt: "a := 1.0 << int(1); _ = a", err: false},
  1096. {stmt: "a := int(1) << 2.0; _ = a", err: false},
  1097. {stmt: "a := ivec2(1) << 2.0; _ = a", err: false},
  1098. {stmt: "var a = 1; b := a << 2.0; _ = b", err: false},
  1099. {stmt: "var a = 1; b := 2.0 << a; _ = b", err: false}, // PR: #2916
  1100. {stmt: "a := float(1.0) << 2; _ = a", err: true},
  1101. {stmt: "a := 1 << float(2.0); _ = a", err: true},
  1102. {stmt: "a := ivec2(1) << 2; _ = a", err: false},
  1103. {stmt: "a := 1 << ivec2(2); _ = a", err: true},
  1104. {stmt: "a := ivec2(1) << float(2.0); _ = a", err: true},
  1105. {stmt: "a := float(1.0) << ivec2(2); _ = a", err: true},
  1106. {stmt: "a := ivec2(1) << ivec2(2); _ = a", err: false},
  1107. {stmt: "a := ivec3(1) << ivec2(2); _ = a", err: true},
  1108. {stmt: "a := ivec2(1) << ivec3(2); _ = a", err: true},
  1109. {stmt: "a := 1 << vec2(2); _ = a", err: true},
  1110. {stmt: "a := vec2(1) << 2; _ = a", err: true},
  1111. {stmt: "a := float(1.0) << vec2(2); _ = a", err: true},
  1112. {stmt: "a := vec2(1) << float(2.0); _ = a", err: true},
  1113. {stmt: "a := vec2(1) << vec2(2); _ = a", err: true},
  1114. {stmt: "a := vec2(1) << vec3(2); _ = a", err: true},
  1115. {stmt: "a := vec3(1) << vec2(2); _ = a", err: true},
  1116. {stmt: "a := vec2(1) << ivec2(2); _ = a", err: true},
  1117. {stmt: "a := ivec2(1) << vec2(2); _ = a", err: true},
  1118. {stmt: "a := vec3(1) << ivec2(2); _ = a", err: true},
  1119. {stmt: "a := ivec2(1) << vec3(2); _ = a", err: true},
  1120. {stmt: "a := 1 >> 2; _ = a", err: false},
  1121. {stmt: "a := 1 >> 2.0; _ = a", err: false},
  1122. {stmt: "a := 1.0 >> 2; _ = a", err: false},
  1123. {stmt: "a := 1.0 >> 2.0; _ = a", err: false},
  1124. {stmt: "a := 1.0 >> int(1); _ = a", err: false},
  1125. {stmt: "a := int(1) >> 2.0; _ = a", err: false},
  1126. {stmt: "a := ivec2(1) >> 2.0; _ = a", err: false},
  1127. {stmt: "var a = 1; b := a >> 2.0; _ = b", err: false},
  1128. {stmt: "var a = 1; b := 2.0 >> a; _ = b", err: false}, // PR: #2916
  1129. {stmt: "a := float(1.0) >> 2; _ = a", err: true},
  1130. {stmt: "a := 1 >> float(2.0); _ = a", err: true},
  1131. {stmt: "a := ivec2(1) >> 2; _ = a", err: false},
  1132. {stmt: "a := 1 >> ivec2(2); _ = a", err: true},
  1133. {stmt: "a := ivec2(1) >> float(2.0); _ = a", err: true},
  1134. {stmt: "a := float(1.0) >> ivec2(2); _ = a", err: true},
  1135. {stmt: "a := ivec2(1) >> ivec2(2); _ = a", err: false},
  1136. {stmt: "a := ivec3(1) >> ivec2(2); _ = a", err: true},
  1137. {stmt: "a := ivec2(1) >> ivec3(2); _ = a", err: true},
  1138. {stmt: "a := 1 >> vec2(2); _ = a", err: true},
  1139. {stmt: "a := vec2(1) >> 2; _ = a", err: true},
  1140. {stmt: "a := float(1.0) >> vec2(2); _ = a", err: true},
  1141. {stmt: "a := vec2(1) >> float(2.0); _ = a", err: true},
  1142. {stmt: "a := vec2(1) >> vec2(2); _ = a", err: true},
  1143. {stmt: "a := vec2(1) >> vec3(2); _ = a", err: true},
  1144. {stmt: "a := vec3(1) >> vec2(2); _ = a", err: true},
  1145. {stmt: "a := vec2(1) >> ivec2(2); _ = a", err: true},
  1146. {stmt: "a := ivec2(1) >> vec2(2); _ = a", err: true},
  1147. {stmt: "a := vec3(1) >> ivec2(2); _ = a", err: true},
  1148. {stmt: "a := ivec2(1) >> vec3(2); _ = a", err: true},
  1149. }
  1150. for _, c := range cases {
  1151. _, err := compileToIR([]byte(fmt.Sprintf(`package main
  1152. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  1153. %s
  1154. return dstPos
  1155. }`, c.stmt)))
  1156. if err == nil && c.err {
  1157. t.Errorf("%s must return an error but does not", c.stmt)
  1158. } else if err != nil && !c.err {
  1159. t.Errorf("%s must not return nil but returned %v", c.stmt, err)
  1160. }
  1161. }
  1162. }
  1163. func TestSyntaxOperatorShiftAssign(t *testing.T) {
  1164. cases := []struct {
  1165. stmt string
  1166. err bool
  1167. }{
  1168. {stmt: "a := 1; a <<= 2; _ = a", err: false},
  1169. {stmt: "a := 1; a <<= 2.0; _ = a", err: false},
  1170. {stmt: "a := float(1.0); a <<= 2; _ = a", err: true},
  1171. {stmt: "a := 1; a <<= float(2.0); _ = a", err: true},
  1172. {stmt: "a := ivec2(1); a <<= 2; _ = a", err: false},
  1173. {stmt: "a := 1; a <<= ivec2(2); _ = a", err: true},
  1174. {stmt: "a := ivec2(1); a <<= float(2.0); _ = a", err: true},
  1175. {stmt: "a := float(1.0); a <<= ivec2(2); _ = a", err: true},
  1176. {stmt: "a := ivec2(1); a <<= ivec2(2); _ = a", err: false},
  1177. {stmt: "a := ivec3(1); a <<= ivec2(2); _ = a", err: true},
  1178. {stmt: "a := ivec2(1); a <<= ivec3(2); _ = a", err: true},
  1179. {stmt: "a := 1; a <<= vec2(2); _ = a", err: true},
  1180. {stmt: "a := vec2(1); a <<= 2; _ = a", err: true},
  1181. {stmt: "a := float(1.0); a <<= vec2(2); _ = a", err: true},
  1182. {stmt: "a := vec2(1); a <<= float(2.0); _ = a", err: true},
  1183. {stmt: "a := vec2(1); a <<= vec2(2); _ = a", err: true},
  1184. {stmt: "a := vec2(1); a <<= vec3(2); _ = a", err: true},
  1185. {stmt: "a := vec3(1); a <<= vec2(2); _ = a", err: true},
  1186. {stmt: "a := vec2(1); a <<= ivec2(2); _ = a", err: true},
  1187. {stmt: "a := ivec2(1); a <<= vec2(2); _ = a", err: true},
  1188. {stmt: "a := vec3(1); a <<= ivec2(2); _ = a", err: true},
  1189. {stmt: "a := ivec2(1); a <<= vec3(2); _ = a", err: true},
  1190. {stmt: "const c = 2; a := 1; a <<= c; _ = a", err: false},
  1191. {stmt: "const c = 2.0; a := 1; a <<= c; _ = a", err: false},
  1192. {stmt: "const c = 2; a := float(1.0); a <<= c; _ = a", err: true},
  1193. {stmt: "const c float = 2; a := 1; a <<= c; _ = a", err: true},
  1194. {stmt: "const c float = 2.0; a := 1; a <<= c; _ = a", err: true},
  1195. {stmt: "const c int = 2; a := ivec2(1); a <<= c; _ = a", err: false},
  1196. {stmt: "const c int = 2; a := vec2(1); a <<= c; _ = a", err: true},
  1197. {stmt: "a := 1; a >>= 2; _ = a", err: false},
  1198. {stmt: "a := 1; a >>= 2.0; _ = a", err: false},
  1199. {stmt: "a := float(1.0); a >>= 2; _ = a", err: true},
  1200. {stmt: "a := 1; a >>= float(2.0); _ = a", err: true},
  1201. {stmt: "a := ivec2(1); a >>= 2; _ = a", err: false},
  1202. {stmt: "a := 1; a >>= ivec2(2); _ = a", err: true},
  1203. {stmt: "a := ivec2(1); a >>= float(2.0); _ = a", err: true},
  1204. {stmt: "a := float(1.0); a >>= ivec2(2); _ = a", err: true},
  1205. {stmt: "a := ivec2(1); a >>= ivec2(2); _ = a", err: false},
  1206. {stmt: "a := ivec3(1); a >>= ivec2(2); _ = a", err: true},
  1207. {stmt: "a := ivec2(1); a >>= ivec3(2); _ = a", err: true},
  1208. {stmt: "a := 1; a >>= vec2(2); _ = a", err: true},
  1209. {stmt: "a := vec2(1); a >>= 2; _ = a", err: true},
  1210. {stmt: "a := float(1.0); a >>= vec2(2); _ = a", err: true},
  1211. {stmt: "a := vec2(1); a >>= float(2.0); _ = a", err: true},
  1212. {stmt: "a := vec2(1); a >>= vec2(2); _ = a", err: true},
  1213. {stmt: "a := vec2(1); a >>= vec3(2); _ = a", err: true},
  1214. {stmt: "a := vec3(1); a >>= vec2(2); _ = a", err: true},
  1215. {stmt: "a := vec2(1); a >>= ivec2(2); _ = a", err: true},
  1216. {stmt: "a := ivec2(1); a >>= vec2(2); _ = a", err: true},
  1217. {stmt: "a := vec3(1); a >>= ivec2(2); _ = a", err: true},
  1218. {stmt: "a := ivec2(1); a >>= vec3(2); _ = a", err: true},
  1219. {stmt: "const c = 2; a := 1; a >>= c; _ = a", err: false},
  1220. {stmt: "const c = 2.0; a := 1; a >>= c; _ = a", err: false},
  1221. {stmt: "const c = 2; a := float(1.0); a >>= c; _ = a", err: true},
  1222. {stmt: "const c float = 2; a := 1; a >>= c; _ = a", err: true},
  1223. {stmt: "const c float = 2.0; a := 1; a >>= c; _ = a", err: true},
  1224. {stmt: "const c int = 2; a := ivec2(1); a >>= c; _ = a", err: false},
  1225. {stmt: "const c int = 2; a := vec2(1); a >>= c; _ = a", err: true},
  1226. }
  1227. for _, c := range cases {
  1228. _, err := compileToIR([]byte(fmt.Sprintf(`package main
  1229. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  1230. %s
  1231. return dstPos
  1232. }`, c.stmt)))
  1233. if err == nil && c.err {
  1234. t.Errorf("%s must return an error but does not", c.stmt)
  1235. } else if err != nil && !c.err {
  1236. t.Errorf("%s must not return nil but returned %v", c.stmt, err)
  1237. }
  1238. }
  1239. }
  1240. // Issue #1971
  1241. func TestSyntaxOperatorMultiplyAssign(t *testing.T) {
  1242. cases := []struct {
  1243. stmt string
  1244. err bool
  1245. }{
  1246. {stmt: "a := 1.0; a *= 2", err: false},
  1247. {stmt: "a := 1.0; a *= 2.0", err: false},
  1248. {stmt: "const c = 2; a := 1.0; a *= c", err: false},
  1249. {stmt: "const c = 2.0; a := 1.0; a *= c", err: false},
  1250. {stmt: "const c int = 2; a := 1.0; a *= c", err: true},
  1251. {stmt: "const c int = 2.0; a := 1.0; a *= c", err: true},
  1252. {stmt: "const c float = 2; a := 1.0; a *= c", err: false},
  1253. {stmt: "const c float = 2.0; a := 1.0; a *= c", err: false},
  1254. {stmt: "a := 1.0; a *= int(2)", err: true},
  1255. {stmt: "a := 1.0; a *= vec2(2)", err: true},
  1256. {stmt: "a := 1.0; a *= vec3(2)", err: true},
  1257. {stmt: "a := 1.0; a *= vec4(2)", err: true},
  1258. {stmt: "a := 1.0; a *= ivec2(2)", err: true},
  1259. {stmt: "a := 1.0; a *= ivec3(2)", err: true},
  1260. {stmt: "a := 1.0; a *= ivec4(2)", err: true},
  1261. {stmt: "a := 1.0; a *= mat2(2)", err: true},
  1262. {stmt: "a := 1.0; a *= mat3(2)", err: true},
  1263. {stmt: "a := 1.0; a *= mat4(2)", err: true},
  1264. {stmt: "a := vec2(1); a *= 2", err: false},
  1265. {stmt: "a := vec2(1); a *= 2.0", err: false},
  1266. {stmt: "const c = 2; a := vec2(1); a *= c", err: false},
  1267. {stmt: "const c = 2.0; a := vec2(1); a *= c", err: false},
  1268. {stmt: "const c int = 2; a := vec2(1); a *= c", err: true},
  1269. {stmt: "const c int = 2.0; a := vec2(1); a *= c", err: true},
  1270. {stmt: "const c float = 2; a := vec2(1); a *= c", err: false},
  1271. {stmt: "const c float = 2.0; a := vec2(1); a *= c", err: false},
  1272. {stmt: "a := vec2(1); a /= 2.0", err: false},
  1273. {stmt: "a := vec2(1); a += 2.0", err: false},
  1274. {stmt: "a := vec2(1); a *= int(2)", err: true},
  1275. {stmt: "a := vec2(1); a *= float(2)", err: false},
  1276. {stmt: "a := vec2(1); a /= float(2)", err: false},
  1277. {stmt: "a := vec2(1); a *= vec2(2)", err: false},
  1278. {stmt: "a := vec2(1); a += vec2(2)", err: false},
  1279. {stmt: "a := vec2(1); a *= vec3(2)", err: true},
  1280. {stmt: "a := vec2(1); a *= vec4(2)", err: true},
  1281. {stmt: "a := vec2(1); a *= ivec2(2)", err: true},
  1282. {stmt: "a := vec2(1); a += ivec2(2)", err: true},
  1283. {stmt: "a := vec2(1); a *= ivec3(2)", err: true},
  1284. {stmt: "a := vec2(1); a *= ivec4(2)", err: true},
  1285. {stmt: "a := vec2(1); a *= mat2(2)", err: false},
  1286. {stmt: "a := vec2(1); a += mat2(2)", err: true},
  1287. {stmt: "a := vec2(1); a /= mat2(2)", err: true},
  1288. {stmt: "a := vec2(1); a *= mat3(2)", err: true},
  1289. {stmt: "a := vec2(1); a *= mat4(2)", err: true},
  1290. {stmt: "a := ivec2(1); a *= 2", err: false},
  1291. {stmt: "a := ivec2(1); a *= 2.0", err: false},
  1292. {stmt: "const c = 2; a := ivec2(1); a *= c", err: false},
  1293. {stmt: "const c = 2.0; a := ivec2(1); a *= c", err: false},
  1294. {stmt: "const c int = 2; a := ivec2(1); a *= c", err: false},
  1295. {stmt: "const c int = 2.0; a := ivec2(1); a *= c", err: false},
  1296. {stmt: "const c float = 2; a := ivec2(1); a *= c", err: true},
  1297. {stmt: "const c float = 2.0; a := ivec2(1); a *= c", err: true},
  1298. {stmt: "a := ivec2(1); a /= 2.0", err: false},
  1299. {stmt: "a := ivec2(1); a += 2.0", err: false},
  1300. {stmt: "a := ivec2(1); a *= int(2)", err: false},
  1301. {stmt: "a := ivec2(1); a *= float(2)", err: true},
  1302. {stmt: "a := ivec2(1); a /= float(2)", err: true},
  1303. {stmt: "a := ivec2(1); a *= vec2(2)", err: true},
  1304. {stmt: "a := ivec2(1); a += vec2(2)", err: true},
  1305. {stmt: "a := ivec2(1); a *= vec3(2)", err: true},
  1306. {stmt: "a := ivec2(1); a *= vec4(2)", err: true},
  1307. {stmt: "a := ivec2(1); a *= ivec2(2)", err: false},
  1308. {stmt: "a := ivec2(1); a += ivec2(2)", err: false},
  1309. {stmt: "a := ivec2(1); a *= ivec3(2)", err: true},
  1310. {stmt: "a := ivec2(1); a *= ivec4(2)", err: true},
  1311. {stmt: "a := ivec2(1); a *= mat2(2)", err: true},
  1312. {stmt: "a := ivec2(1); a += mat2(2)", err: true},
  1313. {stmt: "a := ivec2(1); a /= mat2(2)", err: true},
  1314. {stmt: "a := ivec2(1); a *= mat3(2)", err: true},
  1315. {stmt: "a := ivec2(1); a *= mat4(2)", err: true},
  1316. {stmt: "a := mat2(1); a *= 2", err: false},
  1317. {stmt: "a := mat2(1); a *= 2.0", err: false},
  1318. {stmt: "const c = 2; a := mat2(1); a *= c", err: false},
  1319. {stmt: "const c = 2.0; a := mat2(1); a *= c", err: false},
  1320. {stmt: "const c int = 2; a := mat2(1); a *= c", err: true},
  1321. {stmt: "const c int = 2.0; a := mat2(1); a *= c", err: true},
  1322. {stmt: "const c float = 2; a := mat2(1); a *= c", err: false},
  1323. {stmt: "const c float = 2.0; a := mat2(1); a *= c", err: false},
  1324. {stmt: "a := mat2(1); a /= 2.0", err: false},
  1325. {stmt: "a := mat2(1); a += 2.0", err: true},
  1326. {stmt: "a := mat2(1); a *= int(2)", err: true},
  1327. {stmt: "a := mat2(1); a *= float(2)", err: false},
  1328. {stmt: "a := mat2(1); a /= float(2)", err: false},
  1329. {stmt: "a := mat2(1); a *= vec2(2)", err: true},
  1330. {stmt: "a := mat2(1); a += vec2(2)", err: true},
  1331. {stmt: "a := mat2(1); a *= vec3(2)", err: true},
  1332. {stmt: "a := mat2(1); a *= vec4(2)", err: true},
  1333. {stmt: "a := mat2(1); a *= ivec2(2)", err: true},
  1334. {stmt: "a := mat2(1); a += ivec2(2)", err: true},
  1335. {stmt: "a := mat2(1); a *= ivec3(2)", err: true},
  1336. {stmt: "a := mat2(1); a *= ivec4(2)", err: true},
  1337. {stmt: "a := mat2(1); a *= mat2(2)", err: false},
  1338. {stmt: "a := mat2(1); a += mat2(2)", err: false},
  1339. {stmt: "a := mat2(1); a /= mat2(2)", err: true},
  1340. {stmt: "a := mat2(1); a *= mat3(2)", err: true},
  1341. {stmt: "a := mat2(1); a *= mat4(2)", err: true},
  1342. }
  1343. for _, c := range cases {
  1344. _, err := compileToIR([]byte(fmt.Sprintf(`package main
  1345. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  1346. %s
  1347. return dstPos
  1348. }`, c.stmt)))
  1349. if err == nil && c.err {
  1350. t.Errorf("%s must return an error but does not", c.stmt)
  1351. } else if err != nil && !c.err {
  1352. t.Errorf("%s must not return nil but returned %v", c.stmt, err)
  1353. }
  1354. }
  1355. }
  1356. // Issue #2754
  1357. func TestSyntaxBitwiseOperatorAssign(t *testing.T) {
  1358. cases := []struct {
  1359. stmt string
  1360. err bool
  1361. }{
  1362. {stmt: "a := 1; a &= 2", err: false},
  1363. {stmt: "a := 1; a &= 2.0", err: false},
  1364. {stmt: "const c = 2; a := 1; a &= c", err: false},
  1365. {stmt: "const c = 2.0; a := 1; a &= c", err: false},
  1366. {stmt: "const c int = 2; a := 1; a &= c", err: false},
  1367. {stmt: "const c int = 2.0; a := 1; a &= c", err: false},
  1368. {stmt: "const c float = 2; a := 1; a &= c", err: true},
  1369. {stmt: "const c float = 2.0; a := 1; a &= c", err: true},
  1370. {stmt: "a := 1; a &= int(2)", err: false},
  1371. {stmt: "a := 1; a &= vec2(2)", err: true},
  1372. {stmt: "a := 1; a &= vec3(2)", err: true},
  1373. {stmt: "a := 1; a &= vec4(2)", err: true},
  1374. {stmt: "a := 1; a &= ivec2(2)", err: true},
  1375. {stmt: "a := 1; a &= ivec3(2)", err: true},
  1376. {stmt: "a := 1; a &= ivec4(2)", err: true},
  1377. {stmt: "a := 1; a &= mat2(2)", err: true},
  1378. {stmt: "a := 1; a &= mat3(2)", err: true},
  1379. {stmt: "a := 1; a &= mat4(2)", err: true},
  1380. {stmt: "a := 1.0; a &= 2", err: true},
  1381. {stmt: "a := ivec2(1); a &= 2", err: false},
  1382. {stmt: "a := ivec2(1); a &= ivec2(1)", err: false},
  1383. {stmt: "a := ivec2(1); a &= ivec3(1)", err: true},
  1384. {stmt: "a := ivec2(1); a &= ivec4(1)", err: true},
  1385. {stmt: "a := vec2(1); a &= 2", err: true},
  1386. {stmt: "a := vec2(1); a &= vec2(2)", err: true},
  1387. {stmt: "a := mat2(1); a &= 2", err: true},
  1388. {stmt: "a := mat2(1); a &= mat2(2)", err: true},
  1389. {stmt: "a := 1; a |= 2", err: false},
  1390. {stmt: "a := 1; a |= 2.0", err: false},
  1391. {stmt: "const c = 2; a := 1; a |= c", err: false},
  1392. {stmt: "const c = 2.0; a := 1; a |= c", err: false},
  1393. {stmt: "const c int = 2; a := 1; a |= c", err: false},
  1394. {stmt: "const c int = 2.0; a := 1; a |= c", err: false},
  1395. {stmt: "const c float = 2; a := 1; a |= c", err: true},
  1396. {stmt: "const c float = 2.0; a := 1; a |= c", err: true},
  1397. {stmt: "a := 1; a |= int(2)", err: false},
  1398. {stmt: "a := 1; a |= vec2(2)", err: true},
  1399. {stmt: "a := 1; a |= vec3(2)", err: true},
  1400. {stmt: "a := 1; a |= vec4(2)", err: true},
  1401. {stmt: "a := 1; a |= ivec2(2)", err: true},
  1402. {stmt: "a := 1; a |= ivec3(2)", err: true},
  1403. {stmt: "a := 1; a |= ivec4(2)", err: true},
  1404. {stmt: "a := 1; a |= mat2(2)", err: true},
  1405. {stmt: "a := 1; a |= mat3(2)", err: true},
  1406. {stmt: "a := 1; a |= mat4(2)", err: true},
  1407. {stmt: "a := 1.0; a |= 2", err: true},
  1408. {stmt: "a := ivec2(1); a |= 2", err: false},
  1409. {stmt: "a := ivec2(1); a |= ivec2(1)", err: false},
  1410. {stmt: "a := ivec2(1); a |= ivec3(1)", err: true},
  1411. {stmt: "a := ivec2(1); a |= ivec4(1)", err: true},
  1412. {stmt: "a := vec2(1); a |= 2", err: true},
  1413. {stmt: "a := vec2(1); a |= vec2(2)", err: true},
  1414. {stmt: "a := mat2(1); a |= 2", err: true},
  1415. {stmt: "a := mat2(1); a |= mat2(2)", err: true},
  1416. {stmt: "a := 1; a ^= 2", err: false},
  1417. {stmt: "a := 1; a ^= 2.0", err: false},
  1418. {stmt: "const c = 2; a := 1; a ^= c", err: false},
  1419. {stmt: "const c = 2.0; a := 1; a ^= c", err: false},
  1420. {stmt: "const c int = 2; a := 1; a ^= c", err: false},
  1421. {stmt: "const c int = 2.0; a := 1; a ^= c", err: false},
  1422. {stmt: "const c float = 2; a := 1; a ^= c", err: true},
  1423. {stmt: "const c float = 2.0; a := 1; a ^= c", err: true},
  1424. {stmt: "a := 1; a ^= int(2)", err: false},
  1425. {stmt: "a := 1; a ^= vec2(2)", err: true},
  1426. {stmt: "a := 1; a ^= vec3(2)", err: true},
  1427. {stmt: "a := 1; a ^= vec4(2)", err: true},
  1428. {stmt: "a := 1; a ^= ivec2(2)", err: true},
  1429. {stmt: "a := 1; a ^= ivec3(2)", err: true},
  1430. {stmt: "a := 1; a ^= ivec4(2)", err: true},
  1431. {stmt: "a := 1; a ^= mat2(2)", err: true},
  1432. {stmt: "a := 1; a ^= mat3(2)", err: true},
  1433. {stmt: "a := 1; a ^= mat4(2)", err: true},
  1434. {stmt: "a := 1.0; a ^= 2", err: true},
  1435. {stmt: "a := ivec2(1); a ^= 2", err: false},
  1436. {stmt: "a := ivec2(1); a ^= ivec2(1)", err: false},
  1437. {stmt: "a := ivec2(1); a ^= ivec3(1)", err: true},
  1438. {stmt: "a := ivec2(1); a ^= ivec4(1)", err: true},
  1439. {stmt: "a := vec2(1); a ^= 2", err: true},
  1440. {stmt: "a := vec2(1); a ^= vec2(2)", err: true},
  1441. {stmt: "a := mat2(1); a ^= 2", err: true},
  1442. {stmt: "a := mat2(1); a ^= mat2(2)", err: true},
  1443. }
  1444. for _, c := range cases {
  1445. _, err := compileToIR([]byte(fmt.Sprintf(`package main
  1446. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  1447. %s
  1448. return dstPos
  1449. }`, c.stmt)))
  1450. if err == nil && c.err {
  1451. t.Errorf("%s must return an error but does not", c.stmt)
  1452. } else if err != nil && !c.err {
  1453. t.Errorf("%s must not return nil but returned %v", c.stmt, err)
  1454. }
  1455. }
  1456. }
  1457. func TestSyntaxAtan(t *testing.T) {
  1458. cases := []struct {
  1459. stmt string
  1460. err bool
  1461. }{
  1462. // `atan` takes 1 argument.
  1463. {stmt: "_ = atan(vec4(0))", err: false},
  1464. {stmt: "_ = atan(vec4(0), vec4(0))", err: true},
  1465. // `atan2` takes 2 arguments.
  1466. {stmt: "_ = atan2(vec4(0))", err: true},
  1467. {stmt: "_ = atan2(vec4(0), vec4(0))", err: false},
  1468. }
  1469. for _, c := range cases {
  1470. _, err := compileToIR([]byte(fmt.Sprintf(`package main
  1471. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  1472. %s
  1473. return dstPos
  1474. }`, c.stmt)))
  1475. if err == nil && c.err {
  1476. t.Errorf("%s must return an error but does not", c.stmt)
  1477. } else if err != nil && !c.err {
  1478. t.Errorf("%s must not return nil but returned %v", c.stmt, err)
  1479. }
  1480. }
  1481. }
  1482. // Issue #1972
  1483. func TestSyntaxType(t *testing.T) {
  1484. if _, err := compileToIR([]byte(`package main
  1485. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  1486. var x vec2 = vec3(0)
  1487. _ = x
  1488. return color
  1489. }
  1490. `)); err == nil {
  1491. t.Errorf("error must be non-nil but was nil")
  1492. }
  1493. if _, err := compileToIR([]byte(`package main
  1494. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  1495. var x, y vec2 = vec2(0), vec3(0)
  1496. _, _ = x, y
  1497. return color
  1498. }
  1499. `)); err == nil {
  1500. t.Errorf("error must be non-nil but was nil")
  1501. }
  1502. if _, err := compileToIR([]byte(`package main
  1503. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  1504. var x vec2
  1505. x = vec3(0)
  1506. _ = x
  1507. return color
  1508. }
  1509. `)); err == nil {
  1510. t.Errorf("error must be non-nil but was nil")
  1511. }
  1512. if _, err := compileToIR([]byte(`package main
  1513. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  1514. var x, y vec2
  1515. x, y = vec2(0), vec3(0)
  1516. _ = x
  1517. _ = y
  1518. return color
  1519. }
  1520. `)); err == nil {
  1521. t.Errorf("error must be non-nil but was nil")
  1522. }
  1523. if _, err := compileToIR([]byte(`package main
  1524. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  1525. var x vec2
  1526. x = 0
  1527. _ = x
  1528. return color
  1529. }
  1530. `)); err == nil {
  1531. t.Errorf("error must be non-nil but was nil")
  1532. }
  1533. if _, err := compileToIR([]byte(`package main
  1534. func Foo() (vec3, vec3) {
  1535. return vec3(0), vec3(1)
  1536. }
  1537. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  1538. var x, y vec2 = Foo()
  1539. _ = x
  1540. _ = y
  1541. return color
  1542. }
  1543. `)); err == nil {
  1544. t.Errorf("error must be non-nil but was nil")
  1545. }
  1546. if _, err := compileToIR([]byte(`package main
  1547. func Foo() (vec3, vec3) {
  1548. return vec3(0), vec3(1)
  1549. }
  1550. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  1551. var x, y vec2
  1552. x, y = Foo()
  1553. _ = x
  1554. _ = y
  1555. return color
  1556. }
  1557. `)); err == nil {
  1558. t.Errorf("error must be non-nil but was nil")
  1559. }
  1560. }
  1561. // Issue #1972
  1562. func TestSyntaxTypeBlankVar(t *testing.T) {
  1563. if _, err := compileToIR([]byte(`package main
  1564. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  1565. var _ vec2 = vec3(0)
  1566. return color
  1567. }
  1568. `)); err == nil {
  1569. t.Errorf("error must be non-nil but was nil")
  1570. }
  1571. if _, err := compileToIR([]byte(`package main
  1572. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  1573. var _, _ vec2 = vec2(0), vec3(0)
  1574. return color
  1575. }
  1576. `)); err == nil {
  1577. t.Errorf("error must be non-nil but was nil")
  1578. }
  1579. if _, err := compileToIR([]byte(`package main
  1580. func Foo() (vec3, vec3) {
  1581. return vec3(0), vec3(1)
  1582. }
  1583. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  1584. var _, _ vec2 = Foo()
  1585. return color
  1586. }
  1587. `)); err == nil {
  1588. t.Errorf("error must be non-nil but was nil")
  1589. }
  1590. }
  1591. // Issue #2032
  1592. func TestSyntaxTypeFuncCall(t *testing.T) {
  1593. if _, err := compileToIR([]byte(`package main
  1594. func Foo(x vec2) {
  1595. }
  1596. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  1597. Foo(0)
  1598. return color
  1599. }
  1600. `)); err == nil {
  1601. t.Errorf("error must be non-nil but was nil")
  1602. }
  1603. if _, err := compileToIR([]byte(`package main
  1604. func Foo(x vec2, y vec3) {
  1605. }
  1606. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  1607. Foo(0, 1)
  1608. return color
  1609. }
  1610. `)); err == nil {
  1611. t.Errorf("error must be non-nil but was nil")
  1612. }
  1613. if _, err := compileToIR([]byte(`package main
  1614. func Foo(x vec2, y vec3) {
  1615. }
  1616. func Bar() (int, int) {
  1617. return 0, 1
  1618. }
  1619. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  1620. Foo(Bar())
  1621. return color
  1622. }
  1623. `)); err == nil {
  1624. t.Errorf("error must be non-nil but was nil")
  1625. }
  1626. // Issue #2965
  1627. if _, err := compileToIR([]byte(`package main
  1628. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  1629. abs(sign)
  1630. return color
  1631. }
  1632. `)); err == nil {
  1633. t.Errorf("error must be non-nil but was nil")
  1634. }
  1635. }
  1636. // Issue #2184
  1637. func TestSyntaxConstructorFuncType(t *testing.T) {
  1638. cases := []struct {
  1639. stmt string
  1640. err bool
  1641. }{
  1642. {stmt: "a := bool(false); _ = a", err: false},
  1643. {stmt: "i := false; a := bool(i); _ = a", err: false},
  1644. {stmt: "a := bool(1); _ = a", err: true},
  1645. {stmt: "a := bool(1.0); _ = a", err: true},
  1646. {stmt: "a := bool(); _ = a", err: true},
  1647. {stmt: "a := bool(false, true); _ = a", err: true},
  1648. {stmt: "a := int(1); _ = a", err: false},
  1649. {stmt: "a := int(1.0); _ = a", err: false},
  1650. {stmt: "i := 1; a := int(i); _ = a", err: false},
  1651. {stmt: "i := 1.0; a := int(i); _ = a", err: false},
  1652. {stmt: "i := 1.1; a := int(i); _ = a", err: false},
  1653. {stmt: "a := int(1.1); _ = a", err: true},
  1654. {stmt: "a := int(false); _ = a", err: true},
  1655. {stmt: "a := int(); _ = a", err: true},
  1656. {stmt: "a := int(1, 2); _ = a", err: true},
  1657. {stmt: "a := float(1); _ = a", err: false},
  1658. {stmt: "a := float(1.0); _ = a", err: false},
  1659. {stmt: "a := float(1.1); _ = a", err: false},
  1660. {stmt: "i := 1; a := float(i); _ = a", err: false},
  1661. {stmt: "i := 1.0; a := float(i); _ = a", err: false},
  1662. {stmt: "i := 1.1; a := float(i); _ = a", err: false},
  1663. {stmt: "a := float(false); _ = a", err: true},
  1664. {stmt: "a := float(); _ = a", err: true},
  1665. {stmt: "a := float(1, 2); _ = a", err: true},
  1666. {stmt: "a := vec2(1); _ = a", err: false},
  1667. {stmt: "a := vec2(1.0); _ = a", err: false},
  1668. {stmt: "i := 1; a := vec2(i); _ = a", err: true},
  1669. {stmt: "i := 1.0; a := vec2(i); _ = a", err: false},
  1670. {stmt: "a := vec2(vec2(1)); _ = a", err: false},
  1671. {stmt: "a := vec2(vec3(1)); _ = a", err: true},
  1672. {stmt: "a := vec2(ivec2(1)); _ = a", err: false},
  1673. {stmt: "a := vec2(ivec3(1)); _ = a", err: true},
  1674. {stmt: "a := vec2(1, 1); _ = a", err: false},
  1675. {stmt: "a := vec2(1.0, 1.0); _ = a", err: false},
  1676. {stmt: "a := vec2(1.1, 1.1); _ = a", err: false},
  1677. {stmt: "i := 1; a := vec2(i, i); _ = a", err: true},
  1678. {stmt: "i := 1.0; a := vec2(i, i); _ = a", err: false},
  1679. {stmt: "a := vec2(vec2(1), 1); _ = a", err: true},
  1680. {stmt: "a := vec2(1, vec2(1)); _ = a", err: true},
  1681. {stmt: "a := vec2(vec2(1), vec2(1)); _ = a", err: true},
  1682. {stmt: "a := vec2(1, 1, 1); _ = a", err: true},
  1683. {stmt: "a := vec3(1); _ = a", err: false},
  1684. {stmt: "a := vec3(1.0); _ = a", err: false},
  1685. {stmt: "a := vec3(1.1); _ = a", err: false},
  1686. {stmt: "i := 1; a := vec3(i); _ = a", err: true},
  1687. {stmt: "i := 1.0; a := vec3(i); _ = a", err: false},
  1688. {stmt: "a := vec3(vec3(1)); _ = a", err: false},
  1689. {stmt: "a := vec3(vec2(1)); _ = a", err: true},
  1690. {stmt: "a := vec3(vec4(1)); _ = a", err: true},
  1691. {stmt: "a := vec3(ivec3(1)); _ = a", err: false},
  1692. {stmt: "a := vec3(ivec2(1)); _ = a", err: true},
  1693. {stmt: "a := vec3(ivec4(1)); _ = a", err: true},
  1694. {stmt: "a := vec3(1, 1, 1); _ = a", err: false},
  1695. {stmt: "a := vec3(1.0, 1.0, 1.0); _ = a", err: false},
  1696. {stmt: "a := vec3(1.1, 1.1, 1.1); _ = a", err: false},
  1697. {stmt: "i := 1; a := vec3(i, i, i); _ = a", err: true},
  1698. {stmt: "i := 1.0; a := vec3(i, i, i); _ = a", err: false},
  1699. {stmt: "a := vec3(vec2(1), 1); _ = a", err: false},
  1700. {stmt: "a := vec3(1, vec2(1)); _ = a", err: false},
  1701. {stmt: "a := vec3(ivec2(1), 1); _ = a", err: true},
  1702. {stmt: "a := vec3(1, ivec2(1)); _ = a", err: true},
  1703. {stmt: "a := vec3(vec3(1), 1); _ = a", err: true},
  1704. {stmt: "a := vec3(1, vec3(1)); _ = a", err: true},
  1705. {stmt: "a := vec3(vec3(1), vec3(1), vec3(1)); _ = a", err: true},
  1706. {stmt: "a := vec3(1, 1, 1, 1); _ = a", err: true},
  1707. {stmt: "a := vec4(1); _ = a", err: false},
  1708. {stmt: "a := vec4(1.0); _ = a", err: false},
  1709. {stmt: "i := 1; a := vec4(i); _ = a", err: true},
  1710. {stmt: "i := 1.0; a := vec4(i); _ = a", err: false},
  1711. {stmt: "a := vec4(vec4(1)); _ = a", err: false},
  1712. {stmt: "a := vec4(vec2(1)); _ = a", err: true},
  1713. {stmt: "a := vec4(vec3(1)); _ = a", err: true},
  1714. {stmt: "a := vec4(ivec4(1)); _ = a", err: false},
  1715. {stmt: "a := vec4(ivec2(1)); _ = a", err: true},
  1716. {stmt: "a := vec4(ivec3(1)); _ = a", err: true},
  1717. {stmt: "a := vec4(1, 1, 1, 1); _ = a", err: false},
  1718. {stmt: "a := vec4(1.0, 1.0, 1.0, 1.0); _ = a", err: false},
  1719. {stmt: "a := vec4(1.1, 1.1, 1.1, 1.1); _ = a", err: false},
  1720. {stmt: "i := 1; a := vec4(i, i, i, i); _ = a", err: true},
  1721. {stmt: "i := 1.0; a := vec4(i, i, i, i); _ = a", err: false},
  1722. {stmt: "a := vec4(vec2(1), 1, 1); _ = a", err: false},
  1723. {stmt: "a := vec4(1, vec2(1), 1); _ = a", err: false},
  1724. {stmt: "a := vec4(ivec2(1), 1, 1); _ = a", err: true},
  1725. {stmt: "a := vec4(1, ivec2(1), 1); _ = a", err: true},
  1726. {stmt: "a := vec4(1, 1, vec2(1)); _ = a", err: false},
  1727. {stmt: "a := vec4(vec2(1), vec2(1)); _ = a", err: false},
  1728. {stmt: "a := vec4(ivec2(1), ivec2(1)); _ = a", err: true},
  1729. {stmt: "a := vec4(vec3(1), 1); _ = a", err: false},
  1730. {stmt: "a := vec4(1, vec3(1)); _ = a", err: false},
  1731. {stmt: "a := vec4(ivec3(1), 1); _ = a", err: true},
  1732. {stmt: "a := vec4(1, ivec3(1)); _ = a", err: true},
  1733. {stmt: "a := vec4(vec4(1), 1); _ = a", err: true},
  1734. {stmt: "a := vec4(1, vec4(1)); _ = a", err: true},
  1735. {stmt: "a := vec4(vec4(1), vec4(1), vec4(1), vec4(1)); _ = a", err: true},
  1736. {stmt: "a := vec4(1, 1, 1, 1, 1); _ = a", err: true},
  1737. {stmt: "a := ivec2(1); _ = a", err: false},
  1738. {stmt: "a := ivec2(1.0); _ = a", err: false},
  1739. {stmt: "i := 1; a := ivec2(i); _ = a", err: false},
  1740. {stmt: "i := 1.0; a := ivec2(i); _ = a", err: true},
  1741. {stmt: "a := ivec2(vec2(1)); _ = a", err: false},
  1742. {stmt: "a := ivec2(vec3(1)); _ = a", err: true},
  1743. {stmt: "a := ivec2(ivec2(1)); _ = a", err: false},
  1744. {stmt: "a := ivec2(ivec3(1)); _ = a", err: true},
  1745. {stmt: "a := ivec2(1, 1); _ = a", err: false},
  1746. {stmt: "a := ivec2(1.0, 1.0); _ = a", err: false},
  1747. {stmt: "i := 1; a := ivec2(i, i); _ = a", err: false},
  1748. {stmt: "i := 1.0; a := ivec2(i, i); _ = a", err: true},
  1749. {stmt: "a := ivec2(vec2(1), 1); _ = a", err: true},
  1750. {stmt: "a := ivec2(1, vec2(1)); _ = a", err: true},
  1751. {stmt: "a := ivec2(ivec2(1), 1); _ = a", err: true},
  1752. {stmt: "a := ivec2(1, ivec2(1)); _ = a", err: true},
  1753. {stmt: "a := ivec2(ivec2(1), ivec2(1)); _ = a", err: true},
  1754. {stmt: "a := ivec2(1, 1, 1); _ = a", err: true},
  1755. {stmt: "a := ivec3(1); _ = a", err: false},
  1756. {stmt: "a := ivec3(1.0); _ = a", err: false},
  1757. {stmt: "a := ivec3(1.1); _ = a", err: true},
  1758. {stmt: "i := 1; a := ivec3(i); _ = a", err: false},
  1759. {stmt: "i := 1.0; a := ivec3(i); _ = a", err: true},
  1760. {stmt: "a := ivec3(vec3(1)); _ = a", err: false},
  1761. {stmt: "a := ivec3(vec2(1)); _ = a", err: true},
  1762. {stmt: "a := ivec3(vec4(1)); _ = a", err: true},
  1763. {stmt: "a := ivec3(ivec3(1)); _ = a", err: false},
  1764. {stmt: "a := ivec3(ivec2(1)); _ = a", err: true},
  1765. {stmt: "a := ivec3(ivec4(1)); _ = a", err: true},
  1766. {stmt: "a := ivec3(1, 1, 1); _ = a", err: false},
  1767. {stmt: "a := ivec3(1.0, 1.0, 1.0); _ = a", err: false},
  1768. {stmt: "a := ivec3(1.1, 1.1, 1.1); _ = a", err: true},
  1769. {stmt: "i := 1; a := ivec3(i, i, i); _ = a", err: false},
  1770. {stmt: "i := 1.0; a := ivec3(i, i, i); _ = a", err: true},
  1771. {stmt: "a := ivec3(vec2(1), 1); _ = a", err: true},
  1772. {stmt: "a := ivec3(1, vec2(1)); _ = a", err: true},
  1773. {stmt: "a := ivec3(ivec2(1), 1); _ = a", err: false},
  1774. {stmt: "a := ivec3(1, ivec2(1)); _ = a", err: false},
  1775. {stmt: "a := ivec3(vec3(1), 1); _ = a", err: true},
  1776. {stmt: "a := ivec3(1, vec3(1)); _ = a", err: true},
  1777. {stmt: "a := ivec3(vec3(1), vec3(1), vec3(1)); _ = a", err: true},
  1778. {stmt: "a := ivec3(1, 1, 1, 1); _ = a", err: true},
  1779. {stmt: "a := ivec4(1); _ = a", err: false},
  1780. {stmt: "a := ivec4(1.0); _ = a", err: false},
  1781. {stmt: "i := 1; a := ivec4(i); _ = a", err: false},
  1782. {stmt: "i := 1.0; a := ivec4(i); _ = a", err: true},
  1783. {stmt: "a := ivec4(vec4(1)); _ = a", err: false},
  1784. {stmt: "a := ivec4(vec2(1)); _ = a", err: true},
  1785. {stmt: "a := ivec4(vec3(1)); _ = a", err: true},
  1786. {stmt: "a := ivec4(ivec4(1)); _ = a", err: false},
  1787. {stmt: "a := ivec4(ivec2(1)); _ = a", err: true},
  1788. {stmt: "a := ivec4(ivec3(1)); _ = a", err: true},
  1789. {stmt: "a := ivec4(1, 1, 1, 1); _ = a", err: false},
  1790. {stmt: "a := ivec4(1.0, 1.0, 1.0, 1.0); _ = a", err: false},
  1791. {stmt: "a := ivec4(1.1, 1.1, 1.1, 1.1); _ = a", err: true},
  1792. {stmt: "i := 1; a := ivec4(i, i, i, i); _ = a", err: false},
  1793. {stmt: "i := 1.0; a := ivec4(i, i, i, i); _ = a", err: true},
  1794. {stmt: "a := ivec4(vec2(1), 1, 1); _ = a", err: true},
  1795. {stmt: "a := ivec4(1, vec2(1), 1); _ = a", err: true},
  1796. {stmt: "a := ivec4(1, 1, vec2(1)); _ = a", err: true},
  1797. {stmt: "a := ivec4(ivec2(1), 1, 1); _ = a", err: false},
  1798. {stmt: "a := ivec4(1, ivec2(1), 1); _ = a", err: false},
  1799. {stmt: "a := ivec4(1, 1, ivec2(1)); _ = a", err: false},
  1800. {stmt: "a := ivec4(vec2(1), vec2(1)); _ = a", err: true},
  1801. {stmt: "a := ivec4(ivec2(1), ivec2(1)); _ = a", err: false},
  1802. {stmt: "a := ivec4(vec3(1), 1); _ = a", err: true},
  1803. {stmt: "a := ivec4(1, vec3(1)); _ = a", err: true},
  1804. {stmt: "a := ivec4(ivec3(1), 1); _ = a", err: false},
  1805. {stmt: "a := ivec4(1, ivec3(1)); _ = a", err: false},
  1806. {stmt: "a := ivec4(vec4(1), 1); _ = a", err: true},
  1807. {stmt: "a := ivec4(1, vec4(1)); _ = a", err: true},
  1808. {stmt: "a := ivec4(vec4(1), vec4(1), vec4(1), vec4(1)); _ = a", err: true},
  1809. {stmt: "a := ivec4(1, 1, 1, 1, 1); _ = a", err: true},
  1810. {stmt: "a := mat2(1); _ = a", err: false},
  1811. {stmt: "a := mat2(1.0); _ = a", err: false},
  1812. {stmt: "i := 1; a := mat2(i); _ = a", err: true},
  1813. {stmt: "i := 1.0; a := mat2(i); _ = a", err: false},
  1814. {stmt: "a := mat2(mat2(1)); _ = a", err: false},
  1815. {stmt: "a := mat2(vec2(1)); _ = a", err: true},
  1816. {stmt: "a := mat2(ivec2(1)); _ = a", err: true},
  1817. {stmt: "a := mat2(mat3(1)); _ = a", err: true},
  1818. {stmt: "a := mat2(mat4(1)); _ = a", err: true},
  1819. {stmt: "a := mat2(vec2(1), vec2(1)); _ = a", err: false},
  1820. {stmt: "a := mat2(ivec2(1), ivec2(1)); _ = a", err: true},
  1821. {stmt: "a := mat2(1, 1); _ = a", err: true},
  1822. {stmt: "a := mat2(1, vec2(1)); _ = a", err: true},
  1823. {stmt: "a := mat2(vec2(1), vec3(1)); _ = a", err: true},
  1824. {stmt: "a := mat2(mat2(1), mat2(1)); _ = a", err: true},
  1825. {stmt: "a := mat2(1, 1, 1, 1); _ = a", err: false},
  1826. {stmt: "a := mat2(1.0, 1.0, 1.0, 1.0); _ = a", err: false},
  1827. {stmt: "i := 1; a := mat2(i, i, i, i); _ = a", err: true},
  1828. {stmt: "i := 1.0; a := mat2(i, i, i, i); _ = a", err: false},
  1829. {stmt: "a := mat2(vec2(1), vec2(1), vec2(1), vec2(1)); _ = a", err: true},
  1830. {stmt: "a := mat2(1, 1, 1, vec2(1)); _ = a", err: true},
  1831. {stmt: "a := mat2(1, 1, 1, vec3(1)); _ = a", err: true},
  1832. {stmt: "a := mat2(mat2(1), mat2(1), mat2(1), mat2(1)); _ = a", err: true},
  1833. {stmt: "a := mat2(1, 1, 1); _ = a", err: true},
  1834. {stmt: "a := mat2(1, 1, 1, 1, 1); _ = a", err: true},
  1835. {stmt: "a := mat3(1); _ = a", err: false},
  1836. {stmt: "a := mat3(1.0); _ = a", err: false},
  1837. {stmt: "i := 1; a := mat3(i); _ = a", err: true},
  1838. {stmt: "i := 1.0; a := mat3(i); _ = a", err: false},
  1839. {stmt: "a := mat3(mat3(1)); _ = a", err: false},
  1840. {stmt: "a := mat3(vec2(1)); _ = a", err: true},
  1841. {stmt: "a := mat3(ivec2(1)); _ = a", err: true},
  1842. {stmt: "a := mat3(mat2(1)); _ = a", err: true},
  1843. {stmt: "a := mat3(mat4(1)); _ = a", err: true},
  1844. {stmt: "a := mat3(vec3(1), vec3(1), vec3(1)); _ = a", err: false},
  1845. {stmt: "a := mat3(ivec3(1), ivec3(1), ivec3(1)); _ = a", err: true},
  1846. {stmt: "a := mat3(1, 1, 1); _ = a", err: true},
  1847. {stmt: "a := mat3(1, 1, vec3(1)); _ = a", err: true},
  1848. {stmt: "a := mat3(vec3(1), vec3(1), vec4(1)); _ = a", err: true},
  1849. {stmt: "a := mat3(mat3(1), mat3(1), mat3(1)); _ = a", err: true},
  1850. {stmt: "a := mat3(1, 1, 1, 1, 1, 1, 1, 1, 1); _ = a", err: false},
  1851. {stmt: "a := mat3(1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0); _ = a", err: false},
  1852. {stmt: "i := 1; a := mat3(i, i, i, i, i, i, i, i, i); _ = a", err: true},
  1853. {stmt: "i := 1.0; a := mat3(i, i, i, i, i, i, i, i, i); _ = a", err: false},
  1854. {stmt: "a := mat3(vec3(1), vec3(1), vec3(1), vec3(1), vec3(1), vec3(1), vec3(1), vec3(1), vec3(1)); _ = a", err: true},
  1855. {stmt: "a := mat3(1, 1, 1, 1, 1, 1, 1, 1, vec2(1)); _ = a", err: true},
  1856. {stmt: "a := mat3(1, 1, 1, 1, 1, 1, 1, 1, vec3(1)); _ = a", err: true},
  1857. {stmt: "a := mat3(mat3(1), mat3(1), mat3(1), mat3(1), mat3(1), mat3(1), mat3(1), mat3(1), mat3(1)); _ = a", err: true},
  1858. {stmt: "a := mat3(1, 1, 1, 1, 1, 1, 1, 1); _ = a", err: true},
  1859. {stmt: "a := mat3(1, 1, 1, 1, 1, 1, 1, 1, 1, 1); _ = a", err: true},
  1860. {stmt: "a := mat4(1); _ = a", err: false},
  1861. {stmt: "a := mat4(1.0); _ = a", err: false},
  1862. {stmt: "i := 1; a := mat4(i); _ = a", err: true},
  1863. {stmt: "i := 1.0; a := mat4(i); _ = a", err: false},
  1864. {stmt: "a := mat4(mat4(1)); _ = a", err: false},
  1865. {stmt: "a := mat4(vec2(1)); _ = a", err: true},
  1866. {stmt: "a := mat4(ivec2(1)); _ = a", err: true},
  1867. {stmt: "a := mat4(mat2(1)); _ = a", err: true},
  1868. {stmt: "a := mat4(mat3(1)); _ = a", err: true},
  1869. {stmt: "a := mat4(vec4(1), vec4(1), vec4(1), vec4(1)); _ = a", err: false},
  1870. {stmt: "a := mat4(ivec4(1), ivec4(1), ivec4(1), ivec4(1)); _ = a", err: true},
  1871. {stmt: "a := mat4(1, 1, 1, 1); _ = a", err: true},
  1872. {stmt: "a := mat4(1, 1, 1, vec4(1)); _ = a", err: true},
  1873. {stmt: "a := mat4(vec4(1), vec4(1), vec4(1), vec2(1)); _ = a", err: true},
  1874. {stmt: "a := mat4(mat4(1), mat4(1), mat4(1), mat4(1)); _ = a", err: true},
  1875. {stmt: "a := mat4(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1); _ = a", err: false},
  1876. {stmt: "a := mat4(1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0); _ = a", err: false},
  1877. {stmt: "i := 1; a := mat4(i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i); _ = a", err: true},
  1878. {stmt: "i := 1.0; a := mat4(i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i); _ = a", err: false},
  1879. {stmt: "a := mat4(vec4(1), vec4(1), vec4(1), vec4(1), vec4(1), vec4(1), vec4(1), vec4(1), vec4(1), vec4(1), vec4(1), vec4(1), vec4(1), vec4(1), vec4(1), vec4(1)); _ = a", err: true},
  1880. {stmt: "a := mat4(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, vec2(1)); _ = a", err: true},
  1881. {stmt: "a := mat4(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, vec3(1)); _ = a", err: true},
  1882. {stmt: "a := mat4(mat4(1), mat4(1), mat4(1), mat4(1), mat4(1), mat4(1), mat4(1), mat4(1), mat4(1), mat4(1), mat4(1), mat4(1), mat4(1), mat4(1), mat4(1), mat4(1)); _ = a", err: true},
  1883. {stmt: "a := mat4(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1); _ = a", err: true},
  1884. {stmt: "a := mat4(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1); _ = a", err: true},
  1885. }
  1886. for _, c := range cases {
  1887. _, err := compileToIR([]byte(fmt.Sprintf(`package main
  1888. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  1889. %s
  1890. return dstPos
  1891. }`, c.stmt)))
  1892. if err == nil && c.err {
  1893. t.Errorf("%s must return an error but does not", c.stmt)
  1894. } else if err != nil && !c.err {
  1895. t.Errorf("%s must not return nil but returned %v", c.stmt, err)
  1896. }
  1897. }
  1898. }
  1899. // Issue #2248
  1900. func TestSyntaxDiscard(t *testing.T) {
  1901. if _, err := compileToIR([]byte(`package main
  1902. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  1903. if true {
  1904. discard()
  1905. }
  1906. return vec4(0)
  1907. }
  1908. `)); err != nil {
  1909. t.Error(err)
  1910. }
  1911. // discard without return doesn't work so far.
  1912. // TODO: Allow discard without return.
  1913. if _, err := compileToIR([]byte(`package main
  1914. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  1915. discard()
  1916. return vec4(0)
  1917. }
  1918. `)); err != nil {
  1919. t.Error(err)
  1920. }
  1921. if _, err := compileToIR([]byte(`package main
  1922. func foo() {
  1923. discard()
  1924. }
  1925. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  1926. foo()
  1927. return vec4(0)
  1928. }
  1929. `)); err == nil {
  1930. t.Errorf("error must be non-nil but was nil")
  1931. }
  1932. }
  1933. // Issue #2184
  1934. func TestSyntaxBuiltinFuncSingleArgType(t *testing.T) {
  1935. cases := []struct {
  1936. stmt string
  1937. err bool
  1938. }{
  1939. {stmt: "a := {{.Func}}(); _ = a", err: true},
  1940. {stmt: "a := {{.Func}}(false); _ = a", err: true},
  1941. {stmt: "a := {{.Func}}(1); _ = a", err: false},
  1942. {stmt: "a := {{.Func}}(1.0); _ = a", err: false},
  1943. {stmt: "a := {{.Func}}(int(1)); _ = a", err: true},
  1944. {stmt: "a := {{.Func}}(vec2(1)); _ = a", err: false},
  1945. {stmt: "a := {{.Func}}(vec3(1)); _ = a", err: false},
  1946. {stmt: "a := {{.Func}}(vec4(1)); _ = a", err: false},
  1947. {stmt: "a := {{.Func}}(ivec2(1)); _ = a", err: true},
  1948. {stmt: "a := {{.Func}}(ivec3(1)); _ = a", err: true},
  1949. {stmt: "a := {{.Func}}(ivec4(1)); _ = a", err: true},
  1950. {stmt: "a := {{.Func}}(mat2(1)); _ = a", err: true},
  1951. {stmt: "a := {{.Func}}(mat3(1)); _ = a", err: true},
  1952. {stmt: "a := {{.Func}}(mat4(1)); _ = a", err: true},
  1953. {stmt: "a := {{.Func}}(1, 1); _ = a", err: true},
  1954. }
  1955. funcs := []string{
  1956. "sin",
  1957. "cos",
  1958. "tan",
  1959. "asin",
  1960. "acos",
  1961. "atan",
  1962. "exp",
  1963. "log",
  1964. "exp2",
  1965. "log2",
  1966. "sqrt",
  1967. "inversesqrt",
  1968. "floor",
  1969. "ceil",
  1970. "fract",
  1971. "length",
  1972. "normalize",
  1973. "dfdx",
  1974. "dfdy",
  1975. "fwidth",
  1976. }
  1977. for _, c := range cases {
  1978. for _, f := range funcs {
  1979. stmt := strings.ReplaceAll(c.stmt, "{{.Func}}", f)
  1980. src := fmt.Sprintf(`package main
  1981. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  1982. %s
  1983. return dstPos
  1984. }`, stmt)
  1985. _, err := compileToIR([]byte(src))
  1986. if err == nil && c.err {
  1987. t.Errorf("%s must return an error but does not", stmt)
  1988. } else if err != nil && !c.err {
  1989. t.Errorf("%s must not return nil but returned %v", stmt, err)
  1990. }
  1991. }
  1992. }
  1993. }
  1994. func TestSyntaxBuiltinFuncSingleArgTypeInteger(t *testing.T) {
  1995. cases := []struct {
  1996. stmt string
  1997. err bool
  1998. }{
  1999. {stmt: "a := {{.Func}}(); _ = a", err: true},
  2000. {stmt: "a := {{.Func}}(false); _ = a", err: true},
  2001. {stmt: "a := {{.Func}}(1); _ = a", err: false},
  2002. {stmt: "a := {{.Func}}(1.0); _ = a", err: false},
  2003. {stmt: "a := {{.Func}}(int(1)); _ = a", err: false},
  2004. {stmt: "a := {{.Func}}(vec2(1)); _ = a", err: false},
  2005. {stmt: "a := {{.Func}}(vec3(1)); _ = a", err: false},
  2006. {stmt: "a := {{.Func}}(vec4(1)); _ = a", err: false},
  2007. {stmt: "a := {{.Func}}(ivec2(1)); _ = a", err: false},
  2008. {stmt: "a := {{.Func}}(ivec3(1)); _ = a", err: false},
  2009. {stmt: "a := {{.Func}}(ivec4(1)); _ = a", err: false},
  2010. {stmt: "a := {{.Func}}(mat2(1)); _ = a", err: true},
  2011. {stmt: "a := {{.Func}}(mat3(1)); _ = a", err: true},
  2012. {stmt: "a := {{.Func}}(mat4(1)); _ = a", err: true},
  2013. {stmt: "a := {{.Func}}(1, 1); _ = a", err: true},
  2014. }
  2015. funcs := []string{
  2016. "abs",
  2017. "sign",
  2018. }
  2019. for _, c := range cases {
  2020. for _, f := range funcs {
  2021. stmt := strings.ReplaceAll(c.stmt, "{{.Func}}", f)
  2022. src := fmt.Sprintf(`package main
  2023. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  2024. %s
  2025. return dstPos
  2026. }`, stmt)
  2027. _, err := compileToIR([]byte(src))
  2028. if err == nil && c.err {
  2029. t.Errorf("%s must return an error but does not", stmt)
  2030. } else if err != nil && !c.err {
  2031. t.Errorf("%s must not return nil but returned %v", stmt, err)
  2032. }
  2033. }
  2034. }
  2035. }
  2036. // Issue #2184
  2037. func TestSyntaxBuiltinFuncDoubleArgsType(t *testing.T) {
  2038. cases := []struct {
  2039. stmt string
  2040. err bool
  2041. }{
  2042. {stmt: "a := {{.Func}}(); _ = a", err: true},
  2043. {stmt: "a := {{.Func}}(1); _ = a", err: true},
  2044. {stmt: "a := {{.Func}}(false, false); _ = a", err: true},
  2045. {stmt: "a := {{.Func}}(1, 1); _ = a", err: false},
  2046. {stmt: "a := {{.Func}}(1.0, 1); _ = a", err: false},
  2047. {stmt: "a := {{.Func}}(1, 1.0); _ = a", err: false},
  2048. {stmt: "a := {{.Func}}(int(1), 1); _ = a", err: true},
  2049. {stmt: "a := {{.Func}}(int(1), 1.0); _ = a", err: true},
  2050. {stmt: "a := {{.Func}}(int(1), 1.1); _ = a", err: true},
  2051. {stmt: "a := {{.Func}}(float(1), 1); _ = a", err: false},
  2052. {stmt: "a := {{.Func}}(float(1), 1.0); _ = a", err: false},
  2053. {stmt: "a := {{.Func}}(float(1), 1.1); _ = a", err: false},
  2054. {stmt: "a := {{.Func}}(1, int(1)); _ = a", err: true},
  2055. {stmt: "a := {{.Func}}(1.0, int(1)); _ = a", err: true},
  2056. {stmt: "a := {{.Func}}(1.1, int(1)); _ = a", err: true},
  2057. {stmt: "a := {{.Func}}(1, float(1)); _ = a", err: false},
  2058. {stmt: "a := {{.Func}}(1.0, float(1)); _ = a", err: false},
  2059. {stmt: "a := {{.Func}}(1.1, float(1)); _ = a", err: false},
  2060. {stmt: "a := {{.Func}}(int(1), int(1)); _ = a", err: true},
  2061. {stmt: "a := {{.Func}}(1, vec2(1)); _ = a", err: true},
  2062. {stmt: "a := {{.Func}}(1, vec3(1)); _ = a", err: true},
  2063. {stmt: "a := {{.Func}}(1, vec4(1)); _ = a", err: true},
  2064. {stmt: "a := {{.Func}}(vec2(1), 1); _ = a", err: true},
  2065. {stmt: "a := {{.Func}}(vec2(1), vec2(1)); _ = a", err: false},
  2066. {stmt: "a := {{.Func}}(vec2(1), vec3(1)); _ = a", err: true},
  2067. {stmt: "a := {{.Func}}(vec2(1), vec4(1)); _ = a", err: true},
  2068. {stmt: "a := {{.Func}}(vec3(1), 1); _ = a", err: true},
  2069. {stmt: "a := {{.Func}}(vec3(1), vec2(1)); _ = a", err: true},
  2070. {stmt: "a := {{.Func}}(vec3(1), vec3(1)); _ = a", err: false},
  2071. {stmt: "a := {{.Func}}(vec3(1), vec4(1)); _ = a", err: true},
  2072. {stmt: "a := {{.Func}}(vec4(1), 1); _ = a", err: true},
  2073. {stmt: "a := {{.Func}}(vec4(1), vec2(1)); _ = a", err: true},
  2074. {stmt: "a := {{.Func}}(vec4(1), vec3(1)); _ = a", err: true},
  2075. {stmt: "a := {{.Func}}(vec4(1), vec4(1)); _ = a", err: false},
  2076. {stmt: "a := {{.Func}}(mat2(1), mat2(1)); _ = a", err: true},
  2077. {stmt: "a := {{.Func}}(ivec2(1), 1); _ = a", err: true},
  2078. {stmt: "a := {{.Func}}(ivec2(1), ivec2(1)); _ = a", err: true},
  2079. {stmt: "a := {{.Func}}(1, 1, 1); _ = a", err: true},
  2080. }
  2081. funcs := []string{
  2082. "atan2",
  2083. "pow",
  2084. "distance",
  2085. "dot",
  2086. "reflect",
  2087. }
  2088. for _, c := range cases {
  2089. for _, f := range funcs {
  2090. stmt := strings.ReplaceAll(c.stmt, "{{.Func}}", f)
  2091. src := fmt.Sprintf(`package main
  2092. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  2093. %s
  2094. return dstPos
  2095. }`, stmt)
  2096. _, err := compileToIR([]byte(src))
  2097. if err == nil && c.err {
  2098. t.Errorf("%s must return an error but does not", stmt)
  2099. } else if err != nil && !c.err {
  2100. t.Errorf("%s must not return nil but returned %v", stmt, err)
  2101. }
  2102. }
  2103. }
  2104. }
  2105. // Issue #2184
  2106. func TestSyntaxBuiltinFuncDoubleArgsType2(t *testing.T) {
  2107. cases := []struct {
  2108. stmt string
  2109. err bool
  2110. }{
  2111. {stmt: "a := {{.Func}}(); _ = a", err: true},
  2112. {stmt: "a := {{.Func}}(1); _ = a", err: true},
  2113. {stmt: "a := {{.Func}}(false, false); _ = a", err: true},
  2114. {stmt: "a := {{.Func}}(1, 1); _ = a", err: false},
  2115. {stmt: "a := {{.Func}}(1.0, 1); _ = a", err: false},
  2116. {stmt: "a := {{.Func}}(1, 1.0); _ = a", err: false},
  2117. {stmt: "a := {{.Func}}(int(1), 1); _ = a", err: true},
  2118. {stmt: "a := {{.Func}}(int(1), 1.0); _ = a", err: true},
  2119. {stmt: "a := {{.Func}}(int(1), 1.1); _ = a", err: true},
  2120. {stmt: "a := {{.Func}}(float(1), 1); _ = a", err: false},
  2121. {stmt: "a := {{.Func}}(float(1), 1.0); _ = a", err: false},
  2122. {stmt: "a := {{.Func}}(float(1), 1.1); _ = a", err: false},
  2123. {stmt: "a := {{.Func}}(1, int(1)); _ = a", err: true},
  2124. {stmt: "a := {{.Func}}(1.0, int(1)); _ = a", err: true},
  2125. {stmt: "a := {{.Func}}(1.1, int(1)); _ = a", err: true},
  2126. {stmt: "a := {{.Func}}(1, float(1)); _ = a", err: false},
  2127. {stmt: "a := {{.Func}}(1.0, float(1)); _ = a", err: false},
  2128. {stmt: "a := {{.Func}}(1.1, float(1)); _ = a", err: false},
  2129. {stmt: "a := {{.Func}}(int(1), int(1)); _ = a", err: true},
  2130. {stmt: "a := {{.Func}}(1, vec2(1)); _ = a", err: true},
  2131. {stmt: "a := {{.Func}}(1, vec3(1)); _ = a", err: true},
  2132. {stmt: "a := {{.Func}}(1, vec4(1)); _ = a", err: true},
  2133. {stmt: "a := {{.Func}}(1, ivec2(1)); _ = a", err: true},
  2134. {stmt: "a := {{.Func}}(1, ivec3(1)); _ = a", err: true},
  2135. {stmt: "a := {{.Func}}(1, ivec4(1)); _ = a", err: true},
  2136. {stmt: "a := {{.Func}}(vec2(1), 1); _ = a", err: false}, // The second argument can be a scalar.
  2137. {stmt: "a := {{.Func}}(vec2(1), vec2(1)); _ = a", err: false},
  2138. {stmt: "a := {{.Func}}(vec2(1), vec3(1)); _ = a", err: true},
  2139. {stmt: "a := {{.Func}}(vec2(1), vec4(1)); _ = a", err: true},
  2140. {stmt: "a := {{.Func}}(vec3(1), 1); _ = a", err: false}, // The second argument can be a scalar.
  2141. {stmt: "a := {{.Func}}(vec3(1), vec2(1)); _ = a", err: true},
  2142. {stmt: "a := {{.Func}}(vec3(1), vec3(1)); _ = a", err: false},
  2143. {stmt: "a := {{.Func}}(vec3(1), vec4(1)); _ = a", err: true},
  2144. {stmt: "a := {{.Func}}(vec4(1), 1); _ = a", err: false}, // The second argument can be a scalar.
  2145. {stmt: "a := {{.Func}}(vec4(1), vec2(1)); _ = a", err: true},
  2146. {stmt: "a := {{.Func}}(vec4(1), vec3(1)); _ = a", err: true},
  2147. {stmt: "a := {{.Func}}(vec4(1), vec4(1)); _ = a", err: false},
  2148. {stmt: "a := {{.Func}}(mat2(1), mat2(1)); _ = a", err: true},
  2149. {stmt: "a := {{.Func}}(ivec2(1), 1); _ = a", err: true},
  2150. {stmt: "a := {{.Func}}(ivec2(1), ivec2(1)); _ = a", err: true},
  2151. {stmt: "a := {{.Func}}(1, 1, 1); _ = a", err: true},
  2152. }
  2153. funcs := []string{
  2154. "mod",
  2155. }
  2156. for _, c := range cases {
  2157. for _, f := range funcs {
  2158. stmt := strings.ReplaceAll(c.stmt, "{{.Func}}", f)
  2159. src := fmt.Sprintf(`package main
  2160. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  2161. %s
  2162. return dstPos
  2163. }`, stmt)
  2164. _, err := compileToIR([]byte(src))
  2165. if err == nil && c.err {
  2166. t.Errorf("%s must return an error but does not", stmt)
  2167. } else if err != nil && !c.err {
  2168. t.Errorf("%s must not return nil but returned %v", stmt, err)
  2169. }
  2170. }
  2171. }
  2172. }
  2173. func TestSyntaxBuiltinFuncArgsMinMax(t *testing.T) {
  2174. cases := []struct {
  2175. stmt string
  2176. err bool
  2177. }{
  2178. {stmt: "a := {{.Func}}(); _ = a", err: true},
  2179. {stmt: "a := {{.Func}}(1); _ = a", err: true}, // TODO: Allow this (#2677).
  2180. {stmt: "a := {{.Func}}(false, false); _ = a", err: true},
  2181. {stmt: "a := {{.Func}}(1, 1); var _ int = a", err: false},
  2182. {stmt: "a := {{.Func}}(1.0, 1); var _ float = a", err: false},
  2183. {stmt: "a := {{.Func}}(1, 1.0); var _ float = a", err: false},
  2184. {stmt: "a := {{.Func}}(1.1, 1); _ = a", err: false},
  2185. {stmt: "a := {{.Func}}(1, 1.1); _ = a", err: false},
  2186. {stmt: "a := {{.Func}}(int(1), 1); var _ int = a", err: false},
  2187. {stmt: "a := {{.Func}}(int(1), 1.0); var _ int = a", err: false},
  2188. {stmt: "a := {{.Func}}(int(1), 1.1); _ = a", err: true},
  2189. {stmt: "a := {{.Func}}(float(1), 1); _ = a", err: false},
  2190. {stmt: "a := {{.Func}}(float(1), 1.0); _ = a", err: false},
  2191. {stmt: "a := {{.Func}}(float(1), 1.1); _ = a", err: false},
  2192. {stmt: "a := {{.Func}}(1, int(1)); var _ int = a", err: false},
  2193. {stmt: "a := {{.Func}}(1.0, int(1)); var _ int = a", err: false},
  2194. {stmt: "a := {{.Func}}(1.1, int(1)); _ = a", err: true},
  2195. {stmt: "a := {{.Func}}(1, float(1)); _ = a", err: false},
  2196. {stmt: "a := {{.Func}}(1.0, float(1)); _ = a", err: false},
  2197. {stmt: "a := {{.Func}}(1.1, float(1)); _ = a", err: false},
  2198. {stmt: "a := {{.Func}}(int(1), int(1)); var _ int = a", err: false},
  2199. {stmt: "a := {{.Func}}(int(1), float(1)); _ = a", err: true},
  2200. {stmt: "a := {{.Func}}(float(1), int(1)); _ = a", err: true},
  2201. {stmt: "x := 1.1; a := {{.Func}}(int(x), 1); _ = a", err: false},
  2202. {stmt: "x := 1; a := {{.Func}}(float(x), 1.1); _ = a", err: false},
  2203. {stmt: "x := 1.1; a := {{.Func}}(1, int(x)); _ = a", err: false},
  2204. {stmt: "x := 1; a := {{.Func}}(1.1, float(x)); _ = a", err: false},
  2205. {stmt: "a := {{.Func}}(1, vec2(1)); _ = a", err: true},
  2206. {stmt: "a := {{.Func}}(1, vec3(1)); _ = a", err: true},
  2207. {stmt: "a := {{.Func}}(1, vec4(1)); _ = a", err: true},
  2208. {stmt: "a := {{.Func}}(1, ivec2(1)); _ = a", err: true},
  2209. {stmt: "a := {{.Func}}(1, ivec3(1)); _ = a", err: true},
  2210. {stmt: "a := {{.Func}}(1, ivec4(1)); _ = a", err: true},
  2211. {stmt: "a := {{.Func}}(vec2(1), 1); _ = a", err: false}, // The second argument can be a scalar.
  2212. {stmt: "a := {{.Func}}(vec2(1), 1.0); _ = a", err: false},
  2213. {stmt: "a := {{.Func}}(vec2(1), 1.1); _ = a", err: false},
  2214. {stmt: "a := {{.Func}}(vec2(1), vec2(1)); _ = a", err: false},
  2215. {stmt: "a := {{.Func}}(vec2(1), vec3(1)); _ = a", err: true},
  2216. {stmt: "a := {{.Func}}(vec2(1), vec4(1)); _ = a", err: true},
  2217. {stmt: "a := {{.Func}}(vec3(1), 1); _ = a", err: false}, // The second argument can be a scalar.
  2218. {stmt: "a := {{.Func}}(vec3(1), 1.0); _ = a", err: false},
  2219. {stmt: "a := {{.Func}}(vec3(1), 1.1); _ = a", err: false},
  2220. {stmt: "a := {{.Func}}(vec3(1), vec2(1)); _ = a", err: true},
  2221. {stmt: "a := {{.Func}}(vec3(1), vec3(1)); _ = a", err: false},
  2222. {stmt: "a := {{.Func}}(vec3(1), vec4(1)); _ = a", err: true},
  2223. {stmt: "a := {{.Func}}(vec4(1), 1); _ = a", err: false}, // The second argument can be a scalar.
  2224. {stmt: "a := {{.Func}}(vec4(1), 1.0); _ = a", err: false},
  2225. {stmt: "a := {{.Func}}(vec4(1), 1.1); _ = a", err: false},
  2226. {stmt: "a := {{.Func}}(vec4(1), vec2(1)); _ = a", err: true},
  2227. {stmt: "a := {{.Func}}(vec4(1), vec3(1)); _ = a", err: true},
  2228. {stmt: "a := {{.Func}}(vec4(1), vec4(1)); _ = a", err: false},
  2229. {stmt: "a := {{.Func}}(mat2(1), mat2(1)); _ = a", err: true},
  2230. {stmt: "a := {{.Func}}(ivec2(1), 1); _ = a", err: false}, // The second argument can be a scalar.
  2231. {stmt: "a := {{.Func}}(ivec2(1), 1.0); _ = a", err: false},
  2232. {stmt: "a := {{.Func}}(ivec2(1), 1.1); _ = a", err: true},
  2233. {stmt: "a := {{.Func}}(ivec2(1), ivec2(1)); _ = a", err: false},
  2234. {stmt: "a := {{.Func}}(ivec2(1), ivec3(1)); _ = a", err: true},
  2235. {stmt: "a := {{.Func}}(ivec2(1), ivec4(1)); _ = a", err: true},
  2236. {stmt: "a := {{.Func}}(ivec3(1), 1); _ = a", err: false}, // The second argument can be a scalar.
  2237. {stmt: "a := {{.Func}}(ivec3(1), 1.0); _ = a", err: false},
  2238. {stmt: "a := {{.Func}}(ivec3(1), 1.1); _ = a", err: true},
  2239. {stmt: "a := {{.Func}}(ivec3(1), ivec2(1)); _ = a", err: true},
  2240. {stmt: "a := {{.Func}}(ivec3(1), ivec3(1)); _ = a", err: false},
  2241. {stmt: "a := {{.Func}}(ivec3(1), ivec4(1)); _ = a", err: true},
  2242. {stmt: "a := {{.Func}}(ivec4(1), 1); _ = a", err: false}, // The second argument can be a scalar.
  2243. {stmt: "a := {{.Func}}(ivec4(1), 1.0); _ = a", err: false},
  2244. {stmt: "a := {{.Func}}(ivec4(1), 1.1); _ = a", err: true},
  2245. {stmt: "a := {{.Func}}(ivec4(1), ivec2(1)); _ = a", err: true},
  2246. {stmt: "a := {{.Func}}(ivec4(1), ivec3(1)); _ = a", err: true},
  2247. {stmt: "a := {{.Func}}(ivec4(1), ivec4(1)); _ = a", err: false},
  2248. {stmt: "a := {{.Func}}(1, 1, 1); _ = a", err: true}, // TODO: Allow this (#2677).
  2249. }
  2250. funcs := []string{
  2251. "min",
  2252. "max",
  2253. }
  2254. for _, c := range cases {
  2255. for _, f := range funcs {
  2256. stmt := strings.ReplaceAll(c.stmt, "{{.Func}}", f)
  2257. src := fmt.Sprintf(`package main
  2258. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  2259. %s
  2260. return dstPos
  2261. }`, stmt)
  2262. _, err := compileToIR([]byte(src))
  2263. if err == nil && c.err {
  2264. t.Errorf("%s must return an error but does not", stmt)
  2265. } else if err != nil && !c.err {
  2266. t.Errorf("%s must not return nil but returned %v", stmt, err)
  2267. }
  2268. }
  2269. }
  2270. }
  2271. // Issue #2184
  2272. func TestSyntaxBuiltinFuncStepType(t *testing.T) {
  2273. cases := []struct {
  2274. stmt string
  2275. err bool
  2276. }{
  2277. {stmt: "a := step(); _ = a", err: true},
  2278. {stmt: "a := step(1); _ = a", err: true},
  2279. {stmt: "a := step(false, false); _ = a", err: true},
  2280. {stmt: "a := step(1, 1); _ = a", err: false},
  2281. {stmt: "a := step(1.0, 1); _ = a", err: false},
  2282. {stmt: "a := step(1, 1.0); _ = a", err: false},
  2283. {stmt: "a := step(int(1), int(1)); _ = a", err: true},
  2284. {stmt: "a := step(1, vec2(1)); _ = a", err: false}, // The first argument can be a scalar.
  2285. {stmt: "a := step(1, vec3(1)); _ = a", err: false}, // The first argument can be a scalar.
  2286. {stmt: "a := step(1, vec4(1)); _ = a", err: false}, // The first argument can be a scalar.
  2287. {stmt: "a := step(1, ivec2(1)); _ = a", err: true},
  2288. {stmt: "a := step(1, ivec3(1)); _ = a", err: true},
  2289. {stmt: "a := step(1, ivec4(1)); _ = a", err: true},
  2290. {stmt: "a := step(vec2(1), 1); _ = a", err: true},
  2291. {stmt: "a := step(vec2(1), vec2(1)); _ = a", err: false},
  2292. {stmt: "a := step(vec2(1), vec3(1)); _ = a", err: true},
  2293. {stmt: "a := step(vec2(1), vec4(1)); _ = a", err: true},
  2294. {stmt: "a := step(vec3(1), 1); _ = a", err: true},
  2295. {stmt: "a := step(vec3(1), vec2(1)); _ = a", err: true},
  2296. {stmt: "a := step(vec3(1), vec3(1)); _ = a", err: false},
  2297. {stmt: "a := step(vec3(1), vec4(1)); _ = a", err: true},
  2298. {stmt: "a := step(vec4(1), 1); _ = a", err: true},
  2299. {stmt: "a := step(vec4(1), vec2(1)); _ = a", err: true},
  2300. {stmt: "a := step(vec4(1), vec3(1)); _ = a", err: true},
  2301. {stmt: "a := step(vec4(1), vec4(1)); _ = a", err: false},
  2302. {stmt: "a := step(mat2(1), mat2(1)); _ = a", err: true},
  2303. {stmt: "a := step(ivec2(1), ivec2(1)); _ = a", err: true},
  2304. {stmt: "a := step(1, 1, 1); _ = a", err: true},
  2305. }
  2306. for _, c := range cases {
  2307. stmt := c.stmt
  2308. src := fmt.Sprintf(`package main
  2309. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  2310. %s
  2311. return dstPos
  2312. }`, stmt)
  2313. _, err := compileToIR([]byte(src))
  2314. if err == nil && c.err {
  2315. t.Errorf("%s must return an error but does not", stmt)
  2316. } else if err != nil && !c.err {
  2317. t.Errorf("%s must not return nil but returned %v", stmt, err)
  2318. }
  2319. }
  2320. }
  2321. // Issue #2184
  2322. func TestSyntaxBuiltinFuncTripleArgsType(t *testing.T) {
  2323. cases := []struct {
  2324. stmt string
  2325. err bool
  2326. }{
  2327. {stmt: "a := {{.Func}}(); _ = a", err: true},
  2328. {stmt: "a := {{.Func}}(1); _ = a", err: true},
  2329. {stmt: "a := {{.Func}}(false, false); _ = a", err: true},
  2330. {stmt: "a := {{.Func}}(1, 1); _ = a", err: true},
  2331. {stmt: "a := {{.Func}}(false, false, false); _ = a", err: true},
  2332. {stmt: "a := {{.Func}}(1, 1, 1); _ = a", err: false},
  2333. {stmt: "a := {{.Func}}(1.0, 1, 1); _ = a", err: false},
  2334. {stmt: "a := {{.Func}}(1, 1.0, 1); _ = a", err: false},
  2335. {stmt: "a := {{.Func}}(1, 1, 1.0); _ = a", err: false},
  2336. {stmt: "a := {{.Func}}(1, vec2(1), 1); _ = a", err: true},
  2337. {stmt: "a := {{.Func}}(1, 1, vec2(1)); _ = a", err: true},
  2338. {stmt: "a := {{.Func}}(1, vec2(1), vec2(1)); _ = a", err: true},
  2339. {stmt: "a := {{.Func}}(vec2(1), 1, 1); _ = a", err: true},
  2340. {stmt: "a := {{.Func}}(vec2(1), 1, vec2(1)); _ = a", err: true},
  2341. {stmt: "a := {{.Func}}(vec2(1), vec2(1), 1); _ = a", err: true},
  2342. {stmt: "a := {{.Func}}(vec2(1), vec2(1), vec2(1)); _ = a", err: false},
  2343. {stmt: "a := {{.Func}}(vec2(1), vec2(1), vec3(1)); _ = a", err: true},
  2344. {stmt: "a := {{.Func}}(vec3(1), 1, 1); _ = a", err: true},
  2345. {stmt: "a := {{.Func}}(vec3(1), 1, vec3(1)); _ = a", err: true},
  2346. {stmt: "a := {{.Func}}(vec3(1), vec3(1), 1); _ = a", err: true},
  2347. {stmt: "a := {{.Func}}(vec3(1), vec3(1), vec3(1)); _ = a", err: false},
  2348. {stmt: "a := {{.Func}}(vec4(1), 1, 1); _ = a", err: true},
  2349. {stmt: "a := {{.Func}}(vec4(1), 1, vec4(1)); _ = a", err: true},
  2350. {stmt: "a := {{.Func}}(vec4(1), vec4(1), 1); _ = a", err: true},
  2351. {stmt: "a := {{.Func}}(vec4(1), vec4(1), vec4(1)); _ = a", err: false},
  2352. {stmt: "a := {{.Func}}(ivec2(1), ivec2(1), ivec2(1)); _ = a", err: true},
  2353. }
  2354. funcs := []string{
  2355. "faceforward",
  2356. }
  2357. for _, c := range cases {
  2358. for _, f := range funcs {
  2359. stmt := strings.ReplaceAll(c.stmt, "{{.Func}}", f)
  2360. src := fmt.Sprintf(`package main
  2361. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  2362. %s
  2363. return dstPos
  2364. }`, stmt)
  2365. _, err := compileToIR([]byte(src))
  2366. if err == nil && c.err {
  2367. t.Errorf("%s must return an error but does not", stmt)
  2368. } else if err != nil && !c.err {
  2369. t.Errorf("%s must not return nil but returned %v", stmt, err)
  2370. }
  2371. }
  2372. }
  2373. }
  2374. // Issue #2184
  2375. func TestSyntaxBuiltinFuncClampType(t *testing.T) {
  2376. cases := []struct {
  2377. stmt string
  2378. err bool
  2379. }{
  2380. {stmt: "a := clamp(); _ = a", err: true},
  2381. {stmt: "a := clamp(1); _ = a", err: true},
  2382. {stmt: "a := clamp(false, false); _ = a", err: true},
  2383. {stmt: "a := clamp(1, 1); _ = a", err: true},
  2384. {stmt: "a := clamp(false, false, false); _ = a", err: true},
  2385. {stmt: "a := clamp(1, 1, 1); var _ int = a", err: false},
  2386. {stmt: "a := clamp(int(1), 1, 1); var _ int = a", err: false},
  2387. {stmt: "a := clamp(int(1), 1.0, 1); var _ int = a", err: false},
  2388. {stmt: "a := clamp(int(1), 1, 1.0); var _ int = a", err: false},
  2389. {stmt: "a := clamp(int(1), 1.1, 1); _ = a", err: true},
  2390. {stmt: "a := clamp(int(1), 1, 1.1); _ = a", err: true},
  2391. {stmt: "a := clamp(float(1), 1, 1); var _ float = a", err: false},
  2392. {stmt: "a := clamp(float(1), 1.0, 1); var _ float = a", err: false},
  2393. {stmt: "a := clamp(float(1), 1, 1.0); var _ float = a", err: false},
  2394. {stmt: "a := clamp(float(1), 1.1, 1); _ = a", err: false},
  2395. {stmt: "a := clamp(float(1), 1, 1.1); _ = a", err: false},
  2396. {stmt: "x := 1.1; a := clamp(int(x), 1, 1); _ = a", err: false},
  2397. {stmt: "x := 1; a := clamp(float(x), 1.1, 1.1); _ = a", err: false},
  2398. {stmt: "x := 1.1; a := clamp(1, int(x), 1); _ = a", err: false},
  2399. {stmt: "x := 1; a := clamp(1.1, float(x), 1.1); _ = a", err: false},
  2400. {stmt: "x := 1.1; a := clamp(1, 1, int(x)); _ = a", err: false},
  2401. {stmt: "x := 1; a := clamp(1.1, 1.1, float(x)); _ = a", err: false},
  2402. {stmt: "a := clamp(1.0, 1, 1); var _ float = a", err: false},
  2403. {stmt: "a := clamp(1, 1.0, 1); var _ float = a", err: false},
  2404. {stmt: "a := clamp(1, 1, 1.0); var _ float = a", err: false},
  2405. {stmt: "a := clamp(1.1, 1, 1); var _ float = a", err: false},
  2406. {stmt: "a := clamp(1, 1.1, 1); var _ float = a", err: false},
  2407. {stmt: "a := clamp(1, 1, 1.1); var _ float = a", err: false},
  2408. {stmt: "a := clamp(1, vec2(1), 1); _ = a", err: true},
  2409. {stmt: "a := clamp(1, 1, vec2(1)); _ = a", err: true},
  2410. {stmt: "a := clamp(1, vec2(1), vec2(1)); _ = a", err: true},
  2411. {stmt: "a := clamp(vec2(1), 1, 1); _ = a", err: false},
  2412. {stmt: "a := clamp(vec2(1), 1, vec2(1)); _ = a", err: true},
  2413. {stmt: "a := clamp(vec2(1), vec2(1), 1); _ = a", err: true},
  2414. {stmt: "a := clamp(vec2(1), vec2(1), vec2(1)); _ = a", err: false},
  2415. {stmt: "a := clamp(vec2(1), vec2(1), vec3(1)); _ = a", err: true},
  2416. {stmt: "a := clamp(vec3(1), 1, 1); _ = a", err: false},
  2417. {stmt: "a := clamp(vec3(1), 1, vec3(1)); _ = a", err: true},
  2418. {stmt: "a := clamp(vec3(1), vec3(1), 1); _ = a", err: true},
  2419. {stmt: "a := clamp(vec3(1), vec3(1), vec3(1)); _ = a", err: false},
  2420. {stmt: "a := clamp(vec4(1), 1, 1); _ = a", err: false},
  2421. {stmt: "a := clamp(vec4(1), 1, vec4(1)); _ = a", err: true},
  2422. {stmt: "a := clamp(vec4(1), vec4(1), 1); _ = a", err: true},
  2423. {stmt: "a := clamp(vec4(1), vec4(1), vec4(1)); _ = a", err: false},
  2424. {stmt: "a := clamp(ivec2(1), 1, 1); _ = a", err: false},
  2425. {stmt: "a := clamp(ivec2(1), 1.1, 1); _ = a", err: true},
  2426. {stmt: "a := clamp(ivec2(1), 1, 1.1); _ = a", err: true},
  2427. {stmt: "a := clamp(ivec2(1), 1, ivec2(1)); _ = a", err: true},
  2428. {stmt: "a := clamp(ivec2(1), ivec2(1), 1); _ = a", err: true},
  2429. {stmt: "a := clamp(ivec2(1), ivec2(1), ivec2(1)); _ = a", err: false},
  2430. {stmt: "a := clamp(ivec2(1), ivec2(1), ivec3(1)); _ = a", err: true},
  2431. {stmt: "a := clamp(ivec3(1), 1, 1); _ = a", err: false},
  2432. {stmt: "a := clamp(ivec3(1), 1.1, 1); _ = a", err: true},
  2433. {stmt: "a := clamp(ivec3(1), 1, 1.1); _ = a", err: true},
  2434. {stmt: "a := clamp(ivec3(1), 1, ivec3(1)); _ = a", err: true},
  2435. {stmt: "a := clamp(ivec3(1), ivec3(1), 1); _ = a", err: true},
  2436. {stmt: "a := clamp(ivec3(1), ivec3(1), ivec3(1)); _ = a", err: false},
  2437. {stmt: "a := clamp(ivec4(1), 1, 1); _ = a", err: false},
  2438. {stmt: "a := clamp(ivec4(1), 1.1, 1); _ = a", err: true},
  2439. {stmt: "a := clamp(ivec4(1), 1, 1.1); _ = a", err: true},
  2440. {stmt: "a := clamp(ivec4(1), 1, ivec4(1)); _ = a", err: true},
  2441. {stmt: "a := clamp(ivec4(1), ivec4(1), 1); _ = a", err: true},
  2442. {stmt: "a := clamp(ivec4(1), ivec4(1), ivec4(1)); _ = a", err: false},
  2443. {stmt: "a := clamp(1, 1, 1, 1); _ = a", err: true},
  2444. }
  2445. for _, c := range cases {
  2446. stmt := c.stmt
  2447. src := fmt.Sprintf(`package main
  2448. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  2449. %s
  2450. return dstPos
  2451. }`, stmt)
  2452. _, err := compileToIR([]byte(src))
  2453. if err == nil && c.err {
  2454. t.Errorf("%s must return an error but does not", stmt)
  2455. } else if err != nil && !c.err {
  2456. t.Errorf("%s must not return nil but returned %v", stmt, err)
  2457. }
  2458. }
  2459. }
  2460. // Issue #2184
  2461. func TestSyntaxBuiltinFuncMixType(t *testing.T) {
  2462. cases := []struct {
  2463. stmt string
  2464. err bool
  2465. }{
  2466. {stmt: "a := mix(); _ = a", err: true},
  2467. {stmt: "a := mix(1); _ = a", err: true},
  2468. {stmt: "a := mix(false, false); _ = a", err: true},
  2469. {stmt: "a := mix(1, 1); _ = a", err: true},
  2470. {stmt: "a := mix(false, false, false); _ = a", err: true},
  2471. {stmt: "a := mix(1, 1, 1); _ = a", err: false},
  2472. {stmt: "a := mix(1.0, 1, 1); _ = a", err: false},
  2473. {stmt: "a := mix(1, 1.0, 1); _ = a", err: false},
  2474. {stmt: "a := mix(1, 1, 1.0); _ = a", err: false},
  2475. {stmt: "a := mix(1, vec2(1), 1); _ = a", err: true},
  2476. {stmt: "a := mix(1, 1, vec2(1)); _ = a", err: true},
  2477. {stmt: "a := mix(1, vec2(1), vec2(1)); _ = a", err: true},
  2478. {stmt: "a := mix(vec2(1), 1, 1); _ = a", err: true},
  2479. {stmt: "a := mix(vec2(1), 1, vec2(1)); _ = a", err: true},
  2480. {stmt: "a := mix(vec2(1), vec2(1), 1); _ = a", err: false}, // The thrid argument can be a float.
  2481. {stmt: "a := mix(vec2(1), vec2(1), vec2(1)); _ = a", err: false},
  2482. {stmt: "a := mix(vec2(1), vec2(1), vec3(1)); _ = a", err: true},
  2483. {stmt: "a := mix(vec3(1), 1, 1); _ = a", err: true},
  2484. {stmt: "a := mix(vec3(1), 1, vec3(1)); _ = a", err: true},
  2485. {stmt: "a := mix(vec3(1), vec3(1), 1); _ = a", err: false}, // The thrid argument can be a float.
  2486. {stmt: "a := mix(vec3(1), vec3(1), vec3(1)); _ = a", err: false},
  2487. {stmt: "a := mix(vec4(1), 1, 1); _ = a", err: true},
  2488. {stmt: "a := mix(vec4(1), 1, vec4(1)); _ = a", err: true},
  2489. {stmt: "a := mix(vec4(1), vec4(1), 1); _ = a", err: false}, // The thrid argument can be a float.
  2490. {stmt: "a := mix(vec4(1), vec4(1), vec4(1)); _ = a", err: false},
  2491. {stmt: "a := mix(ivec2(1), ivec2(1), 1); _ = a", err: true},
  2492. {stmt: "a := mix(ivec2(1), ivec2(1), ivec2(1)); _ = a", err: true},
  2493. {stmt: "a := mix(1, 1, 1, 1); _ = a", err: true},
  2494. }
  2495. for _, c := range cases {
  2496. stmt := c.stmt
  2497. src := fmt.Sprintf(`package main
  2498. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  2499. %s
  2500. return dstPos
  2501. }`, stmt)
  2502. _, err := compileToIR([]byte(src))
  2503. if err == nil && c.err {
  2504. t.Errorf("%s must return an error but does not", stmt)
  2505. } else if err != nil && !c.err {
  2506. t.Errorf("%s must not return nil but returned %v", stmt, err)
  2507. }
  2508. }
  2509. }
  2510. // Issue #2184
  2511. func TestSyntaxBuiltinFuncSmoothstepType(t *testing.T) {
  2512. cases := []struct {
  2513. stmt string
  2514. err bool
  2515. }{
  2516. {stmt: "a := smoothstep(); _ = a", err: true},
  2517. {stmt: "a := smoothstep(1); _ = a", err: true},
  2518. {stmt: "a := smoothstep(false, false); _ = a", err: true},
  2519. {stmt: "a := smoothstep(1, 1); _ = a", err: true},
  2520. {stmt: "a := smoothstep(false, false, false); _ = a", err: true},
  2521. {stmt: "a := smoothstep(1, 1, 1); _ = a", err: false},
  2522. {stmt: "a := smoothstep(1.0, 1, 1); _ = a", err: false},
  2523. {stmt: "a := smoothstep(1, 1.0, 1); _ = a", err: false},
  2524. {stmt: "a := smoothstep(1, 1, 1.0); _ = a", err: false},
  2525. {stmt: "a := smoothstep(1, vec2(1), 1); _ = a", err: true},
  2526. {stmt: "a := smoothstep(1, 1, vec2(1)); _ = a", err: false},
  2527. {stmt: "a := smoothstep(1, 1, vec3(1)); _ = a", err: false},
  2528. {stmt: "a := smoothstep(1, 1, vec4(1)); _ = a", err: false},
  2529. {stmt: "a := smoothstep(1, vec2(1), vec2(1)); _ = a", err: true},
  2530. {stmt: "a := smoothstep(vec2(1), 1, 1); _ = a", err: true},
  2531. {stmt: "a := smoothstep(vec2(1), 1, vec2(1)); _ = a", err: true},
  2532. {stmt: "a := smoothstep(vec2(1), vec2(1), 1); _ = a", err: true},
  2533. {stmt: "a := smoothstep(vec2(1), vec2(1), vec2(1)); _ = a", err: false},
  2534. {stmt: "a := smoothstep(vec2(1), vec2(1), vec3(1)); _ = a", err: true},
  2535. {stmt: "a := smoothstep(vec3(1), 1, 1); _ = a", err: true},
  2536. {stmt: "a := smoothstep(vec3(1), 1, vec3(1)); _ = a", err: true},
  2537. {stmt: "a := smoothstep(vec3(1), vec3(1), 1); _ = a", err: true},
  2538. {stmt: "a := smoothstep(vec3(1), vec3(1), vec3(1)); _ = a", err: false},
  2539. {stmt: "a := smoothstep(vec4(1), 1, 1); _ = a", err: true},
  2540. {stmt: "a := smoothstep(vec4(1), 1, vec4(1)); _ = a", err: true},
  2541. {stmt: "a := smoothstep(vec4(1), vec4(1), 1); _ = a", err: true},
  2542. {stmt: "a := smoothstep(vec4(1), vec4(1), vec4(1)); _ = a", err: false},
  2543. {stmt: "a := smoothstep(ivec2(1), 1, 1); _ = a", err: true},
  2544. {stmt: "a := smoothstep(1, ivec2(1), 1); _ = a", err: true},
  2545. {stmt: "a := smoothstep(1, 1, ivec2(1)); _ = a", err: true},
  2546. {stmt: "a := smoothstep(1, 1, 1, 1); _ = a", err: true},
  2547. }
  2548. for _, c := range cases {
  2549. stmt := c.stmt
  2550. src := fmt.Sprintf(`package main
  2551. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  2552. %s
  2553. return dstPos
  2554. }`, stmt)
  2555. _, err := compileToIR([]byte(src))
  2556. if err == nil && c.err {
  2557. t.Errorf("%s must return an error but does not", stmt)
  2558. } else if err != nil && !c.err {
  2559. t.Errorf("%s must not return nil but returned %v", stmt, err)
  2560. }
  2561. }
  2562. }
  2563. // Issue #2184
  2564. func TestSyntaxBuiltinFuncRefractType(t *testing.T) {
  2565. cases := []struct {
  2566. stmt string
  2567. err bool
  2568. }{
  2569. {stmt: "a := refract(); _ = a", err: true},
  2570. {stmt: "a := refract(1); _ = a", err: true},
  2571. {stmt: "a := refract(false, false); _ = a", err: true},
  2572. {stmt: "a := refract(1, 1); _ = a", err: true},
  2573. {stmt: "a := refract(false, false, false); _ = a", err: true},
  2574. {stmt: "a := refract(1, 1, 1); _ = a", err: false},
  2575. {stmt: "a := refract(1.0, 1, 1); _ = a", err: false},
  2576. {stmt: "a := refract(1, 1.0, 1); _ = a", err: false},
  2577. {stmt: "a := refract(1, 1, 1.0); _ = a", err: false},
  2578. {stmt: "a := refract(1, vec2(1), 1); _ = a", err: true},
  2579. {stmt: "a := refract(1, 1, vec2(1)); _ = a", err: true},
  2580. {stmt: "a := refract(1, vec2(1), vec2(1)); _ = a", err: true},
  2581. {stmt: "a := refract(vec2(1), 1, 1); _ = a", err: true},
  2582. {stmt: "a := refract(vec2(1), 1, vec2(1)); _ = a", err: true},
  2583. {stmt: "a := refract(vec2(1), vec2(1), 1); _ = a", err: false}, // The third argument must be a float.
  2584. {stmt: "a := refract(vec2(1), vec2(1), vec2(1)); _ = a", err: true},
  2585. {stmt: "a := refract(vec2(1), vec2(1), vec3(1)); _ = a", err: true},
  2586. {stmt: "a := refract(vec3(1), 1, 1); _ = a", err: true},
  2587. {stmt: "a := refract(vec3(1), 1, vec3(1)); _ = a", err: true},
  2588. {stmt: "a := refract(vec3(1), vec3(1), 1); _ = a", err: false}, // The third argument must be a float.
  2589. {stmt: "a := refract(vec3(1), vec3(1), vec3(1)); _ = a", err: true},
  2590. {stmt: "a := refract(vec4(1), 1, 1); _ = a", err: true},
  2591. {stmt: "a := refract(vec4(1), 1, vec4(1)); _ = a", err: true},
  2592. {stmt: "a := refract(vec4(1), vec4(1), 1); _ = a", err: false}, // The third argument must be a float.
  2593. {stmt: "a := refract(vec4(1), vec4(1), vec4(1)); _ = a", err: true},
  2594. {stmt: "a := refract(ivec2(1), ivec2(1), 1); _ = a", err: true},
  2595. {stmt: "a := refract(1, 1, 1, 1); _ = a", err: true},
  2596. }
  2597. for _, c := range cases {
  2598. stmt := c.stmt
  2599. src := fmt.Sprintf(`package main
  2600. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  2601. %s
  2602. return dstPos
  2603. }`, stmt)
  2604. _, err := compileToIR([]byte(src))
  2605. if err == nil && c.err {
  2606. t.Errorf("%s must return an error but does not", stmt)
  2607. } else if err != nil && !c.err {
  2608. t.Errorf("%s must not return nil but returned %v", stmt, err)
  2609. }
  2610. }
  2611. }
  2612. // Issue #2184
  2613. func TestSyntaxBuiltinFuncCrossType(t *testing.T) {
  2614. cases := []struct {
  2615. stmt string
  2616. err bool
  2617. }{
  2618. {stmt: "a := cross(); _ = a", err: true},
  2619. {stmt: "a := cross(1); _ = a", err: true},
  2620. {stmt: "a := cross(false, false); _ = a", err: true},
  2621. {stmt: "a := cross(1, 1); _ = a", err: true},
  2622. {stmt: "a := cross(1.0, 1); _ = a", err: true},
  2623. {stmt: "a := cross(1, 1.0); _ = a", err: true},
  2624. {stmt: "a := cross(int(1), int(1)); _ = a", err: true},
  2625. {stmt: "a := cross(1, vec2(1)); _ = a", err: true},
  2626. {stmt: "a := cross(1, vec3(1)); _ = a", err: true},
  2627. {stmt: "a := cross(1, vec4(1)); _ = a", err: true},
  2628. {stmt: "a := cross(vec2(1), 1); _ = a", err: true},
  2629. {stmt: "a := cross(vec2(1), vec2(1)); _ = a", err: true},
  2630. {stmt: "a := cross(vec2(1), vec3(1)); _ = a", err: true},
  2631. {stmt: "a := cross(vec2(1), vec4(1)); _ = a", err: true},
  2632. {stmt: "a := cross(vec3(1), 1); _ = a", err: true},
  2633. {stmt: "a := cross(vec3(1), vec2(1)); _ = a", err: true},
  2634. {stmt: "a := cross(vec3(1), vec3(1)); _ = a", err: false}, // Only two vec3s are allowed
  2635. {stmt: "a := cross(vec3(1), vec4(1)); _ = a", err: true},
  2636. {stmt: "a := cross(vec4(1), 1); _ = a", err: true},
  2637. {stmt: "a := cross(vec4(1), vec2(1)); _ = a", err: true},
  2638. {stmt: "a := cross(vec4(1), vec3(1)); _ = a", err: true},
  2639. {stmt: "a := cross(vec4(1), vec4(1)); _ = a", err: true},
  2640. {stmt: "a := cross(mat2(1), mat2(1)); _ = a", err: true},
  2641. {stmt: "a := cross(ivec3(1), ivec3(1)); _ = a", err: true},
  2642. {stmt: "a := cross(1, 1, 1); _ = a", err: true},
  2643. }
  2644. for _, c := range cases {
  2645. stmt := c.stmt
  2646. src := fmt.Sprintf(`package main
  2647. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  2648. %s
  2649. return dstPos
  2650. }`, stmt)
  2651. _, err := compileToIR([]byte(src))
  2652. if err == nil && c.err {
  2653. t.Errorf("%s must return an error but does not", stmt)
  2654. } else if err != nil && !c.err {
  2655. t.Errorf("%s must not return nil but returned %v", stmt, err)
  2656. }
  2657. }
  2658. }
  2659. // Issue #2184
  2660. func TestSyntaxBuiltinFuncTransposeType(t *testing.T) {
  2661. cases := []struct {
  2662. stmt string
  2663. err bool
  2664. }{
  2665. {stmt: "a := transpose(); _ = a", err: true},
  2666. {stmt: "a := transpose(false); _ = a", err: true},
  2667. {stmt: "a := transpose(1); _ = a", err: true},
  2668. {stmt: "a := transpose(1.0); _ = a", err: true},
  2669. {stmt: "a := transpose(int(1)); _ = a", err: true},
  2670. {stmt: "a := transpose(vec2(1)); _ = a", err: true},
  2671. {stmt: "a := transpose(vec3(1)); _ = a", err: true},
  2672. {stmt: "a := transpose(vec4(1)); _ = a", err: true},
  2673. {stmt: "a := transpose(ivec2(1)); _ = a", err: true},
  2674. {stmt: "a := transpose(ivec3(1)); _ = a", err: true},
  2675. {stmt: "a := transpose(ivec4(1)); _ = a", err: true},
  2676. {stmt: "a := transpose(mat2(1)); _ = a", err: false},
  2677. {stmt: "a := transpose(mat3(1)); _ = a", err: false},
  2678. {stmt: "a := transpose(mat4(1)); _ = a", err: false},
  2679. {stmt: "a := transpose(1, 1); _ = a", err: true},
  2680. }
  2681. for _, c := range cases {
  2682. stmt := c.stmt
  2683. src := fmt.Sprintf(`package main
  2684. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  2685. %s
  2686. return dstPos
  2687. }`, stmt)
  2688. _, err := compileToIR([]byte(src))
  2689. if err == nil && c.err {
  2690. t.Errorf("%s must return an error but does not", stmt)
  2691. } else if err != nil && !c.err {
  2692. t.Errorf("%s must not return nil but returned %v", stmt, err)
  2693. }
  2694. }
  2695. }
  2696. // Issue #2187
  2697. func TestSyntaxEqual(t *testing.T) {
  2698. cases := []struct {
  2699. stmt string
  2700. err bool
  2701. }{
  2702. {stmt: "_ = false == true", err: false},
  2703. {stmt: "_ = false != true", err: false},
  2704. {stmt: "_ = false == 1", err: true},
  2705. {stmt: "_ = false != 1", err: true},
  2706. {stmt: "_ = false == 1.0", err: true},
  2707. {stmt: "_ = false != 1.0", err: true},
  2708. {stmt: "_ = false == 1.1", err: true},
  2709. {stmt: "_ = false != 1.1", err: true},
  2710. {stmt: "a, b := false, true; _ = a == b", err: false},
  2711. {stmt: "a, b := false, true; _ = a != b", err: false},
  2712. {stmt: "a, b := false, 1; _ = a == b", err: true},
  2713. {stmt: "a, b := false, 1; _ = a != b", err: true},
  2714. {stmt: "a, b := false, 1.0; _ = a == b", err: true},
  2715. {stmt: "a, b := false, 1.0; _ = a != b", err: true},
  2716. {stmt: "a, b := false, 1.1; _ = a == b", err: true},
  2717. {stmt: "a, b := false, 1.1; _ = a != b", err: true},
  2718. {stmt: "a, b := false, vec2(1); _ = a == b", err: true},
  2719. {stmt: "a, b := false, vec2(1); _ = a != b", err: true},
  2720. {stmt: "a, b := false, ivec2(1); _ = a == b", err: true},
  2721. {stmt: "a, b := false, ivec2(1); _ = a != b", err: true},
  2722. {stmt: "a, b := false, mat2(1); _ = a == b", err: true},
  2723. {stmt: "a, b := false, mat2(1); _ = a != b", err: true},
  2724. {stmt: "_ = 1 == true", err: true},
  2725. {stmt: "_ = 1 != true", err: true},
  2726. {stmt: "_ = 1 == 1", err: false},
  2727. {stmt: "_ = 1 != 1", err: false},
  2728. {stmt: "_ = 1 == 1.0", err: false},
  2729. {stmt: "_ = 1 != 1.0", err: false},
  2730. {stmt: "_ = 1 == 1.1", err: false},
  2731. {stmt: "_ = 1 != 1.1", err: false},
  2732. {stmt: "a, b := 1, true; _ = a == b", err: true},
  2733. {stmt: "a, b := 1, true; _ = a != b", err: true},
  2734. {stmt: "a, b := 1, 1; _ = a == b", err: false},
  2735. {stmt: "a, b := 1, 1; _ = a != b", err: false},
  2736. {stmt: "a, b := 1, 1.0; _ = a == b", err: true},
  2737. {stmt: "a, b := 1, 1.0; _ = a != b", err: true},
  2738. {stmt: "a, b := 1, 1.1; _ = a == b", err: true},
  2739. {stmt: "a, b := 1, 1.1; _ = a != b", err: true},
  2740. {stmt: "a, b := 1, vec2(1); _ = a == b", err: true},
  2741. {stmt: "a, b := 1, vec2(1); _ = a != b", err: true},
  2742. {stmt: "a, b := 1, ivec2(1); _ = a == b", err: true},
  2743. {stmt: "a, b := 1, ivec2(1); _ = a != b", err: true},
  2744. {stmt: "a, b := 1, mat2(1); _ = a == b", err: true},
  2745. {stmt: "a, b := 1, mat2(1); _ = a != b", err: true},
  2746. {stmt: "_ = 1.0 == true", err: true},
  2747. {stmt: "_ = 1.0 != true", err: true},
  2748. {stmt: "_ = 1.0 == 1", err: false},
  2749. {stmt: "_ = 1.0 != 1", err: false},
  2750. {stmt: "_ = 1.0 == 1.0", err: false},
  2751. {stmt: "_ = 1.0 != 1.0", err: false},
  2752. {stmt: "_ = 1.0 == 1.1", err: false},
  2753. {stmt: "_ = 1.0 != 1.1", err: false},
  2754. {stmt: "a, b := 1.0, true; _ = a == b", err: true},
  2755. {stmt: "a, b := 1.0, true; _ = a != b", err: true},
  2756. {stmt: "a, b := 1.0, 1; _ = a == b", err: true},
  2757. {stmt: "a, b := 1.0, 1; _ = a != b", err: true},
  2758. {stmt: "a, b := 1.0, 1.0; _ = a == b", err: false},
  2759. {stmt: "a, b := 1.0, 1.0; _ = a != b", err: false},
  2760. {stmt: "a, b := 1.0, 1.1; _ = a == b", err: false},
  2761. {stmt: "a, b := 1.0, 1.1; _ = a != b", err: false},
  2762. {stmt: "a, b := 1.0, vec2(1); _ = a == b", err: true},
  2763. {stmt: "a, b := 1.0, vec2(1); _ = a != b", err: true},
  2764. {stmt: "a, b := 1.0, ivec2(1); _ = a == b", err: true},
  2765. {stmt: "a, b := 1.0, ivec2(1); _ = a != b", err: true},
  2766. {stmt: "a, b := 1.0, mat2(1); _ = a == b", err: true},
  2767. {stmt: "a, b := 1.0, mat2(1); _ = a != b", err: true},
  2768. {stmt: "_ = 1.1 == true", err: true},
  2769. {stmt: "_ = 1.1 != true", err: true},
  2770. {stmt: "_ = 1.1 == 1", err: false},
  2771. {stmt: "_ = 1.1 != 1", err: false},
  2772. {stmt: "_ = 1.1 == 1.0", err: false},
  2773. {stmt: "_ = 1.1 != 1.0", err: false},
  2774. {stmt: "_ = 1.1 == 1.1", err: false},
  2775. {stmt: "_ = 1.1 != 1.1", err: false},
  2776. {stmt: "a, b := 1.1, true; _ = a == b", err: true},
  2777. {stmt: "a, b := 1.1, true; _ = a != b", err: true},
  2778. {stmt: "a, b := 1.1, 1; _ = a == b", err: true},
  2779. {stmt: "a, b := 1.1, 1; _ = a != b", err: true},
  2780. {stmt: "a, b := 1.1, 1.0; _ = a == b", err: false},
  2781. {stmt: "a, b := 1.1, 1.0; _ = a != b", err: false},
  2782. {stmt: "a, b := 1.1, 1.1; _ = a == b", err: false},
  2783. {stmt: "a, b := 1.1, 1.1; _ = a != b", err: false},
  2784. {stmt: "a, b := 1.1, vec2(1); _ = a == b", err: true},
  2785. {stmt: "a, b := 1.1, vec2(1); _ = a != b", err: true},
  2786. {stmt: "a, b := 1.1, ivec2(1); _ = a == b", err: true},
  2787. {stmt: "a, b := 1.1, ivec2(1); _ = a != b", err: true},
  2788. {stmt: "a, b := 1.1, mat2(1); _ = a == b", err: true},
  2789. {stmt: "a, b := 1.1, mat2(1); _ = a != b", err: true},
  2790. {stmt: "_ = vec2(1) == true", err: true},
  2791. {stmt: "_ = vec2(1) != true", err: true},
  2792. {stmt: "_ = vec2(1) == 1", err: true},
  2793. {stmt: "_ = vec2(1) != 1", err: true},
  2794. {stmt: "_ = vec2(1) == 1.0", err: true},
  2795. {stmt: "_ = vec2(1) != 1.0", err: true},
  2796. {stmt: "_ = vec2(1) == 1.1", err: true},
  2797. {stmt: "_ = vec2(1) != 1.1", err: true},
  2798. {stmt: "a, b := vec2(1), true; _ = a == b", err: true},
  2799. {stmt: "a, b := vec2(1), true; _ = a != b", err: true},
  2800. {stmt: "a, b := vec2(1), 1; _ = a == b", err: true},
  2801. {stmt: "a, b := vec2(1), 1; _ = a != b", err: true},
  2802. {stmt: "a, b := vec2(1), 1.0; _ = a == b", err: true},
  2803. {stmt: "a, b := vec2(1), 1.0; _ = a != b", err: true},
  2804. {stmt: "a, b := vec2(1), 1.1; _ = a == b", err: true},
  2805. {stmt: "a, b := vec2(1), 1.1; _ = a != b", err: true},
  2806. {stmt: "a, b := vec2(1), vec2(1); _ = a == b", err: false},
  2807. {stmt: "a, b := vec2(1), vec2(1); _ = a != b", err: false},
  2808. {stmt: "a, b := vec2(1), ivec2(1); _ = a == b", err: true},
  2809. {stmt: "a, b := vec2(1), ivec2(1); _ = a != b", err: true},
  2810. {stmt: "a, b := vec2(1), mat2(1); _ = a == b", err: true},
  2811. {stmt: "a, b := vec2(1), mat2(1); _ = a != b", err: true},
  2812. {stmt: "_ = ivec2(1) == true", err: true},
  2813. {stmt: "_ = ivec2(1) != true", err: true},
  2814. {stmt: "_ = ivec2(1) == 1", err: true},
  2815. {stmt: "_ = ivec2(1) != 1", err: true},
  2816. {stmt: "_ = ivec2(1) == 1.0", err: true},
  2817. {stmt: "_ = ivec2(1) != 1.0", err: true},
  2818. {stmt: "_ = ivec2(1) == 1.1", err: true},
  2819. {stmt: "_ = ivec2(1) != 1.1", err: true},
  2820. {stmt: "a, b := ivec2(1), true; _ = a == b", err: true},
  2821. {stmt: "a, b := ivec2(1), true; _ = a != b", err: true},
  2822. {stmt: "a, b := ivec2(1), 1; _ = a == b", err: true},
  2823. {stmt: "a, b := ivec2(1), 1; _ = a != b", err: true},
  2824. {stmt: "a, b := ivec2(1), 1.0; _ = a == b", err: true},
  2825. {stmt: "a, b := ivec2(1), 1.0; _ = a != b", err: true},
  2826. {stmt: "a, b := ivec2(1), 1.1; _ = a == b", err: true},
  2827. {stmt: "a, b := ivec2(1), 1.1; _ = a != b", err: true},
  2828. {stmt: "a, b := ivec2(1), vec2(1); _ = a == b", err: true},
  2829. {stmt: "a, b := ivec2(1), vec2(1); _ = a != b", err: true},
  2830. {stmt: "a, b := ivec2(1), ivec2(1); _ = a == b", err: false},
  2831. {stmt: "a, b := ivec2(1), ivec2(1); _ = a != b", err: false},
  2832. {stmt: "a, b := ivec2(1), mat2(1); _ = a == b", err: true},
  2833. {stmt: "a, b := ivec2(1), mat2(1); _ = a != b", err: true},
  2834. {stmt: "_ = mat2(1) == true", err: true},
  2835. {stmt: "_ = mat2(1) != true", err: true},
  2836. {stmt: "_ = mat2(1) == 1", err: true},
  2837. {stmt: "_ = mat2(1) != 1", err: true},
  2838. {stmt: "_ = mat2(1) == 1.0", err: true},
  2839. {stmt: "_ = mat2(1) != 1.0", err: true},
  2840. {stmt: "_ = mat2(1) == 1.1", err: true},
  2841. {stmt: "_ = mat2(1) != 1.1", err: true},
  2842. {stmt: "a, b := mat2(1), true; _ = a == b", err: true},
  2843. {stmt: "a, b := mat2(1), true; _ = a != b", err: true},
  2844. {stmt: "a, b := mat2(1), 1; _ = a == b", err: true},
  2845. {stmt: "a, b := mat2(1), 1; _ = a != b", err: true},
  2846. {stmt: "a, b := mat2(1), 1.0; _ = a == b", err: true},
  2847. {stmt: "a, b := mat2(1), 1.0; _ = a != b", err: true},
  2848. {stmt: "a, b := mat2(1), 1.1; _ = a == b", err: true},
  2849. {stmt: "a, b := mat2(1), 1.1; _ = a != b", err: true},
  2850. {stmt: "a, b := mat2(1), vec2(1); _ = a == b", err: true},
  2851. {stmt: "a, b := mat2(1), vec2(1); _ = a != b", err: true},
  2852. {stmt: "a, b := mat2(1), ivec2(1); _ = a == b", err: true},
  2853. {stmt: "a, b := mat2(1), ivec2(1); _ = a != b", err: true},
  2854. {stmt: "a, b := mat2(1), mat2(1); _ = a == b", err: true}, // Comparing matrices are not allowed.
  2855. {stmt: "a, b := mat2(1), mat2(1); _ = a != b", err: true}, // Comparing matrices are not allowed.
  2856. {stmt: "_ = false && true", err: false},
  2857. {stmt: "_ = false || true", err: false},
  2858. {stmt: "_ = false && 1", err: true},
  2859. {stmt: "_ = false || 1", err: true},
  2860. {stmt: "_ = false && 1.0", err: true},
  2861. {stmt: "_ = false || 1.0", err: true},
  2862. {stmt: "_ = false && 1.1", err: true},
  2863. {stmt: "_ = false || 1.1", err: true},
  2864. {stmt: "a, b := false, true; _ = a && b", err: false},
  2865. {stmt: "a, b := false, true; _ = a || b", err: false},
  2866. {stmt: "a, b := false, 1; _ = a && b", err: true},
  2867. {stmt: "a, b := false, 1; _ = a || b", err: true},
  2868. {stmt: "a, b := false, 1.0; _ = a && b", err: true},
  2869. {stmt: "a, b := false, 1.0; _ = a || b", err: true},
  2870. {stmt: "a, b := false, 1.1; _ = a && b", err: true},
  2871. {stmt: "a, b := false, 1.1; _ = a || b", err: true},
  2872. {stmt: "a, b := false, vec2(1); _ = a && b", err: true},
  2873. {stmt: "a, b := false, vec2(1); _ = a || b", err: true},
  2874. {stmt: "a, b := false, ivec2(1); _ = a && b", err: true},
  2875. {stmt: "a, b := false, ivec2(1); _ = a || b", err: true},
  2876. {stmt: "a, b := false, mat2(1); _ = a && b", err: true},
  2877. {stmt: "a, b := false, mat2(1); _ = a || b", err: true},
  2878. {stmt: "_ = 1.0 && true", err: true},
  2879. {stmt: "_ = 1.0 || true", err: true},
  2880. {stmt: "_ = 1.0 && 1", err: true},
  2881. {stmt: "_ = 1.0 || 1", err: true},
  2882. {stmt: "_ = 1.0 && 1.0", err: true},
  2883. {stmt: "_ = 1.0 || 1.0", err: true},
  2884. {stmt: "_ = 1.0 && 1.1", err: true},
  2885. {stmt: "_ = 1.0 || 1.1", err: true},
  2886. {stmt: "a, b := 1.0, true; _ = a && b", err: true},
  2887. {stmt: "a, b := 1.0, true; _ = a || b", err: true},
  2888. {stmt: "a, b := 1.0, 1; _ = a && b", err: true},
  2889. {stmt: "a, b := 1.0, 1; _ = a || b", err: true},
  2890. {stmt: "a, b := 1.0, 1.0; _ = a && b", err: true},
  2891. {stmt: "a, b := 1.0, 1.0; _ = a || b", err: true},
  2892. {stmt: "a, b := 1.0, 1.1; _ = a && b", err: true},
  2893. {stmt: "a, b := 1.0, 1.1; _ = a || b", err: true},
  2894. {stmt: "a, b := 1.0, vec2(1); _ = a && b", err: true},
  2895. {stmt: "a, b := 1.0, vec2(1); _ = a || b", err: true},
  2896. {stmt: "a, b := 1.0, ivec2(1); _ = a && b", err: true},
  2897. {stmt: "a, b := 1.0, ivec2(1); _ = a || b", err: true},
  2898. {stmt: "a, b := 1.0, mat2(1); _ = a && b", err: true},
  2899. {stmt: "a, b := 1.0, mat2(1); _ = a || b", err: true},
  2900. }
  2901. for _, c := range cases {
  2902. stmt := c.stmt
  2903. src := fmt.Sprintf(`package main
  2904. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  2905. %s
  2906. return dstPos
  2907. }`, stmt)
  2908. _, err := compileToIR([]byte(src))
  2909. if err == nil && c.err {
  2910. t.Errorf("%s must return an error but does not", stmt)
  2911. } else if err != nil && !c.err {
  2912. t.Errorf("%s must not return nil but returned %v", stmt, err)
  2913. }
  2914. }
  2915. }
  2916. func TestSyntaxTypeRedeclaration(t *testing.T) {
  2917. cases := []struct {
  2918. stmt string
  2919. err bool
  2920. }{
  2921. {stmt: "type Foo int; type Foo int", err: true},
  2922. {stmt: "type Foo int; type Foo float", err: true},
  2923. {stmt: "type Foo int; { type Foo int }", err: false},
  2924. {stmt: "type Foo int; type Bar int", err: false},
  2925. }
  2926. for _, c := range cases {
  2927. stmt := c.stmt
  2928. src := fmt.Sprintf(`package main
  2929. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  2930. %s
  2931. return dstPos
  2932. }`, stmt)
  2933. _, err := compileToIR([]byte(src))
  2934. if err == nil && c.err {
  2935. t.Errorf("%s must return an error but does not", stmt)
  2936. } else if err != nil && !c.err {
  2937. t.Errorf("%s must not return nil but returned %v", stmt, err)
  2938. }
  2939. }
  2940. }
  2941. func TestSyntaxSwizzling(t *testing.T) {
  2942. cases := []struct {
  2943. stmt string
  2944. err bool
  2945. }{
  2946. {stmt: "var a vec2; var b float = a.x; _ = b", err: false},
  2947. {stmt: "var a vec2; var b float = a.y; _ = b", err: false},
  2948. {stmt: "var a vec2; var b float = a.z; _ = b", err: true},
  2949. {stmt: "var a vec2; var b float = a.w; _ = b", err: true},
  2950. {stmt: "var a vec2; var b vec2 = a.xy; _ = b", err: false},
  2951. {stmt: "var a vec2; var b vec3 = a.xyz; _ = b", err: true},
  2952. {stmt: "var a vec2; var b vec3 = a.xyw; _ = b", err: true},
  2953. {stmt: "var a vec2; var b vec3 = a.xyy; _ = b", err: false},
  2954. {stmt: "var a vec2; var b vec3 = a.zzz; _ = b", err: true},
  2955. {stmt: "var a vec2; var b vec4 = a.xyzw; _ = b", err: true},
  2956. {stmt: "var a vec2; var b float = a.r; _ = b", err: false},
  2957. {stmt: "var a vec2; var b float = a.g; _ = b", err: false},
  2958. {stmt: "var a vec2; var b float = a.b; _ = b", err: true},
  2959. {stmt: "var a vec2; var b float = a.a; _ = b", err: true},
  2960. {stmt: "var a vec2; var b vec2 = a.rg; _ = b", err: false},
  2961. {stmt: "var a vec2; var b vec3 = a.rgb; _ = b", err: true},
  2962. {stmt: "var a vec2; var b vec3 = a.rga; _ = b", err: true},
  2963. {stmt: "var a vec2; var b vec3 = a.rgg; _ = b", err: false},
  2964. {stmt: "var a vec2; var b vec3 = a.bbb; _ = b", err: true},
  2965. {stmt: "var a vec2; var b vec4 = a.rgba; _ = b", err: true},
  2966. {stmt: "var a vec2; var b float = a.s; _ = b", err: false},
  2967. {stmt: "var a vec2; var b float = a.t; _ = b", err: false},
  2968. {stmt: "var a vec2; var b float = a.p; _ = b", err: true},
  2969. {stmt: "var a vec2; var b float = a.q; _ = b", err: true},
  2970. {stmt: "var a vec2; var b vec2 = a.st; _ = b", err: false},
  2971. {stmt: "var a vec2; var b vec3 = a.stp; _ = b", err: true},
  2972. {stmt: "var a vec2; var b vec3 = a.stq; _ = b", err: true},
  2973. {stmt: "var a vec2; var b vec3 = a.stt; _ = b", err: false},
  2974. {stmt: "var a vec2; var b vec3 = a.ppp; _ = b", err: true},
  2975. {stmt: "var a vec2; var b vec4 = a.stpq; _ = b", err: true},
  2976. {stmt: "var a vec3; var b float = a.x; _ = b", err: false},
  2977. {stmt: "var a vec3; var b float = a.y; _ = b", err: false},
  2978. {stmt: "var a vec3; var b float = a.z; _ = b", err: false},
  2979. {stmt: "var a vec3; var b float = a.w; _ = b", err: true},
  2980. {stmt: "var a vec3; var b vec2 = a.xy; _ = b", err: false},
  2981. {stmt: "var a vec3; var b vec3 = a.xyz; _ = b", err: false},
  2982. {stmt: "var a vec3; var b vec3 = a.xyw; _ = b", err: true},
  2983. {stmt: "var a vec3; var b vec3 = a.xyy; _ = b", err: false},
  2984. {stmt: "var a vec3; var b vec3 = a.zzz; _ = b", err: false},
  2985. {stmt: "var a vec3; var b vec4 = a.xyzw; _ = b", err: true},
  2986. {stmt: "var a vec4; var b float = a.x; _ = b", err: false},
  2987. {stmt: "var a vec4; var b float = a.y; _ = b", err: false},
  2988. {stmt: "var a vec4; var b float = a.z; _ = b", err: false},
  2989. {stmt: "var a vec4; var b float = a.w; _ = b", err: false},
  2990. {stmt: "var a vec4; var b vec2 = a.xy; _ = b", err: false},
  2991. {stmt: "var a vec4; var b vec3 = a.xyz; _ = b", err: false},
  2992. {stmt: "var a vec4; var b vec3 = a.xyw; _ = b", err: false},
  2993. {stmt: "var a vec4; var b vec3 = a.xyy; _ = b", err: false},
  2994. {stmt: "var a vec4; var b vec3 = a.zzz; _ = b", err: false},
  2995. {stmt: "var a vec4; var b vec4 = a.xyzw; _ = b", err: false},
  2996. {stmt: "var a ivec2; var b int = a.x; _ = b", err: false},
  2997. {stmt: "var a ivec2; var b int = a.y; _ = b", err: false},
  2998. {stmt: "var a ivec2; var b int = a.z; _ = b", err: true},
  2999. {stmt: "var a ivec2; var b int = a.w; _ = b", err: true},
  3000. {stmt: "var a ivec2; var b ivec2 = a.xy; _ = b", err: false},
  3001. {stmt: "var a ivec2; var b ivec3 = a.xyz; _ = b", err: true},
  3002. {stmt: "var a ivec2; var b ivec3 = a.xyw; _ = b", err: true},
  3003. {stmt: "var a ivec2; var b ivec3 = a.xyy; _ = b", err: false},
  3004. {stmt: "var a ivec2; var b ivec3 = a.zzz; _ = b", err: true},
  3005. {stmt: "var a ivec2; var b ivec4 = a.xyzw; _ = b", err: true},
  3006. {stmt: "var a ivec3; var b int = a.x; _ = b", err: false},
  3007. {stmt: "var a ivec3; var b int = a.y; _ = b", err: false},
  3008. {stmt: "var a ivec3; var b int = a.z; _ = b", err: false},
  3009. {stmt: "var a ivec3; var b int = a.w; _ = b", err: true},
  3010. {stmt: "var a ivec3; var b ivec2 = a.xy; _ = b", err: false},
  3011. {stmt: "var a ivec3; var b ivec3 = a.xyz; _ = b", err: false},
  3012. {stmt: "var a ivec3; var b ivec3 = a.xyw; _ = b", err: true},
  3013. {stmt: "var a ivec3; var b ivec3 = a.xyy; _ = b", err: false},
  3014. {stmt: "var a ivec3; var b ivec3 = a.zzz; _ = b", err: false},
  3015. {stmt: "var a ivec3; var b ivec4 = a.xyzw; _ = b", err: true},
  3016. {stmt: "var a ivec4; var b int = a.x; _ = b", err: false},
  3017. {stmt: "var a ivec4; var b int = a.y; _ = b", err: false},
  3018. {stmt: "var a ivec4; var b int = a.z; _ = b", err: false},
  3019. {stmt: "var a ivec4; var b int = a.w; _ = b", err: false},
  3020. {stmt: "var a ivec4; var b ivec2 = a.xy; _ = b", err: false},
  3021. {stmt: "var a ivec4; var b ivec3 = a.xyz; _ = b", err: false},
  3022. {stmt: "var a ivec4; var b ivec3 = a.xyw; _ = b", err: false},
  3023. {stmt: "var a ivec4; var b ivec3 = a.xyy; _ = b", err: false},
  3024. {stmt: "var a ivec4; var b ivec3 = a.zzz; _ = b", err: false},
  3025. {stmt: "var a ivec4; var b ivec4 = a.xyzw; _ = b", err: false},
  3026. }
  3027. for _, c := range cases {
  3028. stmt := c.stmt
  3029. src := fmt.Sprintf(`package main
  3030. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  3031. %s
  3032. return dstPos
  3033. }`, stmt)
  3034. _, err := compileToIR([]byte(src))
  3035. if err == nil && c.err {
  3036. t.Errorf("%s must return an error but does not", stmt)
  3037. } else if err != nil && !c.err {
  3038. t.Errorf("%s must not return nil but returned %v", stmt, err)
  3039. }
  3040. }
  3041. }
  3042. func TestSyntaxConstType(t *testing.T) {
  3043. cases := []struct {
  3044. stmt string
  3045. err bool
  3046. }{
  3047. {stmt: "const a = false", err: false},
  3048. {stmt: "const a bool = false", err: false},
  3049. {stmt: "const a int = false", err: true},
  3050. {stmt: "const a float = false", err: true},
  3051. {stmt: "const a vec2 = false", err: true},
  3052. {stmt: "const a ivec2 = false", err: true},
  3053. {stmt: "const a = bool(false)", err: false},
  3054. {stmt: "const a bool = bool(false)", err: false},
  3055. {stmt: "const a int = bool(false)", err: true},
  3056. {stmt: "const a float = bool(false)", err: true},
  3057. {stmt: "const a vec2 = bool(false)", err: true},
  3058. {stmt: "const a ivec2 = bool(false)", err: true},
  3059. {stmt: "const a = int(false)", err: true},
  3060. {stmt: "const a bool = int(false)", err: true},
  3061. {stmt: "const a int = int(false)", err: true},
  3062. {stmt: "const a float = int(false)", err: true},
  3063. {stmt: "const a vec2 = int(false)", err: true},
  3064. {stmt: "const a ivec2 = int(false)", err: true},
  3065. {stmt: "const a = float(false)", err: true},
  3066. {stmt: "const a bool = float(false)", err: true},
  3067. {stmt: "const a int = float(false)", err: true},
  3068. {stmt: "const a float = float(false)", err: true},
  3069. {stmt: "const a vec2 = float(false)", err: true},
  3070. {stmt: "const a ivec2 = float(false)", err: true},
  3071. {stmt: "const a = 1", err: false},
  3072. {stmt: "const a bool = 1", err: true},
  3073. {stmt: "const a int = 1", err: false},
  3074. {stmt: "const a float = 1", err: false},
  3075. {stmt: "const a vec2 = 1", err: true},
  3076. {stmt: "const a ivec2 = 1", err: true},
  3077. {stmt: "const a = int(1)", err: false},
  3078. {stmt: "const a bool = int(1)", err: true},
  3079. {stmt: "const a int = int(1)", err: false},
  3080. {stmt: "const a float = int(1)", err: true},
  3081. {stmt: "const a vec2 = int(1)", err: true},
  3082. {stmt: "const a ivec2 = int(1)", err: true},
  3083. {stmt: "const a = float(1)", err: false},
  3084. {stmt: "const a bool = float(1)", err: true},
  3085. {stmt: "const a int = float(1)", err: true},
  3086. {stmt: "const a float = float(1)", err: false},
  3087. {stmt: "const a vec2 = float(1)", err: true},
  3088. {stmt: "const a ivec2 = float(1)", err: true},
  3089. {stmt: "const a = 1.0", err: false},
  3090. {stmt: "const a bool = 1.0", err: true},
  3091. {stmt: "const a int = 1.0", err: false},
  3092. {stmt: "const a float = 1.0", err: false},
  3093. {stmt: "const a vec2 = 1.0", err: true},
  3094. {stmt: "const a ivec2 = 1.0", err: true},
  3095. {stmt: "const a = int(1.0)", err: false},
  3096. {stmt: "const a bool = int(1.0)", err: true},
  3097. {stmt: "const a int = int(1.0)", err: false},
  3098. {stmt: "const a float = int(1.0)", err: true},
  3099. {stmt: "const a vec2 = int(1.0)", err: true},
  3100. {stmt: "const a ivec2 = int(1.0)", err: true},
  3101. {stmt: "const a = float(1.0)", err: false},
  3102. {stmt: "const a bool = float(1.0)", err: true},
  3103. {stmt: "const a int = float(1.0)", err: true},
  3104. {stmt: "const a float = float(1.0)", err: false},
  3105. {stmt: "const a vec2 = float(1.0)", err: true},
  3106. {stmt: "const a ivec2 = float(1.0)", err: true},
  3107. {stmt: "const a = 1.1", err: false},
  3108. {stmt: "const a bool = 1.1", err: true},
  3109. {stmt: "const a int = 1.1", err: true},
  3110. {stmt: "const a float = 1.1", err: false},
  3111. {stmt: "const a vec2 = 1.1", err: true},
  3112. {stmt: "const a ivec2 = 1.1", err: true},
  3113. {stmt: "const a = int(1.1)", err: true},
  3114. {stmt: "const a bool = int(1.1)", err: true},
  3115. {stmt: "const a int = int(1.1)", err: true},
  3116. {stmt: "const a float = int(1.1)", err: true},
  3117. {stmt: "const a vec2 = int(1.1)", err: true},
  3118. {stmt: "const a ivec2 = int(1.1)", err: true},
  3119. {stmt: "const a = float(1.1)", err: false},
  3120. {stmt: "const a bool = float(1.1)", err: true},
  3121. {stmt: "const a int = float(1.1)", err: true},
  3122. {stmt: "const a float = float(1.1)", err: false},
  3123. {stmt: "const a vec2 = float(1.1)", err: true},
  3124. {stmt: "const a ivec2 = float(1.1)", err: true},
  3125. {stmt: "const a = vec2(0)", err: true},
  3126. {stmt: "const a bool = vec2(0)", err: true},
  3127. {stmt: "const a int = vec2(0)", err: true},
  3128. {stmt: "const a float = vec2(0)", err: true},
  3129. {stmt: "const a vec2 = vec2(0)", err: true},
  3130. {stmt: "const a ivec2 = vec2(0)", err: true},
  3131. {stmt: "const a = ivec2(0)", err: true},
  3132. {stmt: "const a bool = ivec2(0)", err: true},
  3133. {stmt: "const a int = ivec2(0)", err: true},
  3134. {stmt: "const a float = ivec2(0)", err: true},
  3135. {stmt: "const a vec2 = ivec2(0)", err: true},
  3136. {stmt: "const a ivec2 = ivec2(0)", err: true},
  3137. }
  3138. for _, c := range cases {
  3139. stmt := c.stmt
  3140. src := fmt.Sprintf(`package main
  3141. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  3142. %s
  3143. return dstPos
  3144. }`, stmt)
  3145. _, err := compileToIR([]byte(src))
  3146. if err == nil && c.err {
  3147. t.Errorf("%s must return an error but does not", stmt)
  3148. } else if err != nil && !c.err {
  3149. t.Errorf("%s must not return nil but returned %v", stmt, err)
  3150. }
  3151. }
  3152. }
  3153. // Issue #2549
  3154. func TestSyntaxConstType2(t *testing.T) {
  3155. cases := []struct {
  3156. stmt string
  3157. err bool
  3158. }{
  3159. {stmt: "const x = 1; y := x*x; _ = vec4(1) / y", err: true},
  3160. {stmt: "const x = 1.0; y := x*x; _ = vec4(1) / y", err: false},
  3161. {stmt: "const x int = 1; y := x*x; _ = vec4(1) / y", err: true},
  3162. {stmt: "const x int = 1.0; y := x*x; _ = vec4(1) / y", err: true},
  3163. {stmt: "const x float = 1; y := x*x; _ = vec4(1) / y", err: false},
  3164. {stmt: "const x float = 1.0; y := x*x; _ = vec4(1) / y", err: false},
  3165. }
  3166. for _, c := range cases {
  3167. stmt := c.stmt
  3168. src := fmt.Sprintf(`package main
  3169. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  3170. %s
  3171. return dstPos
  3172. }`, stmt)
  3173. _, err := compileToIR([]byte(src))
  3174. if err == nil && c.err {
  3175. t.Errorf("%s must return an error but does not", stmt)
  3176. } else if err != nil && !c.err {
  3177. t.Errorf("%s must not return nil but returned %v", stmt, err)
  3178. }
  3179. }
  3180. }
  3181. // Issue #2704
  3182. func TestSyntaxConstType3(t *testing.T) {
  3183. cases := []struct {
  3184. stmt string
  3185. err bool
  3186. }{
  3187. {stmt: "const x = 1; const y = 1; _ = x * y", err: false},
  3188. {stmt: "const x = 1; const y int = 1; _ = x * y", err: false},
  3189. {stmt: "const x int = 1; const y = 1; _ = x * y", err: false},
  3190. {stmt: "const x int = 1; const y int = 1; _ = x * y", err: false},
  3191. {stmt: "const x = 1; const y float = 1; _ = x * y", err: false},
  3192. {stmt: "const x float = 1; const y = 1; _ = x * y", err: false},
  3193. {stmt: "const x float = 1; const y float = 1; _ = x * y", err: false},
  3194. {stmt: "const x int = 1; const y float = 1; _ = x * y", err: true},
  3195. {stmt: "const x float = 1; const y int = 1; _ = x * y", err: true},
  3196. }
  3197. for _, c := range cases {
  3198. stmt := c.stmt
  3199. src := fmt.Sprintf(`package main
  3200. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  3201. %s
  3202. return dstPos
  3203. }`, stmt)
  3204. _, err := compileToIR([]byte(src))
  3205. if err == nil && c.err {
  3206. t.Errorf("%s must return an error but does not", stmt)
  3207. } else if err != nil && !c.err {
  3208. t.Errorf("%s must not return nil but returned %v", stmt, err)
  3209. }
  3210. }
  3211. }
  3212. // Issue #2348
  3213. func TestSyntaxCompositeLit(t *testing.T) {
  3214. cases := []struct {
  3215. stmt string
  3216. err bool
  3217. }{
  3218. {stmt: "_ = undefined{1, 2, 3, 4}", err: true},
  3219. {stmt: "_ = int{1, 2, 3, 4}", err: true},
  3220. {stmt: "_ = vec4{1, 2, 3, 4}", err: true},
  3221. }
  3222. for _, c := range cases {
  3223. stmt := c.stmt
  3224. src := fmt.Sprintf(`package main
  3225. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  3226. %s
  3227. return dstPos
  3228. }`, stmt)
  3229. _, err := compileToIR([]byte(src))
  3230. if err == nil && c.err {
  3231. t.Errorf("%s must return an error but does not", stmt)
  3232. } else if err != nil && !c.err {
  3233. t.Errorf("%s must not return nil but returned %v", stmt, err)
  3234. }
  3235. }
  3236. }
  3237. func TestSyntaxCompilerDirective(t *testing.T) {
  3238. cases := []struct {
  3239. src string
  3240. unit shaderir.Unit
  3241. err bool
  3242. }{
  3243. {
  3244. src: `package main
  3245. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  3246. return dstPos
  3247. }`,
  3248. unit: shaderir.Texels,
  3249. err: false,
  3250. },
  3251. {
  3252. src: `//kage:unit texels
  3253. package main
  3254. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  3255. return dstPos
  3256. }`,
  3257. unit: shaderir.Texels,
  3258. err: false,
  3259. },
  3260. {
  3261. src: `//kage:unit pixels
  3262. package main
  3263. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  3264. return dstPos
  3265. }`,
  3266. unit: shaderir.Pixels,
  3267. err: false,
  3268. },
  3269. {
  3270. src: `//kage:unit foo
  3271. package main
  3272. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  3273. return dstPos
  3274. }`,
  3275. err: true,
  3276. },
  3277. {
  3278. src: `//kage:unit pixels
  3279. //kage:unit pixels
  3280. package main
  3281. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  3282. return dstPos
  3283. }`,
  3284. err: true,
  3285. },
  3286. {
  3287. src: `//kage:unit pixels
  3288. //kage:unit texels
  3289. package main
  3290. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  3291. return dstPos
  3292. }`,
  3293. err: true,
  3294. },
  3295. {
  3296. src: "\t " + `//kage:unit pixels` + " \t\r" + `
  3297. package main
  3298. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  3299. return dstPos
  3300. }`,
  3301. unit: shaderir.Pixels,
  3302. err: false,
  3303. },
  3304. }
  3305. for _, c := range cases {
  3306. ir, err := compileToIR([]byte(c.src))
  3307. if err == nil && c.err {
  3308. t.Errorf("Compile(%q) must return an error but does not", c.src)
  3309. } else if err != nil && !c.err {
  3310. t.Errorf("Compile(%q) must not return nil but returned %v", c.src, err)
  3311. }
  3312. if err != nil || c.err {
  3313. continue
  3314. }
  3315. if got, want := ir.Unit, c.unit; got != want {
  3316. t.Errorf("Compile(%q).Unit: got: %d, want: %d", c.src, got, want)
  3317. }
  3318. }
  3319. }
  3320. // Issue #2654
  3321. func TestSyntaxOmittedReturnType(t *testing.T) {
  3322. if _, err := compileToIR([]byte(`package main
  3323. func foo(x vec2) {
  3324. x = bar(x)
  3325. _ = x
  3326. }
  3327. func bar(x vec2) {
  3328. return x
  3329. }`)); err == nil {
  3330. t.Error("compileToIR must return an error but did not")
  3331. }
  3332. }
  3333. // Issue #2590
  3334. func TestSyntaxAssignToUniformVariables(t *testing.T) {
  3335. if _, err := compileToIR([]byte(`package main
  3336. var Foo float
  3337. func foo(x vec2) {
  3338. Foo = 0
  3339. }`)); err == nil {
  3340. t.Error("compileToIR must return an error but did not")
  3341. }
  3342. if _, err := compileToIR([]byte(`package main
  3343. var Foo float
  3344. func foo(x vec2) {
  3345. var x int
  3346. x, Foo = 0, 0
  3347. _ = x
  3348. }`)); err == nil {
  3349. t.Error("compileToIR must return an error but did not")
  3350. }
  3351. if _, err := compileToIR([]byte(`package main
  3352. var Foo float
  3353. func foo(x vec2) {
  3354. Foo += 0
  3355. }`)); err == nil {
  3356. t.Error("compileToIR must return an error but did not")
  3357. }
  3358. // Issue #2711
  3359. if _, err := compileToIR([]byte(`package main
  3360. var Foo float = 1
  3361. `)); err == nil {
  3362. t.Error("compileToIR must return an error but did not")
  3363. }
  3364. if _, err := compileToIR([]byte(`package main
  3365. var Foo, Bar int = 1, 1
  3366. `)); err == nil {
  3367. t.Error("compileToIR must return an error but did not")
  3368. }
  3369. }
  3370. // Issue #2705
  3371. func TestSyntaxInitWithNegativeInteger(t *testing.T) {
  3372. if _, err := compileToIR([]byte(`package main
  3373. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  3374. var x float = -0
  3375. _ = x
  3376. return dstPos
  3377. }`)); err != nil {
  3378. t.Error(err)
  3379. }
  3380. }
  3381. // Issue #2706
  3382. func TestSyntaxReturnConst(t *testing.T) {
  3383. cases := []struct {
  3384. typ string
  3385. stmt string
  3386. err bool
  3387. }{
  3388. {typ: "bool", stmt: "true", err: false},
  3389. {typ: "int", stmt: "true", err: true},
  3390. {typ: "float", stmt: "true", err: true},
  3391. {typ: "bool", stmt: "1", err: true},
  3392. {typ: "int", stmt: "1", err: false},
  3393. {typ: "float", stmt: "1", err: false},
  3394. {typ: "bool", stmt: "1.0", err: true},
  3395. {typ: "int", stmt: "1.0", err: false},
  3396. {typ: "float", stmt: "1.0", err: false},
  3397. {typ: "bool", stmt: "1.1", err: true},
  3398. {typ: "int", stmt: "1.1", err: true},
  3399. {typ: "float", stmt: "1.1", err: false},
  3400. }
  3401. for _, c := range cases {
  3402. typ := c.typ
  3403. stmt := c.stmt
  3404. src := fmt.Sprintf(`package main
  3405. func Foo() %s {
  3406. return %s
  3407. }
  3408. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  3409. return dstPos
  3410. }`, typ, stmt)
  3411. _, err := compileToIR([]byte(src))
  3412. if err == nil && c.err {
  3413. t.Errorf("return %s for type %s must return an error but does not", stmt, typ)
  3414. } else if err != nil && !c.err {
  3415. t.Errorf("return %s for type %s must not return nil but returned %v", stmt, typ, err)
  3416. }
  3417. }
  3418. }
  3419. // Issue #2706
  3420. func TestSyntaxScalarAndVector(t *testing.T) {
  3421. cases := []struct {
  3422. stmt string
  3423. err bool
  3424. }{
  3425. {stmt: "a := vec2(1) + 1; var b vec2 = a; _ = b", err: false},
  3426. {stmt: "a := 1 + vec2(1); var b vec2 = a; _ = b", err: false},
  3427. {stmt: "a := vec2(1); b := 1; var c vec2 = a + b; _ = c", err: true},
  3428. {stmt: "a := vec2(1); b := 1; var c vec2 = b + a; _ = c", err: true},
  3429. {stmt: "a := vec2(1) + 1.0; var b vec2 = a; _ = b", err: false},
  3430. {stmt: "a := 1.0 + vec2(1); var b vec2 = a; _ = b", err: false},
  3431. {stmt: "a := vec2(1); b := 1.0; var c vec2 = a + b; _ = c", err: false},
  3432. {stmt: "a := vec2(1); b := 1.0; var c vec2 = b + a; _ = c", err: false},
  3433. {stmt: "a := vec2(1) + 1.1; var b vec2 = a; _ = b", err: false},
  3434. {stmt: "a := 1.1 + vec2(1); var b vec2 = a; _ = b", err: false},
  3435. {stmt: "a := vec2(1); b := 1.1; var c vec2 = a + b; _ = c", err: false},
  3436. {stmt: "a := vec2(1); b := 1.1; var c vec2 = b + a; _ = c", err: false},
  3437. {stmt: "a := ivec2(1) + 1; var b ivec2 = a; _ = b", err: false},
  3438. {stmt: "a := 1 + ivec2(1); var b ivec2 = a; _ = b", err: false},
  3439. {stmt: "a := ivec2(1); b := 1; var c ivec2 = a + b; _ = c", err: false},
  3440. {stmt: "a := ivec2(1); b := 1; var c ivec2 = b + a; _ = c", err: false},
  3441. {stmt: "a := ivec2(1) + 1.0; var b ivec2 = a; _ = b", err: false},
  3442. {stmt: "a := 1.0 + ivec2(1); var b ivec2 = a; _ = b", err: false},
  3443. {stmt: "a := ivec2(1); b := 1.0; var c ivec2 = a + b; _ = c", err: true},
  3444. {stmt: "a := ivec2(1); b := 1.0; var c ivec2 = b + a; _ = c", err: true},
  3445. {stmt: "a := ivec2(1) + 1.1; var b ivec2 = a; _ = b", err: true},
  3446. {stmt: "a := 1.1 + ivec2(1); var b ivec2 = a; _ = b", err: true},
  3447. {stmt: "a := ivec2(1); b := 1.1; var c ivec2 = a + b; _ = c", err: true},
  3448. {stmt: "a := ivec2(1); b := 1.1; var c ivec2 = b + a; _ = c", err: true},
  3449. }
  3450. for _, c := range cases {
  3451. stmt := c.stmt
  3452. src := fmt.Sprintf(`package main
  3453. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  3454. %s
  3455. return dstPos
  3456. }`, stmt)
  3457. _, err := compileToIR([]byte(src))
  3458. if err == nil && c.err {
  3459. t.Errorf("%s must return an error but does not", stmt)
  3460. } else if err != nil && !c.err {
  3461. t.Errorf("%s must not return nil but returned %v", stmt, err)
  3462. }
  3463. }
  3464. }
  3465. // Issue #2712
  3466. func TestSyntaxCast(t *testing.T) {
  3467. cases := []struct {
  3468. stmt string
  3469. err bool
  3470. }{
  3471. {stmt: "a := int(1); _ = a", err: false},
  3472. {stmt: "a := int(1.0); _ = a", err: false},
  3473. {stmt: "a := int(1.1); _ = a", err: true},
  3474. {stmt: "const c = 1.1; a := int(c); _ = a", err: true},
  3475. {stmt: "const c float = 1.1; a := int(c); _ = a", err: true},
  3476. {stmt: "a := float(1); _ = a", err: false},
  3477. {stmt: "a := float(1.0); _ = a", err: false},
  3478. {stmt: "a := float(1.1); _ = a", err: false},
  3479. {stmt: "a := 1; _ = int(a)", err: false},
  3480. {stmt: "a := 1.0; _ = int(a)", err: false},
  3481. {stmt: "a := 1.1; _ = int(a)", err: false},
  3482. {stmt: "a := 1; _ = float(a)", err: false},
  3483. {stmt: "a := 1.0; _ = float(a)", err: false},
  3484. {stmt: "a := 1.1; _ = float(a)", err: false},
  3485. }
  3486. for _, c := range cases {
  3487. stmt := c.stmt
  3488. src := fmt.Sprintf(`package main
  3489. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  3490. %s
  3491. return dstPos
  3492. }`, stmt)
  3493. _, err := compileToIR([]byte(src))
  3494. if err == nil && c.err {
  3495. t.Errorf("%s must return an error but does not", stmt)
  3496. } else if err != nil && !c.err {
  3497. t.Errorf("%s must not return nil but returned %v", stmt, err)
  3498. }
  3499. }
  3500. }
  3501. // Issue #2718
  3502. func TestSyntaxCompare(t *testing.T) {
  3503. cases := []struct {
  3504. stmt string
  3505. err bool
  3506. }{
  3507. {stmt: "_ = false == true", err: false},
  3508. {stmt: "_ = int(0) == int(1)", err: false},
  3509. {stmt: "_ = float(0) == float(1)", err: false},
  3510. {stmt: "_ = vec2(0) == vec2(1)", err: false},
  3511. {stmt: "_ = vec3(0) == vec3(1)", err: false},
  3512. {stmt: "_ = vec4(0) == vec4(1)", err: false},
  3513. {stmt: "_ = ivec2(0) == ivec2(1)", err: false},
  3514. {stmt: "_ = ivec3(0) == ivec3(1)", err: false},
  3515. {stmt: "_ = ivec4(0) == ivec4(1)", err: false},
  3516. {stmt: "_ = mat2(0) == mat2(1)", err: true},
  3517. {stmt: "_ = mat3(0) == mat3(1)", err: true},
  3518. {stmt: "_ = mat4(0) == mat4(1)", err: true},
  3519. {stmt: "_ = false != true", err: false},
  3520. {stmt: "_ = int(0) != int(1)", err: false},
  3521. {stmt: "_ = float(0) != float(1)", err: false},
  3522. {stmt: "_ = vec2(0) != vec2(1)", err: false},
  3523. {stmt: "_ = vec3(0) != vec3(1)", err: false},
  3524. {stmt: "_ = vec4(0) != vec4(1)", err: false},
  3525. {stmt: "_ = ivec2(0) != ivec2(1)", err: false},
  3526. {stmt: "_ = ivec3(0) != ivec3(1)", err: false},
  3527. {stmt: "_ = ivec4(0) != ivec4(1)", err: false},
  3528. {stmt: "_ = mat2(0) != mat2(1)", err: true},
  3529. {stmt: "_ = mat3(0) != mat3(1)", err: true},
  3530. {stmt: "_ = mat4(0) != mat4(1)", err: true},
  3531. {stmt: "_ = false < true", err: true},
  3532. {stmt: "_ = int(0) < int(1)", err: false},
  3533. {stmt: "_ = float(0) < float(1)", err: false},
  3534. {stmt: "_ = vec2(0) < vec2(1)", err: true},
  3535. {stmt: "_ = vec3(0) < vec3(1)", err: true},
  3536. {stmt: "_ = vec4(0) < vec4(1)", err: true},
  3537. {stmt: "_ = ivec2(0) < ivec2(1)", err: true},
  3538. {stmt: "_ = ivec3(0) < ivec3(1)", err: true},
  3539. {stmt: "_ = ivec4(0) < ivec4(1)", err: true},
  3540. {stmt: "_ = mat2(0) < mat2(1)", err: true},
  3541. {stmt: "_ = mat3(0) < mat3(1)", err: true},
  3542. {stmt: "_ = mat4(0) < mat4(1)", err: true},
  3543. {stmt: "_ = false <= true", err: true},
  3544. {stmt: "_ = int(0) <= int(1)", err: false},
  3545. {stmt: "_ = float(0) <= float(1)", err: false},
  3546. {stmt: "_ = vec2(0) <= vec2(1)", err: true},
  3547. {stmt: "_ = vec3(0) <= vec3(1)", err: true},
  3548. {stmt: "_ = vec4(0) <= vec4(1)", err: true},
  3549. {stmt: "_ = ivec2(0) <= ivec2(1)", err: true},
  3550. {stmt: "_ = ivec3(0) <= ivec3(1)", err: true},
  3551. {stmt: "_ = ivec4(0) <= ivec4(1)", err: true},
  3552. {stmt: "_ = mat2(0) <= mat2(1)", err: true},
  3553. {stmt: "_ = mat3(0) <= mat3(1)", err: true},
  3554. {stmt: "_ = mat4(0) <= mat4(1)", err: true},
  3555. {stmt: "_ = false > true", err: true},
  3556. {stmt: "_ = int(0) > int(1)", err: false},
  3557. {stmt: "_ = float(0) > float(1)", err: false},
  3558. {stmt: "_ = vec2(0) > vec2(1)", err: true},
  3559. {stmt: "_ = vec3(0) > vec3(1)", err: true},
  3560. {stmt: "_ = vec4(0) > vec4(1)", err: true},
  3561. {stmt: "_ = ivec2(0) > ivec2(1)", err: true},
  3562. {stmt: "_ = ivec3(0) > ivec3(1)", err: true},
  3563. {stmt: "_ = ivec4(0) > ivec4(1)", err: true},
  3564. {stmt: "_ = mat2(0) > mat2(1)", err: true},
  3565. {stmt: "_ = mat3(0) > mat3(1)", err: true},
  3566. {stmt: "_ = mat4(0) > mat4(1)", err: true},
  3567. {stmt: "_ = false >= true", err: true},
  3568. {stmt: "_ = int(0) >= int(1)", err: false},
  3569. {stmt: "_ = float(0) >= float(1)", err: false},
  3570. {stmt: "_ = vec2(0) >= vec2(1)", err: true},
  3571. {stmt: "_ = vec3(0) >= vec3(1)", err: true},
  3572. {stmt: "_ = vec4(0) >= vec4(1)", err: true},
  3573. {stmt: "_ = ivec2(0) >= ivec2(1)", err: true},
  3574. {stmt: "_ = ivec3(0) >= ivec3(1)", err: true},
  3575. {stmt: "_ = ivec4(0) >= ivec4(1)", err: true},
  3576. {stmt: "_ = mat2(0) >= mat2(1)", err: true},
  3577. {stmt: "_ = mat3(0) >= mat3(1)", err: true},
  3578. {stmt: "_ = mat4(0) >= mat4(1)", err: true},
  3579. }
  3580. for _, c := range cases {
  3581. stmt := c.stmt
  3582. src := fmt.Sprintf(`package main
  3583. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  3584. %s
  3585. return dstPos
  3586. }`, stmt)
  3587. _, err := compileToIR([]byte(src))
  3588. if err == nil && c.err {
  3589. t.Errorf("%s must return an error but does not", stmt)
  3590. } else if err != nil && !c.err {
  3591. t.Errorf("%s must not return nil but returned %v", stmt, err)
  3592. }
  3593. }
  3594. }
  3595. // Issue #2680
  3596. func TestSyntaxForWithLocalVariable(t *testing.T) {
  3597. if _, err := compileToIR([]byte(`package main
  3598. func foo() {
  3599. i := 0
  3600. for i = 0; i < 1; i++ {
  3601. }
  3602. }`)); err == nil {
  3603. t.Error("compileToIR must return an error but did not")
  3604. }
  3605. if _, err := compileToIR([]byte(`package main
  3606. func foo() {
  3607. for i, j := 0, 0; i < 1; i++ {
  3608. _ = j
  3609. }
  3610. }`)); err == nil {
  3611. t.Error("compileToIR must return an error but did not")
  3612. }
  3613. }
  3614. // Issue #2648
  3615. func TestSyntaxDuplicatedUniformVariables(t *testing.T) {
  3616. if _, err := compileToIR([]byte(`package main
  3617. var Foo int
  3618. var Foo int
  3619. `)); err == nil {
  3620. t.Error("compileToIR must return an error but did not")
  3621. }
  3622. if _, err := compileToIR([]byte(`package main
  3623. var Foo int
  3624. var Bar float
  3625. var Foo vec2
  3626. `)); err == nil {
  3627. t.Error("compileToIR must return an error but did not")
  3628. }
  3629. }
  3630. // Issue #2747
  3631. func TestSyntaxMultipleAssignmentsAndTypeCheck(t *testing.T) {
  3632. if _, err := compileToIR([]byte(`package main
  3633. func Foo() (float, bool) {
  3634. return 0, false
  3635. }
  3636. func Bar() {
  3637. f, b := Foo()
  3638. _, _ = f, b
  3639. return
  3640. }
  3641. `)); err != nil {
  3642. t.Error(err)
  3643. }
  3644. if _, err := compileToIR([]byte(`package main
  3645. func Foo() (float, bool) {
  3646. return 0, false
  3647. }
  3648. func Bar() {
  3649. var f float
  3650. var b bool
  3651. f, b = Foo()
  3652. _, _ = f, b
  3653. return
  3654. }
  3655. `)); err != nil {
  3656. t.Error(err)
  3657. }
  3658. if _, err := compileToIR([]byte(`package main
  3659. func Foo() {
  3660. a, b := 0
  3661. _, _ = a, b
  3662. }
  3663. `)); err == nil {
  3664. t.Error("compileToIR must return an error but did not")
  3665. }
  3666. if _, err := compileToIR([]byte(`package main
  3667. func Foo() {
  3668. a, b, c := 0, 0
  3669. _, _ = a, b, c
  3670. }
  3671. `)); err == nil {
  3672. t.Error("compileToIR must return an error but did not")
  3673. }
  3674. if _, err := compileToIR([]byte(`package main
  3675. func Foo() {
  3676. var a, b int
  3677. a, b = 0
  3678. _, _ = a, b
  3679. }
  3680. `)); err == nil {
  3681. t.Error("compileToIR must return an error but did not")
  3682. }
  3683. if _, err := compileToIR([]byte(`package main
  3684. func Foo() {
  3685. var a, b, c int
  3686. a, b, c = 0, 0
  3687. _, _ = a, b, c
  3688. }
  3689. `)); err == nil {
  3690. t.Error("compileToIR must return an error but did not")
  3691. }
  3692. }
  3693. func TestSyntaxBitwiseOperator(t *testing.T) {
  3694. cases := []struct {
  3695. stmt string
  3696. err bool
  3697. }{
  3698. {stmt: "_ = false & true", err: true},
  3699. {stmt: "_ = int(0) & int(1)", err: false},
  3700. {stmt: "_ = float(0) & float(1)", err: true},
  3701. {stmt: "_ = vec2(0) & vec2(1)", err: true},
  3702. {stmt: "_ = vec3(0) & vec3(1)", err: true},
  3703. {stmt: "_ = vec4(0) & vec4(1)", err: true},
  3704. {stmt: "_ = ivec2(0) & ivec2(1)", err: false},
  3705. {stmt: "_ = ivec3(0) & ivec3(1)", err: false},
  3706. {stmt: "_ = ivec4(0) & ivec4(1)", err: false},
  3707. {stmt: "_ = ivec2(0) & int(1)", err: false},
  3708. {stmt: "_ = ivec3(0) & int(1)", err: false},
  3709. {stmt: "_ = ivec4(0) & int(1)", err: false},
  3710. {stmt: "_ = int(0) & ivec2(1)", err: false},
  3711. {stmt: "_ = int(0) & ivec3(1)", err: false},
  3712. {stmt: "_ = int(0) & ivec4(1)", err: false},
  3713. {stmt: "_ = mat2(0) & mat2(1)", err: true},
  3714. {stmt: "_ = mat3(0) & mat3(1)", err: true},
  3715. {stmt: "_ = mat4(0) & mat4(1)", err: true},
  3716. {stmt: "_ = false | true", err: true},
  3717. {stmt: "_ = int(0) | int(1)", err: false},
  3718. {stmt: "_ = float(0) | float(1)", err: true},
  3719. {stmt: "_ = vec2(0) | vec2(1)", err: true},
  3720. {stmt: "_ = vec3(0) | vec3(1)", err: true},
  3721. {stmt: "_ = vec4(0) | vec4(1)", err: true},
  3722. {stmt: "_ = ivec2(0) | ivec2(1)", err: false},
  3723. {stmt: "_ = ivec3(0) | ivec3(1)", err: false},
  3724. {stmt: "_ = ivec4(0) | ivec4(1)", err: false},
  3725. {stmt: "_ = ivec2(0) | int(1)", err: false},
  3726. {stmt: "_ = ivec3(0) | int(1)", err: false},
  3727. {stmt: "_ = ivec4(0) | int(1)", err: false},
  3728. {stmt: "_ = int(0) | ivec2(1)", err: false},
  3729. {stmt: "_ = int(0) | ivec3(1)", err: false},
  3730. {stmt: "_ = int(0) | ivec4(1)", err: false},
  3731. {stmt: "_ = mat2(0) | mat2(1)", err: true},
  3732. {stmt: "_ = mat3(0) | mat3(1)", err: true},
  3733. {stmt: "_ = mat4(0) | mat4(1)", err: true},
  3734. {stmt: "_ = false ^ true", err: true},
  3735. {stmt: "_ = int(0) ^ int(1)", err: false},
  3736. {stmt: "_ = float(0) ^ float(1)", err: true},
  3737. {stmt: "_ = vec2(0) ^ vec2(1)", err: true},
  3738. {stmt: "_ = vec3(0) ^ vec3(1)", err: true},
  3739. {stmt: "_ = vec4(0) ^ vec4(1)", err: true},
  3740. {stmt: "_ = ivec2(0) ^ ivec2(1)", err: false},
  3741. {stmt: "_ = ivec3(0) ^ ivec3(1)", err: false},
  3742. {stmt: "_ = ivec4(0) ^ ivec4(1)", err: false},
  3743. {stmt: "_ = ivec2(0) ^ int(1)", err: false},
  3744. {stmt: "_ = ivec3(0) ^ int(1)", err: false},
  3745. {stmt: "_ = ivec4(0) ^ int(1)", err: false},
  3746. {stmt: "_ = int(0) ^ ivec2(1)", err: false},
  3747. {stmt: "_ = int(0) ^ ivec3(1)", err: false},
  3748. {stmt: "_ = int(0) ^ ivec4(1)", err: false},
  3749. {stmt: "_ = mat2(0) ^ mat2(1)", err: true},
  3750. {stmt: "_ = mat3(0) ^ mat3(1)", err: true},
  3751. {stmt: "_ = mat4(0) ^ mat4(1)", err: true},
  3752. }
  3753. for _, c := range cases {
  3754. stmt := c.stmt
  3755. src := fmt.Sprintf(`package main
  3756. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  3757. %s
  3758. return dstPos
  3759. }`, stmt)
  3760. _, err := compileToIR([]byte(src))
  3761. if err == nil && c.err {
  3762. t.Errorf("%s must return an error but does not", stmt)
  3763. } else if err != nil && !c.err {
  3764. t.Errorf("%s must not return nil but returned %v", stmt, err)
  3765. }
  3766. }
  3767. }
  3768. // Issue #2891
  3769. func TestSyntaxInvalidArgument(t *testing.T) {
  3770. if _, err := compileToIR([]byte(`package main
  3771. func Foo(x int) int {
  3772. return 0
  3773. }
  3774. func Bar() int {
  3775. return Foo(Foo)
  3776. }
  3777. `)); err == nil {
  3778. t.Error("compileToIR must return an error but did not")
  3779. }
  3780. }
  3781. // Issue #2891, #2910
  3782. func TestSyntaxTailingUnaryOperator(t *testing.T) {
  3783. if _, err := compileToIR([]byte(`package main
  3784. func Foo() {
  3785. 1 + x := vec2(2)
  3786. }
  3787. `)); err == nil {
  3788. t.Error("compileToIR must return an error but did not")
  3789. }
  3790. if _, err := compileToIR([]byte(`package main
  3791. func Foo() {
  3792. 1 + x, y := Bar()
  3793. }
  3794. func Bar() (int, int) {
  3795. return 0, 0
  3796. }
  3797. `)); err == nil {
  3798. t.Error("compileToIR must return an error but did not")
  3799. }
  3800. }
  3801. // Issue #2926, #2989
  3802. func TestSyntaxNonTypeExpression(t *testing.T) {
  3803. if _, err := compileToIR([]byte(`package main
  3804. func Foo() {
  3805. }
  3806. func Bar() float {
  3807. return +Foo
  3808. }
  3809. `)); err == nil {
  3810. t.Error("compileToIR must return an error but did not")
  3811. }
  3812. if _, err := compileToIR([]byte(`package main
  3813. func Foo() {
  3814. }
  3815. func Bar() float {
  3816. return Foo + 1.0
  3817. }
  3818. `)); err == nil {
  3819. t.Error("compileToIR must return an error but did not")
  3820. }
  3821. if _, err := compileToIR([]byte(`package main
  3822. func Foo() {
  3823. }
  3824. func Bar() float {
  3825. return 1.0 + Foo
  3826. }
  3827. `)); err == nil {
  3828. t.Error("compileToIR must return an error but did not")
  3829. }
  3830. if _, err := compileToIR([]byte(`package main
  3831. func Foo() {
  3832. }
  3833. func Bar() float {
  3834. return Foo.x
  3835. }
  3836. `)); err == nil {
  3837. t.Error("compileToIR must return an error but did not")
  3838. }
  3839. if _, err := compileToIR([]byte(`package main
  3840. func Foo() {
  3841. }
  3842. func Bar() float {
  3843. return Foo[0]
  3844. }
  3845. `)); err == nil {
  3846. t.Error("compileToIR must return an error but did not")
  3847. }
  3848. }
  3849. // Issue #2993
  3850. func TestSyntaxIfAndConstBool(t *testing.T) {
  3851. if _, err := compileToIR([]byte(`package main
  3852. func Foo() int {
  3853. const X = true
  3854. if X {
  3855. return 1
  3856. }
  3857. return 0
  3858. }
  3859. `)); err != nil {
  3860. t.Error(err)
  3861. }
  3862. if _, err := compileToIR([]byte(`package main
  3863. func Foo() int {
  3864. const X bool = true
  3865. if X {
  3866. return 1
  3867. }
  3868. return 0
  3869. }
  3870. `)); err != nil {
  3871. t.Error(err)
  3872. }
  3873. }
  3874. // Issue #3111
  3875. func TestSyntaxTooManyElementsAtInitialization(t *testing.T) {
  3876. cases := []struct {
  3877. stmt string
  3878. err bool
  3879. }{
  3880. {stmt: "_ = [-1]int{}", err: true},
  3881. {stmt: "_ = [-1]int{0}", err: true},
  3882. {stmt: "_ = [-1]int{0, 0}", err: true},
  3883. {stmt: "_ = [-1]int{0, 0, 0}", err: true},
  3884. {stmt: "_ = [0]int{}", err: false},
  3885. {stmt: "_ = [0]int{0}", err: true},
  3886. {stmt: "_ = [0]int{0, 0}", err: true},
  3887. {stmt: "_ = [0]int{0, 0, 0}", err: true},
  3888. {stmt: "_ = [1]int{}", err: false},
  3889. {stmt: "_ = [1]int{0}", err: false},
  3890. {stmt: "_ = [1]int{0, 0}", err: true},
  3891. {stmt: "_ = [1]int{0, 0, 0}", err: true},
  3892. {stmt: "_ = [2]int{}", err: false},
  3893. {stmt: "_ = [2]int{0}", err: false},
  3894. {stmt: "_ = [2]int{0, 0}", err: false},
  3895. {stmt: "_ = [2]int{0, 0, 0}", err: true},
  3896. }
  3897. for _, c := range cases {
  3898. stmt := c.stmt
  3899. src := fmt.Sprintf(`package main
  3900. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  3901. %s
  3902. return dstPos
  3903. }`, stmt)
  3904. _, err := compileToIR([]byte(src))
  3905. if err == nil && c.err {
  3906. t.Errorf("%s must return an error but does not", stmt)
  3907. } else if err != nil && !c.err {
  3908. t.Errorf("%s must not return nil but returned %v", stmt, err)
  3909. }
  3910. }
  3911. }
  3912. func TestSyntaxArrayOutOfBounds(t *testing.T) {
  3913. cases := []struct {
  3914. stmt string
  3915. err bool
  3916. }{
  3917. {stmt: "a := [0]int{}; _ = a[-1]", err: true},
  3918. {stmt: "a := [0]int{}; _ = a[0]", err: true},
  3919. {stmt: "a := [0]int{}; _ = a[1]", err: true},
  3920. {stmt: "a := [0]int{}; _ = a[2]", err: true},
  3921. {stmt: "a := [0]int{}; _ = a[3]", err: true},
  3922. {stmt: "a := [0]int{}; b := -1; _ = a[b]", err: false},
  3923. {stmt: "a := [0]int{}; b := 0; _ = a[b]", err: false},
  3924. {stmt: "a := [0]int{}; b := 1; _ = a[b]", err: false},
  3925. {stmt: "a := [0]int{}; b := 2; _ = a[b]", err: false},
  3926. {stmt: "a := [0]int{}; b := 3; _ = a[b]", err: false},
  3927. {stmt: "a := [1]int{}; _ = a[-1]", err: true},
  3928. {stmt: "a := [1]int{}; _ = a[0]", err: false},
  3929. {stmt: "a := [1]int{}; _ = a[1]", err: true},
  3930. {stmt: "a := [1]int{}; _ = a[2]", err: true},
  3931. {stmt: "a := [1]int{}; _ = a[3]", err: true},
  3932. {stmt: "a := [1]int{}; b := -1; _ = a[b]", err: false},
  3933. {stmt: "a := [1]int{}; b := 0; _ = a[b]", err: false},
  3934. {stmt: "a := [1]int{}; b := 1; _ = a[b]", err: false},
  3935. {stmt: "a := [1]int{}; b := 2; _ = a[b]", err: false},
  3936. {stmt: "a := [1]int{}; b := 3; _ = a[b]", err: false},
  3937. {stmt: "a := [2]int{}; _ = a[-1]", err: true},
  3938. {stmt: "a := [2]int{}; _ = a[0]", err: false},
  3939. {stmt: "a := [2]int{}; _ = a[1]", err: false},
  3940. {stmt: "a := [2]int{}; _ = a[2]", err: true},
  3941. {stmt: "a := [2]int{}; _ = a[3]", err: true},
  3942. {stmt: "a := [2]int{}; b := -1; _ = a[b]", err: false},
  3943. {stmt: "a := [2]int{}; b := 0; _ = a[b]", err: false},
  3944. {stmt: "a := [2]int{}; b := 1; _ = a[b]", err: false},
  3945. {stmt: "a := [2]int{}; b := 2; _ = a[b]", err: false},
  3946. {stmt: "a := [2]int{}; b := 3; _ = a[b]", err: false},
  3947. {stmt: "a := vec2(0); _ = a[-1]", err: true},
  3948. {stmt: "a := vec2(0); _ = a[0]", err: false},
  3949. {stmt: "a := vec2(0); _ = a[1]", err: false},
  3950. {stmt: "a := vec2(0); _ = a[2]", err: true},
  3951. {stmt: "a := vec2(0); _ = a[3]", err: true},
  3952. {stmt: "a := vec2(0); b := -1; _ = a[b]", err: false},
  3953. {stmt: "a := vec2(0); b := 0; _ = a[b]", err: false},
  3954. {stmt: "a := vec2(0); b := 1; _ = a[b]", err: false},
  3955. {stmt: "a := vec2(0); b := 2; _ = a[b]", err: false},
  3956. {stmt: "a := vec2(0); b := 3; _ = a[b]", err: false},
  3957. {stmt: "a := mat3(0); _ = a[-1]", err: true},
  3958. {stmt: "a := mat3(0); _ = a[0]", err: false},
  3959. {stmt: "a := mat3(0); _ = a[1]", err: false},
  3960. {stmt: "a := mat3(0); _ = a[2]", err: false},
  3961. {stmt: "a := mat3(0); _ = a[3]", err: true},
  3962. {stmt: "a := mat3(0); b := -1; _ = a[b]", err: false},
  3963. {stmt: "a := mat3(0); b := 0; _ = a[b]", err: false},
  3964. {stmt: "a := mat3(0); b := 1; _ = a[b]", err: false},
  3965. {stmt: "a := mat3(0); b := 2; _ = a[b]", err: false},
  3966. {stmt: "a := mat3(0); b := 3; _ = a[b]", err: false},
  3967. }
  3968. for _, c := range cases {
  3969. stmt := c.stmt
  3970. src := fmt.Sprintf(`package main
  3971. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  3972. %s
  3973. return dstPos
  3974. }`, stmt)
  3975. _, err := compileToIR([]byte(src))
  3976. if err == nil && c.err {
  3977. t.Errorf("%s must return an error but does not", stmt)
  3978. } else if err != nil && !c.err {
  3979. t.Errorf("%s must not return nil but returned %v", stmt, err)
  3980. }
  3981. }
  3982. }