Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[refactor] mirror no_redef_warning from MRI
  • Loading branch information
kares committed May 26, 2026
commit 1567b927fcc3e55524b44024b24ecf5f03f9197e
6 changes: 3 additions & 3 deletions core/src/main/java/org/jruby/RubyModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -5702,7 +5702,7 @@ public synchronized <T extends RubyModule> T defineAlias(ThreadContext context,

DynamicMethod method = getMethods().get(name);
if (method != null && entry.method.getRealMethod() != method.getRealMethod() && !method.isUndefined()) {
if (method.getRealMethod().getAliasCount() == 0) {
if (method.getRealMethod().warnsOnRedefinition()) {
warning(context, "method redefined; discarding old " + name);

if (method instanceof PositionAware posAware) {
Expand All @@ -5712,8 +5712,8 @@ public synchronized <T extends RubyModule> T defineAlias(ThreadContext context,
}

if (name.equals(oldName)) {
// MRI treats self-aliasing as an intentional alias, which suppresses redefinition warning
entry.method.getRealMethod().adjustAliasCount(1);
// MRI treats self-aliasing as an intentional alias and suppresses redefinition warning
entry.method.getRealMethod().setNoRedefinitionWarning();
} else {
checkAliasFrameAccesses(context, oldName, name, entry.method);
putAlias(context, name, entry, oldName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public class AliasMethod extends DynamicMethod {
*/
public AliasMethod(RubyModule implementationClass, CacheEntry entry, String newName, String oldName) {
super(implementationClass, determineVisibility(newName, entry.method.getVisibility()), oldName);
entry.method.getRealMethod().adjustAliasCount(1);
entry.method.getRealMethod().setAliased(true);

this.entry = entry;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,10 @@ public abstract class DynamicMethod {
protected final String name;
/** An arbitrarily-typed "method handle" for use by compilers and call sites */
protected Object handle;
/** How many times has this method been aliased. */
protected int aliasCount;
/** Whether this method has been aliased */
private boolean aliased;
/** Whether this method has been marked to suppress redefinition warnings */
private boolean noRedefinitionWarning;

private static final int BUILTIN_FLAG = 0b1;
private static final int NOTIMPL_FLAG = 0b10;
Expand Down Expand Up @@ -678,12 +680,20 @@ protected DynamicMethod(RubyModule implementationClass, Visibility visibility) {
this(implementationClass, visibility, "(anonymous)");
}

public void adjustAliasCount(int delta) {
this.aliasCount += delta;
void setAliased(boolean aliased) {
this.aliased = aliased;
}

public int getAliasCount() {
return this.aliasCount;
public boolean isAliased() {
return this.aliased;
}

public void setNoRedefinitionWarning() {
this.noRedefinitionWarning = true;
}

public boolean warnsOnRedefinition() {
return !this.aliased && !this.noRedefinitionWarning;
}

/**
Expand Down
Loading