Skip to content

Add a code fix for should-have-been-array#3330

Open
amcasey wants to merge 4 commits into
mainfrom
amcasey/WrapInArray
Open

Add a code fix for should-have-been-array#3330
amcasey wants to merge 4 commits into
mainfrom
amcasey/WrapInArray

Conversation

@amcasey

@amcasey amcasey commented Jun 13, 2026

Copy link
Copy Markdown
Member

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.)

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.)
#[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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I did it, but I'd just as soon revert the new commit.

#[error("expected {0}, found {1}")]
#[diagnostic(code("Qsc.TypeCk.TyMismatch"))]
TyMismatch(String, String, #[label] Span),
TyMismatch(

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We talked about adding a new ErrorKind, but I thought enriching this one gave us more flexibility when creating future code actions.

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.

1 participant