Interaction of macros and comments with ; in REPL

I was quite surprised to see the following in REPL:

julia> macro g(ex)
       "Some value"
       end
@g (macro with 1 method)

julia> @g 1
"Some value"

julia> @g 1;

julia> @g 1 # comment
"Some value"

julia> @g 1; # comment

julia> @g 1; # comment;

julia> @g 1; # comment; another comment
"Some value"

julia> @g 1; # comment; another comment;

julia> @g(1); # comment

julia> @g(1); # comment; another comment
"Some value"

julia> @g(1); # comment; another comment;

julia> @g(1) # comment
"Some value"

julia> @g(1) # comment;

julia>

It means that the behavior of REPL is sensitive to the fact if we used ; in a comment or not (and the expectation is that after # all text should be ignored unless).

This is not a purely artificial question. A common case when this might happen is when we use @time or @btime and log benchmark results. If you also have comments in the line that contains macro the benchmark results volume can unexpectedly explode. Here is a simple example:

julia> @time rand(1000, 1000); # all OK
  0.028492 seconds (5.33 k allocations: 7.906 MiB)

julia> @time rand(1000, 1000); # all OK; but now it is not
  0.002986 seconds (6 allocations: 7.630 MiB)
1000×1000 Array{Float64,2}:
 0.0267641  0.658582   0.848755   0.317831  0.688003   0.358419   0.451134    0.23211    0.959732    …  0.900364   0.510592   0.548442    0.278663    0.62676    0.637126   0.230665    0.21124
 0.573036   0.343188   0.826076   0.730182  0.136591   0.195317   0.493071    0.949521   0.423192       0.305033   0.876593   0.244199    0.386221    0.969509   0.319844   0.335223    0.829834
 0.856271   0.400261   0.698654   0.841522  0.625836   0.860484   0.200021    0.0116017  0.0875489      0.0179683  0.0320464  0.63124     0.00268192  0.873807   0.839423   0.635909    0.555291
 0.852382   0.960672   0.153343   0.293958  0.15946    0.820972   0.784847    0.390328   0.469626       0.776801   0.397079   0.80543     0.275       0.14089    0.47268    0.0156535   0.85925
 0.25104    0.20491    0.428919   0.857973  0.753786   0.0301861  0.395199    0.369403   0.47481        0.047398   0.799893   0.652982    0.696275    0.492488   0.448998   0.0513526   0.338989
 0.393509   0.607272   0.334652   0.46158   0.274222   0.804766   0.970835    0.71435    0.359019    …  0.864391   0.438048   0.808643    0.721354    0.688137   0.465822   0.00739675  0.824901
 0.889251   0.703329   0.759935   0.709762  0.565584   0.628499   0.00271273  0.51494    0.897257       0.161289   0.641785   0.56721     0.594981    0.822345   0.166982   0.0249376   0.892944
 0.709555   0.411339   0.996607   0.731638  0.590527   0.435896   0.232339    0.666103   0.490926       0.072736   0.770996   0.647315    0.392009    0.616822   0.404794   0.712745    0.735653
 0.862593   0.402576   0.692238   0.524676  0.585742   0.138828   0.66776     0.418467   0.818053       0.395829   0.303623   0.363488    0.813408    0.125701   0.494382   0.455527    0.478308
 0.872455   0.920469   0.392234   0.29627   0.165056   0.151531   0.428493    0.225258   0.72585        0.464203   0.674847   0.976506    0.487631    0.676531   0.0794573  0.9357      0.771471
 0.157901   0.25171    0.35577    0.987172  0.386309   0.049215   0.222573    0.294111   0.761429    …  0.141349   0.234049   0.76048     0.547256    0.167864   0.711885   0.152854    0.658445
 0.589158   0.0245295  0.918736   0.581051  0.142954   0.972766   0.53413     0.140499   0.294059       0.0350834  0.563872   0.724622    0.982327    0.490663   0.672911   0.803811    0.337851
 0.423949   0.0140113  0.415359   0.120011  0.612815   0.383749   0.341847    0.297124   0.755884       0.517018   0.105703   0.928132    0.692935    0.711834   0.980814   0.178398    0.145736
 0.0600562  0.560431   0.0226274  0.793114  0.815692   0.599694   0.284682    0.675083   0.605591       0.0634941  0.716539   0.877788    0.699433    0.670576   0.257364   0.715528    0.72092
 0.424772   0.443975   0.0783245  0.32872   0.436885   0.864198   0.0958389   0.527964   0.122099       0.2896     0.854291   0.286997    0.869232    0.0159727  0.970274   0.757292    0.709655
 0.137642   0.552306   0.340412   0.870594  0.8005     0.58662    0.704773    0.205258   0.929446    …  0.149419   0.889036   0.750801    0.832122    0.158465   0.524737   0.836708    0.856735
 0.934958   0.775266   0.193415   0.139921  0.77649    0.091841   0.442313    0.379873   0.210962       0.561086   0.340543   0.573748    0.252492    0.41789    0.756878   0.475725    0.619045
 0.638356   0.736071   0.195017   0.686435  0.297163   0.826745   0.292213    0.676927   0.332895       0.157774   0.296899   0.565132    0.142281    0.268541   0.997655   0.262132    0.0477603
 0.16549    0.359255   0.1783     0.3693    0.0963325  0.841711   0.996923    0.297843   0.267039       0.863845   0.40365    0.917608    0.653329    0.825724   0.316271   0.88585     0.717097
 0.532575   0.38428    0.846096   0.303175  0.36408    0.626931   0.915248    0.782076   0.831681       0.827864   0.168862   0.313873    0.966666    0.755359   0.519957   0.512541    0.199088
 0.0359976  0.064213   0.491815   0.613698  0.346771   0.794803   0.451207    0.613652   0.862894    …  0.82131    0.484946   0.44644     0.0967432   0.273457   0.03746    0.665998    0.138288
 0.68198    0.0610806  0.675673   0.972068  0.10548    0.828372   0.342581    0.180128   0.644963       0.249285   0.180819   0.792028    0.340836    0.892033   0.295675   0.0940658   0.267208
 0.668541   0.401939   0.79358    0.746644  0.927863   0.0135706  0.394751    0.727533   0.113493       0.345811   0.314602   0.0864143   0.953271    0.236522   0.258167   0.909776    0.925962
 0.0922822  0.884553   0.774214   0.458847  0.756019   0.500089   0.678584    0.192439   0.631339       0.096009   0.310327   0.393652    0.538764    0.930267   0.757297   0.779954    0.244525
 0.454251   0.0322761  0.465341   0.895469  0.0625092  0.532418   0.583566    0.821984   0.896099       0.48433    0.843093   0.526709    0.61162     0.847836   0.1635     0.392544    0.918549
 ⋮                                                     ⋮                                             ⋱                                    ⋮
 0.727286   0.106576   0.763713   0.986037  0.167134   0.434408   0.885278    0.234356   0.659182       0.40644    0.0681324  0.815116    0.648259    0.674225   0.995851   0.948789    0.808887
 0.38977    0.943557   0.19886    0.617703  0.159148   0.412448   0.219963    0.805158   0.588202       0.196682   0.072714   0.426219    0.381959    0.663514   0.466398   0.0162166   0.802478
 0.116801   0.742059   0.401393   0.528056  0.0541768  0.256147   0.927635    0.719834   0.525814       0.592159   0.523849   0.571465    0.0326836   0.306318   0.839485   0.183633    0.0165282
 0.718953   0.547046   0.821147   0.18311   0.915053   0.483597   0.10538     0.517791   0.749066       0.928134   0.284119   0.425519    0.390993    0.85582    0.0638284  0.775352    0.754087
 0.71819    0.809737   0.0654892  0.913811  0.757074   0.164386   0.528953    0.994725   0.858255    …  0.846287   0.179156   0.687095    0.563957    0.511393   0.596384   0.583887    0.841501
 0.585481   0.416224   0.934921   0.929297  0.303899   0.897434   0.240078    0.782317   0.910373       0.621187   0.703555   0.0383639   0.204674    0.975596   0.900892   0.767434    0.363289
 0.39524    0.131929   0.992866   0.068157  0.507883   0.654878   0.610657    0.577879   0.00957053     0.345147   0.226008   0.338321    0.935626    0.60693    0.859111   0.383681    0.316735
 0.480617   0.0918676  0.18141    0.909645  0.608337   0.670476   0.682836    0.824339   0.805192       0.499084   0.661822   0.351314    0.60607     0.461762   0.381912   0.553977    0.871948
 0.787662   0.591027   0.472551   0.222001  0.0655733  0.116074   0.724356    0.972063   0.512234       0.677201   0.89766    0.273225    0.268796    0.571354   0.291354   0.304498    0.503353
 0.0627622  0.505999   0.912835   0.888306  0.58696    0.446313   0.927461    0.0156876  0.408947    …  0.740654   0.638439   0.680929    0.364069    0.791384   0.190683   0.596154    0.429361
 0.948272   0.397318   0.408595   0.589306  0.524703   0.844776   0.735696    0.218957   0.963029       0.635286   0.218833   0.952485    0.263801    0.0543318  0.769076   0.109304    0.722779
 0.27112    0.975753   0.520775   0.495836  0.920183   0.651118   0.334148    0.401216   0.270204       0.905123   0.752772   0.278488    0.648417    0.268399   0.097165   0.314593    0.634961
 0.767498   0.14352    0.0404379  0.617141  0.679762   0.83504    0.945535    0.620964   0.997305       0.633123   0.347835   0.0712735   0.532536    0.579622   0.247206   0.563325    0.698014
 0.482833   0.711086   0.0395703  0.896233  0.296759   0.906968   0.325958    0.380955   0.548374       0.605732   0.296735   0.00830663  0.215665    0.693552   0.699519   0.700885    0.939767
 0.789204   0.858355   0.237738   0.388806  0.414321   0.42554    0.713096    0.960728   0.8659      …  0.33489    0.100444   0.26247     0.910938    0.273536   0.686318   0.259117    0.486116
 0.880129   0.640954   0.975515   0.864191  0.0699951  0.0465749  0.383876    0.255432   0.592298       0.161853   0.427513   0.45963     0.342952    0.999757   0.0772689  0.255583    0.313283
 0.600257   0.380218   0.169106   0.449569  0.754442   0.896011   0.623048    0.649595   0.275728       0.628487   0.907642   0.0955009   0.178071    0.142105   0.456238   0.782727    0.613741
 0.611542   0.380539   0.821424   0.441122  0.0553052  0.332895   0.477381    0.479204   0.422758       0.489139   0.0348038  0.26374     0.138535    0.972803   0.74023    0.666659    0.457319
 0.366427   0.597743   0.408222   0.543907  0.812792   0.218637   0.80632     0.160942   0.694943       0.36444    0.276327   0.977391    0.786791    0.356966   0.492407   0.712287    0.861778
 0.70826    0.739991   0.0771072  0.851083  0.777034   0.943255   0.566574    0.323754   0.253069    …  0.363804   0.695912   0.0336533   0.518607    0.101784   0.0545115  0.351019    0.663125
 0.985455   0.551387   0.364168   0.932284  0.0588912  0.462793   0.289022    0.744923   0.486689       0.381584   0.621326   0.363666    0.203018    0.382784   0.74265    0.265523    0.620865
 0.863684   0.907237   0.48292    0.453384  0.692192   0.148738   0.258507    0.769743   0.769718       0.386317   0.0224697  0.139299    0.173425    0.102454   0.636354   0.686261    0.568425
 0.41339    0.556762   0.778314   0.494947  0.01019    0.181109   0.741261    0.662134   0.985842       0.323563   0.440997   0.414983    0.945543    0.732891   0.407309   0.298331    0.594593
 0.209233   0.502239   0.442412   0.831158  0.540056   0.532196   0.0342574   0.435019   0.83454        0.799601   0.791672   0.483698    0.011429    0.760036   0.163032   0.309858    0.945666

julia>

See e.g. https://github.com/JuliaLang/julia/issues/28743. I believe it is a pretty heuristic function to determine if output should be displayed or not, see https://github.com/JuliaLang/julia/blob/d9b7db9354f71c4942453c1eebca562b7404789d/stdlib/REPL/src/REPL.jl#L1064-L1113

3 Likes

Can you show an example of this happening for @btime?

Here you go:

julia> @btime 1 # fsd;
  0.001 ns (0 allocations: 0 bytes)

julia> @btime 1 # fsd
  0.001 ns (0 allocations: 0 bytes)
1

It seemed to me you implied that this would somehow affect the timing reported by @btime (which I why I though you specifically used the timing macros as examples).

No - I just spotted it when doing analysis of logs with timings.