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

fix: compiler crash on primitive type checking against opaque types #3712

Merged
merged 4 commits into from Jan 30, 2024

Conversation

tanishiking
Copy link
Member

fix #3700

It is caused because in isPrimitiveValueType against opaque type, t.widenDealias.typeSymbol isn't ClassSymbol but a TypeSymbol for cmark_event_type. This commit adds an additional check that is the type symbol ClassSymbol before casting.

Comment on lines 5 to 13
opaque type cmark_event_type = CUnsignedInt
object cmark_event_type:
inline def define(inline a: Long): cmark_event_type = a.toUInt
val CMARK_EVENT_NONE = define(0)

@main def hello(): Unit =
val evtype = cmark_event_type.CMARK_EVENT_NONE
stdio.printf(c"bla: %s, hello: %d", evtype)

Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of expensive scripted tests, let's define it as quick unit test in nscplugin, for example in this test

class NIRCompilerTest3 {
inline def nativeCompilation(source: String): Unit = {

We only need to ensure that is compiles, we don't require linking

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks! done in 1a3dbcc
(I added clib.cp to Test/ nscplugin / javaOptions since I couldn't find a way to reproduce the compilation crash without using clib.stdio 🤔

Copy link
Contributor

Choose a reason for hiding this comment

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

I think you might have problems with that, becouse there are 2 definitions of printf. One is defined as extension here:

implicit class StdioHelpers(val _stdio: libc.stdio.type) extends AnyVal {
def printf(format: CString, args: CVarArg*): CInt =
Zone { implicit z => stdio.vprintf(format, toCVarArgList(args.toSeq)) }
it's a legacy support from before introduction of better varargs in extern
The second one is standard extern binding
@blocking def fprintf(stream: Ptr[FILE], format: CString, vargs: Any*): CInt =
extern

I belive it could have been coused by the clash of the two. It would be great if we could get rid of the dependency. I'd like to remove legacy version in the future.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks @WojciechMazur!
I'm not sure the duplication of printf is the problem, I could reproduce it without clib 74fbddd

It seems like we hit the ensureUnboxed -> isPrimitiveType when the argument is for extern method's varargs (e.g. printf's vargs)

if (isVarArg) ensureUnboxed(rawValue, paramTpe.finalResultType)
else rawValue

With something like def test(t: Any*) = extern, and if I pass the opaque typed value to the test method, we could break the compiler 👍

Adding clib classpath to nscplugin in test scope
I couldn't find a way to reproduce the compiler crash without using
clib
We could reproduce the issue of compiler crash on opaque-types without clib
@WojciechMazur WojciechMazur merged commit d3a0956 into scala-native:main Jan 30, 2024
58 of 62 checks passed
@armanbilge armanbilge added the backport candidate PR which might be backported into previous major release of SN label Mar 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport candidate PR which might be backported into previous major release of SN
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Compiler crash with opaque types, inline, varargs, unsigned
3 participants