Skip to content

Conversation

@ysthakur
Copy link
Member

This PR parses return types for commands and records these in the typechecker, although it doesn't check that the actual result type matches the expected type.

Regarding parsing, I chose to add a AstNode::InOutTypes(Vec<NodeId>) variant to hold the list of input-output type pairs. Making that list its own node means we can highlight the entire span [ foo -> bar ] in error messages, including the brackets. But if that's not something we need, I can get rid of the InOutTypes variant and directly store a Vec<NodeId> inside each AstNode::Def. That'd be simpler.

@ysthakur ysthakur marked this pull request as draft January 13, 2025 02:45
@ysthakur ysthakur marked this pull request as ready for review January 13, 2025 02:49
let return_ty = return_ty
.map(|ty| {
let AstNode::InOutTypes(types) = self.compiler.get_node(ty) else {
panic!("internal error: return type is not a return type");
Copy link
Contributor

Choose a reason for hiding this comment

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

This pattern is repeated a lot and is kind of unwieldy. It could use some helper or API refactor. Just a note for 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.

Yeah. I was thinking about a macro

Copy link
Contributor

Choose a reason for hiding this comment

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

I was thinking along the lines that if we made each AST node its own type, we could use generics, similar to this solution. That would also allow us to deduplicate all the .is_<token>() and .<token>() methods of the Parser.

I'm not a fan of macros since they have different syntax from the rest of Rust, and they can make code hard to read, but sometimes they're helpful.

name,
params,
return_ty: None,
return_ty,
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess the return_ty can be renamed to inout_types or similar at this point.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point, I'll update Def if I make a PR in the future related to it.

Copy link
Member Author

Choose a reason for hiding this comment

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

#51

@kubouch
Copy link
Contributor

kubouch commented Jan 17, 2025

Looks good, thanks! I left some notes/comments but nothing major, merging.

@kubouch kubouch merged commit 823083e into nushell:main Jan 17, 2025
4 checks passed
@ysthakur ysthakur deleted the in-out branch January 17, 2025 18:41
kubouch pushed a commit that referenced this pull request Mar 5, 2025
Since #46, defs have a list
of input-output type pairs rather than a single return type, so as per
@kubouch's suggestion on that PR, I've just renamed `return_ty` to
`in_out_types` everywhere.

Very tiny PR.
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.

2 participants