Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First stab at Err#typeError in api #8168

Merged
merged 13 commits into from Apr 25, 2024
Merged

First stab at Err#typeError in api #8168

merged 13 commits into from Apr 25, 2024

Conversation

enebo
Copy link
Member

@enebo enebo commented Mar 25, 2024

First stab at Err#typeError in api

The basic idea is that there is a series of static methods for raising errors in a single class. They will be:

  1. documented
  2. consistent
  3. completely emcompassing (no need to new RaiseError(...) for standard error types

The first one I hit was TypeError and specifically call sites which were manually building up a normal string. typeError
will:

  1. determine proper object (display if already a type but otherwise getMetaClass). Existing code was doing a) nothing (obj) b) obj.getType() c) obj.getMetaclass(). Some of this has to be wrong.
  2. Uses str() to build up properly encoded string (which was not done for most cases)
  3. Reduces size of code at the callSite to a smaller (I think) more readable snippet
  4. explicitly throws

This last point is probably the most contentious as Java type system gets confused by unconditional throwing methods and if this happens to be last statement in places it will argue a return is missing. My belief is this is an ok tradeoff. We have had dozens of errors in this project where someone made the error but did not actually throw it. The second point is we do not tend to ever make an exception and not immediately throw.

If this is good then I will convertall other TypeError callers from Ruby and deprecate those methods. Additionally, I think we should backport this to 9.4 (and possibly 9.3). The benefit is we can update native extensions at some future point to use these and then force a version requirement at a point forward. This should give us confidence that we can remove the original methods in a later release.

@enebo enebo added this to the JRuby 9.5.0.0 milestone Mar 25, 2024
enebo added 11 commits March 25, 2024 14:59
I realized having throw be in the error methods had too many tradeoffs.
Beyond Java not being able to realize those methods unconditionally
throws (making some return null;  // not reached lines).  It also lacks
the ablity to compose:

```java
withException(context, typeError(context, a, "Array"), e);
```

Doing with this typeError raising is not possible and you would have
to either not have composable methods or make a combinatorial set of
methods which do the same thing.  This commit also has some oddities
for the new castTo but I think there are ok.

The first one is castTo is effectively replacing:

```java
if (a instanceof RubyArray) throw TypeError(context, a, "Array");
RubyArray b = (RubyArray) a;
```

with:

```java
RubyArray b = castToArray(context, a);
```

Looks nice but:
```java
RubyArray b = castToArray(getRuntime.getCurrentContext(), a);
```

Notice we pay price of looking up context when we may not do that in
the original snippet?  I think worst-case you can still do the original
snippet but in normal cases we continue to work on propagating ThreadContext
everywhere and this again will be no cost.

The second oddity is:

```java
if (a instanceof RubyArray) throw TypeError(context, a, str(runtime, ...));
RubyArray b = (RubyArray) a;
```

This form is accepted for castTo as well:

```java
RubyArray b = castToArray(context, a, str(runtime, ...));
```

The problem here is that str is executed every time.  So this form of
castToXXX has a warning in javadocs but it is a potential foot gun.  There
are enough simple string forms where I think this is worth having but it
is undesirable.

I have been considering whether we could pass a lambda for the String part
but that makes me think it would generate a lot more code.

One last observation is that Java instanceof pattern variables encourages
a new idiom of writing code and I am unsure how I feel about it.  Consider
this snippet:

```java
         void put(ThreadContext context, AbstractMemory ptr, long offset, IRubyObject value) {
            if (!(value instanceof Struct)) throw typeError(context, "expected a struct");
            Struct s = (Struct) value;
            byte[] tmp = new byte[Struct.getStructSize(context.runtime, s)];
            s.getMemoryIO().get(0, tmp, 0, tmp.length);
            ptr.getMemoryIO().put(offset, tmp, 0, tmp.length);
         }
```

With pattern var:

```java
         void put(ThreadContext context, AbstractMemory ptr, long offset, IRubyObject value) {
            if (value instanceof Struct s) {
                byte[] tmp = new byte[Struct.getStructSize(context.runtime, s)];
                s.getMemoryIO().get(0, tmp, 0, tmp.length);
                ptr.getMemoryIO().put(offset, tmp, 0, tmp.length);
                return;
            }

            throw typeError(context, "expected a struct");
         }
```

Arguably they are both reasonable ways to write but the pattern var gives
the sense that we are performing a safe cast in a way the compiler will
catch.  The other way is rare to get wrong and it is less weird to see
an error check before the normal path vs the error path appearing to be the
non-special path.  At the end I realized this pattern var will transform
idiomatic Java code to be written using this newer syntactical feature.
We may as well embrace it (sans nice castToXXX methods to hide our casting).

This commit also does a fair amount of style tweaks.  A big part of this new
API work is to see how it "feels".  That lead to some refactoring to help
learn whether the API feels good to me.
@enebo enebo merged commit 4cdf181 into jruby:9.5-dev Apr 25, 2024
34 of 60 checks passed
@enebo enebo deleted the type_error branch April 25, 2024 17:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant