Add a code fix for should-have-been-array#3330
Open
amcasey wants to merge 4 commits into
Open
Conversation
It's a pretty common mistake to pass a single control qubit to a function or functor that expects an array of them. Add a code fix detecting that this has occurred and offering to add square brackets. To make this possible, attach info about the expected and actual types to the ErrorKind. (Unfortunately, Ty is hard to share, so create a simpler TyInfo that can accompany the ErrorKind.)
amcasey
commented
Jun 13, 2026
| #[test] | ||
| fn array_to_nested_array() { | ||
| // Qubit[] passed where Qubit[][] expected - wrapping in [...] is valid. | ||
| // Qubit[] isn't a primitive type, but it's actually complaining at the element |
Member
Author
There was a problem hiding this comment.
I don't love this, but I wasn't convinced it was worth "breaking" it for consistency. If we care, I think we can ask the compiler for the type of the expression at the error span and force that to be a primitive too.
Member
Author
There was a problem hiding this comment.
I did it, but I'd just as soon revert the new commit.
amcasey
commented
Jun 13, 2026
| #[error("expected {0}, found {1}")] | ||
| #[diagnostic(code("Qsc.TypeCk.TyMismatch"))] | ||
| TyMismatch(String, String, #[label] Span), | ||
| TyMismatch( |
Member
Author
There was a problem hiding this comment.
We talked about adding a new ErrorKind, but I thought enriching this one gave us more flexibility when creating future code actions.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
It's a pretty common mistake to pass a single control qubit to a function or functor that expects an array of them. Add a code fix detecting that this has occurred and offering to add square brackets.
To make this possible, attach info about the expected and actual types to the ErrorKind. (Unfortunately, Ty is hard to share, so create a simpler TyInfo that can accompany the ErrorKind.)