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

Allow type ascription on patterns and expressions #10502

Closed
huonw opened this issue Nov 15, 2013 · 11 comments
Closed

Allow type ascription on patterns and expressions #10502

huonw opened this issue Nov 15, 2013 · 11 comments
Labels
A-type-system Area: Type system C-enhancement Category: An issue proposing an enhancement or a PR with one.

Comments

@huonw
Copy link
Member

huonw commented Nov 15, 2013

It'd be nice if

for x: uint in something_generic() {
    more_generics(something_else_generic(x): f64)
}

allowed the programmer to give explicit type hints/annotations to patterns and expressions.

@thestinger
Copy link
Contributor

+1, but I think we should hold off on adding more features to for loops as long as they're a parser expansion hack. There's a serious need to get this moved into the compiler to provide saner semantics and comprehensible error messages.

@huonw
Copy link
Member Author

huonw commented Nov 15, 2013

The for-loops would follow automatically from allowing it in patterns, i.e.

let ((a, b): (u8, uint), c) = ((1, 2), "foo");

would be handled by the same path.

@thestinger
Copy link
Contributor

I don't think it currently has a let statement so it's going to make the unoptimized bytecode worse. I really want this in librustc with support for not borrowing from an lvalue, proper type checking for the iterator and saner code generation.

@huonw
Copy link
Member Author

huonw commented Nov 15, 2013

match foo {
   (x: u8, y) => { ... }
}

is also a pattern context.

@thestinger
Copy link
Contributor

Ah, I didn't realize the type annotations worked there. That would be fine!

@glaebhoerl
Copy link
Contributor

How tightly should type ascription bind? In Haskell it binds looser than everything else (so in a + b + c :: Int, it's a + b + c that's of type Int, not c), and this is sometimes what you want, but it's also frequently not.

@emberian
Copy link
Member

cc @pcwalton (he wanted this)

@nikomatsakis
Copy link
Contributor

I want this too. It's probably good enough to just allow type ascription on bindings, not on arbitrary patterns, which helps address @glehel's question of how tightly they should bind.

@nikomatsakis
Copy link
Contributor

Have to think about how this interacts with the current let pat: type = ...;, syntax and in particular with type coercions that can occur there -- the current behavior would be harder to reproduce this way (though it can probably be done), because we'd have to deduce the type that the expression should have by "bubbling the types up" through the pattern, which we don't currently do -- instead our pattern code is hard-wired to push types down.

@japaric
Copy link
Member

japaric commented Nov 2, 2014

@nick29581 Should this be closed in favor of rust-lang/rfcs#354?

@nrc
Copy link
Member

nrc commented Nov 2, 2014

yes

@nrc nrc closed this as completed Nov 2, 2014
flip1995 pushed a commit to flip1995/rust that referenced this issue Mar 24, 2023
fix `almost_swapped`: Ignore external macros

Fixes rust-lang#10421 ; Related to rust-lang#10499 (Fixing points *1* and *3* from rust-lang#10421)
changelog: [`almost_swapped`]: Add a check to ignore external macros
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-type-system Area: Type system C-enhancement Category: An issue proposing an enhancement or a PR with one.
Projects
None yet
Development

No branches or pull requests

7 participants