stan-dev / stan-dev/rstan

Line number tracker for error output does not count empty lines in stan code (for both syntax and runtime errors)

Open
#1,027 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
R
Stars
1.1k
Forks
266
Avg merge
2h 56m
Merged PRs (30d)
1

Description

Summary:

The line number output by rstan in case of syntax or runtime errors does not account for blank lines in the code. The line indicated in the error message is therefore too low. This makes it harder to find what part of the code was wrong to begin with.

Description:

If Stan code contains no blank lines then the line number output by rstan in case of syntax (or runtime) errors correctly indicates on what line the problem was discovered, and you can identify the line in your editor. However, if blank lines are inserted into the Stan program before the erroneous line, then the same line number (as before the blank lines were inserted) will still be output in the error message. The blank lines thus appear to have been removed before Stan attempts to compile or run the program. This makes it harder to identify the problematic line in the editor. It can be useful to have blank lines in Stan code as a way of stylistically clarifying program structure (well, at least I think so).

Lines containing comments are counted correctly.

Reproducible Steps:

Syntax error
Small reproducible example (in R) for syntax error: The two models in this example (wrong_1 and wrong_2) lack a type declaration for sigma, which will be flagged by rstan. The second model contains two blank lines between the program blocks. However, rstan lists the same line number in the error output in these two cases:

library(rstan)

wrong_1 = "
data {
  vector[4] y;
}
parameters {
    real mu;
    <lower=0> sigma;    // Missing type declaration
}
"

wrong_2 = "
data {
  vector[4] y;
}


parameters {
    real mu;
    <lower=0> sigma;   // Missing type declaration
}
"

stan(model_code=wrong_1, data=list(y=c(3,2,3,2)), chains=1, iter=100)
stan(model_code=wrong_2, data=list(y=c(3,2,3,2)), chains=1, iter=100)

Runtime error
The same problem occurs for runtime errors. Small reproducible example for R below. The two models are identical, except that in correct_2 one blank line has been inserted in the data block before the declaration of y. In this case the syntax is correct (type is declared for sigma) but the call is wrong (the variable y was not provided in the data list). Running the two examples in rstan results in identical error messages pointing to line number 2 (for correct_2 it should have been line number 3)

library(rstan)

correct_1 = "
data {
  vector[4] y;
}
parameters {
    real mu;
    real<lower=0> sigma;
}
"

correct_2 = "
data {

  vector[4] y;
}
parameters {
    real mu;
    real<lower=0> sigma;
}
"

stan(model_code=correct_1, data=list(), chains=1, iter=100)
stan(model_code=correct_2, data=list(), chains=1, iter=100)
Current Output:

Syntax error
Both wrong_1 and wrong_2 results in the output below, where the line is indicated to be number 6.

Error in stanc(file = file, model_code = model_code, model_name = model_name,  : 
  0

Syntax error in 'string', line 6, column 4 to column 5, parsing error:
Only top-level variable declarations allowed in data and parameters blocks.

Runtime error
Both the calls to correct_1 and correct_2 results in the output below, where the line is indicated to be number 2.

Error in new_CppObject_xp(fields$.module, fields$.pointer, ...) : 
  Exception: variable does not exist; processing stage=data initialization; variable name=y; base type=double (in 'anon_model', line 2, column 2 to column 14)
failed to create the sampler; sampling not done
Expected Output:

Syntax error
For wrong_2 I expected the line number to match the erroneous line in the original Stan code (so line number 8 instead of 6, due to the two blank lines)

Error in stanc(file = file, model_code = model_code, model_name = model_name,  : 
  0

Syntax error in 'string', line 6, column 4 to column 5, parsing error:

Only top-level variable declarations allowed in data and parameters blocks.

Runtime error
For correct_2 I expected the line number to match the erroneous line in the original Stan code (so line number 3 instead of 2, due to the extra blank line).

Error in new_CppObject_xp(fields$.module, fields$.pointer, ...) : 
  Exception: variable does not exist; processing stage=data initialization; variable name=y; base type=double (in 'anon_model', line 3, column 2 to column 14)
failed to create the sampler; sampling not done
RStan Version:

2.26.13

R Version:

R version 4.2.1 (2022-06-23)
Run from within RStudio RStudio 2022.07.2 build 576

Operating System:

ProductName: macOS
ProductVersion: 12.6
BuildVersion: 21G115

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the syntax and runtime cases from the issue in rstan, focusing on the stanc error path and the runtime error reporting shown in the examples. Done means blank lines in Stan code are preserved in reported line numbers for both error types, while comment lines continue to count correctly.

Written by the indexing model from the issue text.

Assessment

Tech stack
r
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.