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

Disallow readonly instance fields in InlineArray structs #90262

Closed
wants to merge 1 commit into from

Conversation

VSadov
Copy link
Member

@VSadov VSadov commented Aug 9, 2023

Making the element field of an InlineArray struct should be forbidden as it currently has no meaning.
The restriction is required at very least to be able to attach some meaning to this scenario in the future.

UNDONE:

@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Aug 9, 2023
@ghost ghost assigned VSadov Aug 9, 2023
@VSadov
Copy link
Member Author

VSadov commented Aug 9, 2023

CC: @lambdageek - similar change may be needed in Mono.

@VSadov VSadov added area-TypeSystem-coreclr and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Aug 9, 2023
@lambdageek
Copy link
Member

@VSadov thanks for the heads up.

Try adding this

 							break;
 						}
 					}
+					if ((ftype->attrs & FIELD_ATTRIBUTE_INIT_ONLY) != 0) {
+						mono_class_set_type_load_failure (klass, "InlineArrayAttribute cannot be applied to a type with readonly instance fields.");
+						break;
+					}
 				}
 
 				/* FIXME (LAMESPEC): should we also change the min alignment according to pack? */

Right here:

}
}
}
/* FIXME (LAMESPEC): should we also change the min alignment according to pack? */

/cc @kotlarmilos

@jkotas
Copy link
Member

jkotas commented Aug 9, 2023

I know we have discussed this over email. I agree that this makes sense to block in Roslyn, for now at least. Looking at this again, I am not sure about blocking it in the runtime.

There are many other type shapes that the runtime type loader allows and Roslyn disallows. It feels random to block this particular one. What should be our policy where the type loader matches the same type restrictions as Roslyn vs. where the type loader is less strict than Roslyn?

So far, our general type loader policy has been that the type loader only enforces invariants that are important for making the runtime work reliably. Readonly InlineArray does not harm the runtime in any way, so why block it?

@jkotas
Copy link
Member

jkotas commented Aug 9, 2023

cc @jaredpar @AlekseyTs

@VSadov
Copy link
Member Author

VSadov commented Aug 9, 2023

Readonly InlineArray does not harm the runtime in any way, so why block it?

I can be easily convinced either way. My initial suggestion was also to do nothing on the runtime side, but the last discussion ended with "we should forbid this to not infringe on possible future use".

Since indexing and other accessors are done in Roslyn, forbidding only on the Roslyn side might be sufficient.

@jaredpar @AlekseyTs

@kotlarmilos
Copy link
Member

@VSadov If we decide blocking this scenario in class loader, for Mono try using deferred type load failures callback that should allow AOT compilation to generate the code and raise exceptions during the runtime.

if ((ftype->attrs & FIELD_ATTRIBUTE_INIT_ONLY) != 0) {
    if (mono_get_runtime_callbacks ()->mono_class_set_deferred_type_load_failure_callback) {
        if (mono_get_runtime_callbacks ()->mono_class_set_deferred_type_load_failure_callback (klass, "InlineArrayAttribute cannot be applied to a type with readonly instance fields."))
            break;
        else
            ; // failure occured during AOT compilation, continue execution
    } else {
        mono_class_set_type_load_failure (klass, "InlineArrayAttribute cannot be applied to a type with readonly instance fields.");
        break;
    }
}

/cc: @lambdageek

@AlekseyTs
Copy link
Contributor

Compiler simply blocks declarations of readonly element fields in source, no verification when we load type from metadata. I guess it is fine for runtime to not verify this aspect on type load, but, It still should be specified as illegal in runtime spec, I think.

@VSadov
Copy link
Member Author

VSadov commented Aug 23, 2023

I think we can close this.

@VSadov VSadov closed this Aug 23, 2023
@VSadov
Copy link
Member Author

VSadov commented Aug 23, 2023

Added an item related to this to the documentation tracking issue for InlineArray - #91012

@ghost ghost locked as resolved and limited conversation to collaborators Sep 23, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants