Skip to content

Bug in getS3method?

1 message · GILLIBERT, Andre

#
Hello,
# the following code shows an inconsistency on R

# tested on 4.5.1 (2025-06-13 ucrt) (x86_64-w64-mingw32/x64)

local({
    coef.lm=function(x, ...) {cat("new coef.lm function\n")}
    attach(environment(), name="test.env")
})

mod = lm(I(1:3)~1)
getS3method("coef", "lm")(mod) # uses the new coef.lm function
coef(mod) # uses coef.default of package stats
detach("test.env")


I also tested the old versions of R I kept on my computer.

R 4.0.3 : same behaviour as R 4.5.1

R 3.6.3 : both lines of code call the new coef.lm function


When looking at old R release NEWS for R 4.0.0, I got a possible explanation:
Therefore, it looks like R_LookupMethod() was updated in src/main/objects.c but not getS3method() in src/library/utils/R/objects.R

I found the commit that seems to have caused the change in behaviour:

https://github.com/wch/r-source/commit/753ceaea9854d0b4d7a363041f0176ecc1b036fb<https://astragale.chu-rouen.fr/https/chu-rouen/gw-chu-rouen-03/github.com/wch/r-source/commit/753ceaea9854d0b4d7a363041f0176ecc1b036fb>


<https://astragale.chu-rouen.fr/https/chu-rouen/gw-chu-rouen-03/github.com/wch/r-source/commit/753ceaea9854d0b4d7a363041f0176ecc1b036fb>

Actually, when launching R 4.0.3 with the _R_S3_METHOD_LOOKUP_BASEENV_AFTER_GLOBALENV_=FALSE system environment variable set, I can reproduce the old R 3.6.3 behaviour, but not in R 4.5.1, because this environment variable was completely removed:

https://github.com/wch/r-source/commit/58a2da9d8e506c33f33c16a3a87584be0ab013d6<https://astragale.chu-rouen.fr/https/chu-rouen/gw-chu-rouen-03/github.com/wch/r-source/commit/58a2da9d8e506c33f33c16a3a87584be0ab013d6>


Unless I miss something, getS3method() should be updated to be consistent with the actual method lookup mechanism.


--

Sincerely

Andr? GILLIBERT