Bug 16041 - parse returns non-deterministic results from identical input.
Summary: parse returns non-deterministic results from identical input.
Status: NEW
Alias: None
Product: R
Classification: Unclassified
Component: Low-level (show other bugs)
Version: R 3.1.1
Hardware: x86_64/x64/amd64 (64-bit) OS X Mavericks
: P5 enhancement
Assignee: R-core
URL:
Depends on:
Blocks:
 
Reported: 2014-10-23 14:19 UTC by Jim Hester
Modified: 2014-11-04 14:46 UTC (History)
1 user (show)

See Also:


Attachments
R file which is incorrectly parsed. (192 bytes, text/plain)
2014-10-23 14:19 UTC, Jim Hester
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Jim Hester 2014-10-23 14:19:42 UTC
Created attachment 1677 [details]
R file which is incorrectly parsed.

If you define a function 

> srcdata <- function(filename) {
>  text <- readLines(filename)
>  sf <- srcfilecopy(filename, text, isFile = TRUE)
>  parse(text=sf$lines, srcfile=sf)
> }

and then call it on the attached file two consecutive times you get different results for the parseData element.  Any further calls yield the same results as the second call, the very first call is the only one with incorrect results.  If utils::getParseData is used to format the parseData attribute the difference seems to be an incorrect parent assignment.

> t1 <- srcdata('bad_parse.R')
> t2 <- srcdata('bad_parse.R')
> all.equal(t1$parseData, t2$parseData)
> [1] "Mean relative difference: 1"
> all.equal(getParseData(t1), getParseData(t2))
> [1] "Component “parent”: Mean relative difference: 1"
> t3 <- srcdata('bad_parse.R')
> all.equal(t2$parseData, t3$parseData)
> [1] TRUE
> all.equal(getParseData(t2), getParseData(t3))
> [1] TRUE

Note this bug only appears with very specific inputs, and even whitespace changes to the attached file can prevent it from occurring.  I tried to reduce the example as much as I could while preserving the incorrect behavior.
Comment 1 Jim Hester 2014-10-23 14:38:54 UTC
Sorry the correct test function should be 

> srcdata <- function(filename) {
>  text <- readLines(filename)
>  sf <- srcfilecopy(filename, text, isFile = TRUE)
>  parse(text=sf$lines, srcfile=sf)
>  sf
> }
Comment 2 Duncan Murdoch 2014-11-03 22:31:14 UTC
I can confirm the problem, but so far haven't spotted exactly what is causing it.  I'll try again when I have a couple of hours to spare to play with it.
Comment 3 Duncan Murdoch 2014-11-04 14:46:26 UTC
Now I'm not seeing it.  It acts like an uninitialized variable:  sometimes it works, sometimes it doesn't, depending on what happened to be in memory first.  But if I can't see it happen, I won't be able to fix it, so I'm going to have to abandon this for now.