Skip to content

Conversation

danakj
Copy link
Contributor

@danakj danakj commented Jun 17, 2025

Remove use of i32/bool when a builtin type or test-define class type can work. Make Sub user-defines in a test that is testing builtin functions and not trying to test the prelude, in the same way that it defines its own Negate. Reduce use of the + operator when it isn't contributing to the test's coverage, since the + operator needs the full prelude. Remove use of Core.Print when it's not required for the test.

Move deduce_nested_facet_value.carbon to its own file since it uses TypeAnd, and the rest of deduce.carbon does not, but uses i32. This means they can each use a different min-prelude.

@danakj danakj requested a review from jonmeow June 17, 2025 16:30
@danakj
Copy link
Contributor Author

danakj commented Jun 17, 2025

This is a prereq for #5676 #5680 #5681 and #5682. They will be rebased over it.

Copy link
Contributor

@jonmeow jonmeow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for splitting this out, sorry for being a pain about it.

import library "types";

fn Negate(n: i32) -> i32 = "int.snegate";
fn Negate(a: IntLiteral()) -> IntLiteral() = "int.snegate";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fn Negate(a: IntLiteral()) -> IntLiteral() = "int.snegate";
fn Negate(a: Int(32)) -> Int(32) = "int.snegate";

Can this more precisely match the prior i32, or does it specifically need to be IntLiteral to avoid a conversion?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The old thing was an oversight, we only need to negate literals here. This is more obvious in snegate, where using Int(32) fails with the inputs, since they are larger values. Using Int(32) here instead adds a dependency that the test doesn't need.

library "[[@TEST_NAME]]";

class B(N:! i32);
class I {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, small thing. Can you choose something like class A or class Other? I in particular I automatically think "interface".

I was already thinking about this looking at fail_generic_method.carbon, but this file uses the pattern multiple times. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

// TIP: To dump output, run:
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/function/generic/deduce_nested_facet_value.carbon

// --- deduce_nested_facet_value.carbon
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW -- to the extent that this is about performance of prelude parsing, splitting out tests shouldn't be expected to make tests faster since it's a per-file cost (actually, it'll probably marginally slow down due to the min_prelude + full prelude use instead of just full prelude).

I'd suggest leaving this where it belongs logically, and choose min_prelude based on what works, rather than using min_prelude as a primary rationale for splits.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no min_prelude that works for both, so we'd end up with a full prelude - that's why it's moving. The deduce file needs int.carbon as a prelude and it doesn't have TypeAnd. This test needs facet_types.carbon, and it doesn't have i32.

@@ -20,7 +20,7 @@ interface J {
}

impl () as J where .U = i32 {
fn F[self: Self](u: i32) -> i32 { return u + 2; }
fn F[self: Self](u: i32) -> i32 { return u; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this possibly validating that it could make use of the + operator on i32? (i.e., doing some minor impls validation)

Given min_prelude, maybe switch to -u, which still requires an operator lookup?

Suggested change
fn F[self: Self](u: i32) -> i32 { return u; }
fn F[self: Self](u: i32) -> i32 { return -u; }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it was, this was testing the ability to call the associated function. But returning something different than it received is fine, sure.

@@ -47,7 +47,7 @@ interface J {
class D { }

impl D as J where .U = i32 {
fn F(u: i32) -> i32 { return u + 3; }
fn F(u: i32) -> i32 { return u; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fn F(u: i32) -> i32 { return u; }
fn F(u: i32) -> i32 { return -u; }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -66,16 +66,20 @@ interface J {
class E {
extend impl as J where .U = i32 {
fn F(u: i32) -> i32 {
return u + 1;
return u;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return u;
return -u;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}
fn G[self: Self](v: i32) -> i32 {
return v + 2;
return v;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return v;
return -v;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

impl ImplsSomeInterface as SomeInterface(i32) {
fn F(x: i32) -> i32 {
return x + x;
return x;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return x;
return -x;

(see other comment about trying to use impls of i32)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment on lines 40 to 42
// TODO: This should call the `F` from lib1 and the `F` from lib2, where one
// returns 1 and the other returns 2. Currently it calls the same function
// twice, both returning 1.
Copy link
Contributor

@jonmeow jonmeow Jun 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll just note, in theory the previous "return 3" comment could be compiled and verified. Now that's not really the case. But the "returns 1" also can't be validated AFAICT, I don't think it's really obvious from the code.

Personally, I'd suggest something more like:

Suggested change
// TODO: This should call the `F` from lib1 and the `F` from lib2, where one
// returns 1 and the other returns 2. Currently it calls the same function
// twice, both returning 1.
// TODO: This should call the `F` from lib1 and the `F` from lib2. Currently
// it calls the same function in both (hashes match for both instances of
// `call i32 @_CF.Main.<hash>`).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it's only observable/validated by reading the lower output. Thanks, done.

danakj and others added 2 commits June 17, 2025 13:48
Co-authored-by: Jon Ross-Perkins <[email protected]>
@danakj danakj enabled auto-merge June 17, 2025 17:50
@danakj danakj added this pull request to the merge queue Jun 17, 2025
Merged via the queue into carbon-language:trunk with commit 19d59b2 Jun 17, 2025
8 checks passed
@danakj danakj deleted the reduce-prelude branch June 17, 2025 18:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants