on master help sign in
fix bugs in fromTypeVariable and fromTypeVariableScheme

# PROBLEM The problem started with the fact that getIndexSize was initially written to already include the +2 needed by fromVarIndex. I guess I hand optimized that, but did not reflect it in the name clearly. I guess after that, I used the getIndexSize function in fromTypeVariable and fromTypeVariableScheme without accounting for that padded +2. That meant they both were producing strings with 2 spots of whatever from the memory that happens to be around when the MBA is allocated. # STORY We were compiling a project with 700+ modules. On the initial build, all the files were compiled. With the new compiler, we would expect touching one file to result in only one file being compiled, no matter how many dependencies it has. (It compares against the public interface from last time, and if it's the same, we know everything will keep working in all the dependencies!) So on the incremental build, we touch a file that is imported by many other files, but instead of 1 file being compiled, we see 108. Weird! Trying this again on a second incremental build, we saw just 1 file being compiled. Very weird! It turned out that if you exposed values without type annotations, it was possible that the compiler would need to generate some type variable names for you. Those would then be written into the interface files, and subsequently compared to other interface files to see if it is exactly the same. So with this bug unresolved, it was possible for the messed up names to clash in this context!

commit879f285618c369504e8e639eadbb34e9c2df609d authorEvan Czaplicki <evancz@users.noreply.github.com> dateWed 3 Jul 2019, 18:41 (7 years ago) parenta1c3e30
1 file changed+14−13
compiler/src/Data/Name.hs+14−13
compiler/src/Data/Name.hs+14−13
@@ -202,11 +202,11 @@ fromVarIndex :: Int -> Name
202202fromVarIndex n =
203203 runST
204204 (
205− do let !size = getIndexSize n
205+ do let !size = 2 + getIndexSize n
206206 mba <- newByteArray size
207207 writeWord8 mba 0 0x5F {- _ -}
208208 writeWord8 mba 1 0x76 {- v -}
209− writeDigits mba (size - 1) n
209+ writeDigitsAtEnd mba size n
210210 freeze mba
211211 )
212212
@@ -214,19 +214,20 @@ fromVarIndex n =
214214{-# INLINE getIndexSize #-}
215215getIndexSize :: Int -> Int
216216getIndexSize n
217− | n < 10 = 3
218− | n < 100 = 4
219− | True = 2 + ceiling (logBase 10 (fromIntegral n + 1) :: Float)
217+ | n < 10 = 1
218+ | n < 100 = 2
219+ | True = ceiling (logBase 10 (fromIntegral n + 1) :: Float)
220220
221221
222222
223−writeDigits :: MBA s -> Int -> Int -> ST s ()
224−writeDigits !mba !offset !n =
223+writeDigitsAtEnd :: MBA s -> Int -> Int -> ST s ()
224+writeDigitsAtEnd !mba !oldOffset !n =
225225 do let (q,r) = quotRem n 10
226− writeWord8 mba offset (0x30 + fromIntegral r)
226+ let !newOffset = oldOffset - 1
227+ writeWord8 mba newOffset (0x30 + fromIntegral r)
227228 if q <= 0
228229 then return ()
229− else writeDigits mba (offset-1) q
230+ else writeDigitsAtEnd mba newOffset q
230231
231232
232233
@@ -249,7 +250,7 @@ fromTypeVariable name@(Utf8.Utf8 ba#) index =
249250 mba <- newByteArray size
250251 copyToMBA name mba
251252 writeWord8 mba (I# len#) 0x5F {- _ -}
252− writeDigits mba (size - 1) index
253+ writeDigitsAtEnd mba size index
253254 freeze mba
254255 )
255256 else
@@ -258,7 +259,7 @@ fromTypeVariable name@(Utf8.Utf8 ba#) index =
258259 do let !size = I# len# + getIndexSize index
259260 mba <- newByteArray size
260261 copyToMBA name mba
261− writeDigits mba (size - 1) index
262+ writeDigitsAtEnd mba size index
262263 freeze mba
263264 )
264265
@@ -277,10 +278,10 @@ fromTypeVariableScheme scheme =
277278 freeze mba
278279 else
279280 do let (extra, letter) = quotRem scheme 26
280− let !size = getIndexSize extra
281+ let !size = 1 + getIndexSize extra
281282 mba <- newByteArray size
282283 writeWord8 mba 0 (0x61 + fromIntegral letter)
283− writeDigits mba (size - 1) extra
284+ writeDigitsAtEnd mba size extra
284285 freeze mba
285286 )
286287