Skip to content

Conversation

@lkdvos
Copy link
Contributor

@lkdvos lkdvos commented Nov 11, 2025

This adds the convenience @testinferred, which behaves like @constinferred in the sense that it adds a test for inference, but doesn't try to capture constant propagation, since the way this is done in @constinferred is fundamentally incompatible with adding this to a non-toplevel expression in julia 1.12+:

julia> function do_test()
           @constinferred f(x) == y
       end
ERROR: syntax: World age increment not at top level
Stacktrace:
 [1] top-level scope
   @ REPL[4]:1

julia> function do_test()
           @testinferred f(x) == y
       end
do_test (generic function with 1 method)

I tried to minimize the amount of depending on internal functionality, but some of this is a bit black magic to me. Fixes #10

It is useful to check for type stability.
This is similar to `Test.@inferred`, but instead of throwing an error, a `@test` is added.

Optionally, `AllowedType` relaxes the test, by making it pass when either the type of `f(x)` matches the inferred type modulo `AllowedType`, or when the return type is a subtype of `AllowedType`.
Copy link

Choose a reason for hiding this comment

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

It would be great to show an example here of the usage of AllowedType

@codecov
Copy link

codecov bot commented Nov 20, 2025

Codecov Report

❌ Patch coverage is 89.26554% with 19 lines in your changes missing coverage. Please review.
✅ Project coverage is 77.51%. Comparing base (44a7256) to head (23ce71b).
⚠️ Report is 10 commits behind head on master.

Files with missing lines Patch % Lines
src/testinferred.jl 89.26% 19 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master      #11      +/-   ##
==========================================
+ Coverage   75.00%   77.51%   +2.51%     
==========================================
  Files           4        4              
  Lines         248      338      +90     
==========================================
+ Hits          186      262      +76     
- Misses         62       76      +14     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Jutho
Copy link
Owner

Jutho commented Nov 20, 2025

Ok, I ended up completely redesigning it, such that more code is shared, and it also addresses #9 . Probably needs a bit of cleanup in assigning the documentation to the different macros, and maybe adding a few more tests, but I think the main functionality is working. But maybe I'll await some comments?

Copy link
Contributor Author

@lkdvos lkdvos left a comment

Choose a reason for hiding this comment

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

I haven't looked at the implementation in detail, but overall this looks like a nice improvement. I'm not sure how useful it is to have both a constinferred keyword to @testinferred, and a macro. I think I quite like the two macros instead, but this is obviously just subjective

@Jutho
Copy link
Owner

Jutho commented Nov 24, 2025

It started with issue #9 that wanted the _broken macro to be selectable via a keyword. If it will be useful for constprop is indeed not clear, especially since broken can have a dynamic value whereas for constprop only a literal true or false is accepted.

The two different macros remain available and for @testinferred the default is constprop=false, so the original interface remains. Whether @testinferred ... constprop=true will ever be useful remains to be seen; maybe in a usecase where you want to quickly switch between testing with or without constant propagation.

Copy link
Contributor Author

@lkdvos lkdvos left a comment

Choose a reason for hiding this comment

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

I definitely see the use case for the broken as a keyword, since that can also dynamically be changed, it's mostly the constprop that I am less sure about. Ultimately, this does not hurt me though, so happy to get this merged and released as is!

@Jutho
Copy link
Owner

Jutho commented Nov 28, 2025

Ok, I might just try to increase the coverage a bit further, so that the keyword approach is also tested and then have this merged.

@Jutho Jutho merged commit 5bdb746 into Jutho:master Nov 29, 2025
20 checks passed
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.

@constinferred doesn't work from within a function

3 participants