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

Tracking issue for RFC 803, "Type ascription" #23416

Closed
1 of 6 tasks
nikomatsakis opened this issue Mar 16, 2015 · 60 comments
Closed
1 of 6 tasks

Tracking issue for RFC 803, "Type ascription" #23416

nikomatsakis opened this issue Mar 16, 2015 · 60 comments
Labels
B-RFC-approved Blocker: Approved by a merged RFC but not yet implemented. B-RFC-implemented Blocker: Approved by a merged RFC and implemented. B-unstable Blocker: Implemented in the nightly compiler and unstable. C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. S-tracking-design-concerns Status: There are blocking design concerns. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@nikomatsakis
Copy link
Contributor

nikomatsakis commented Mar 16, 2015

Superseded by #101728 due to the landing of RFC 3307

Tracking issue for rust-lang/rfcs#803. Important points:

  • Implement the expr:Type syntax
  • Ensure soundness with respect to ref positions:
    • let ref x = <expr> / let mut ref = <expr>
    • match <expr>: Type { ref x => ... }
    • (<expr>: Type).method_with_ref_self()
  • Permit coercions like &[1, 2, 3]: &[u8] -- permit coercion in type ascription #78248
@steveklabnik steveklabnik added the B-RFC-approved Blocker: Approved by a merged RFC but not yet implemented. label Mar 18, 2015
@alexcrichton alexcrichton added the T-lang Relevant to the language team, which will review and decide on the PR/issue. label Aug 11, 2015
bors added a commit that referenced this issue Dec 19, 2015
This PR is a rebase of the original PR by @eddyb #21836 with some unrebasable parts manually reapplied, feature gate added + type equality restriction added as described below.

This implementation is partial because the type equality restriction is applied to all type ascription expressions and not only those in lvalue contexts. Thus, all difficulties with detection of these contexts and translation of coercions having effect in runtime are avoided.
So, you can't write things with coercions like `let slice = &[1, 2, 3]: &[u8];`. It obviously makes type ascription less useful than it should be, but it's still much more useful than not having type ascription at all.
In particular, things like `let v = something.iter().collect(): Vec<_>;` and `let u = t.into(): U;` work as expected and I'm pretty happy with these improvements alone.

Part of #23416
@petrochenkov
Copy link
Contributor

There's one thing making type ascription less convenient than it could potentially be. While it has the same operator priority as as (it is modeled after as in general), i.e. the highest from binary operators, it's still lower than member access . (dot).

Compare these two expressions

let v = v.iter().collect::<Vec<_>>().sort(); // Parens are not required
let v = (v.iter().collect(): Vec<_>).sort(); // Parens are required
// Formatted on several lines
let v = (v.iter().
           collect(): Vec<_>). // Looks bad, man
           sort();

I'm not sure if it is worth fixing or if it can be fixed without hacks, but it is worth discussing at least.

@hoodie
Copy link
Contributor

hoodie commented Dec 22, 2015

BTW: This has is a breaking change for crates that implement a similar macro syntax already, such as prettytables.

@jonas-schievink
Copy link
Contributor

@hoodie I opened #30531, since this seems worth tracking

@nrc
Copy link
Member

nrc commented Jan 5, 2016

cc @nrc

@alexcrichton alexcrichton added B-unstable Blocker: Implemented in the nightly compiler and unstable. and removed B-RFC-approved Blocker: Approved by a merged RFC but not yet implemented. labels Feb 18, 2016
@vi
Copy link
Contributor

vi commented Mar 21, 2016

Is it a good idea to only do ascription inside parentheses, so let a = x : T is not ascription yet, but let a = (x : T) is? This can allow more room for some further syntax additions.

@petrochenkov
Copy link
Contributor

For the record: a patch implementing support for method chaining with type ascription #33380

@ottworks
Copy link

ottworks commented Jul 3, 2016

Why is this in the book if it won't compile? (1.9.0)

https://doc.rust-lang.org/book/closures.html

@hoodie
Copy link
Contributor

hoodie commented Jul 3, 2016

@0TT what exactly?

@retep998
Copy link
Member

retep998 commented Jul 3, 2016

@0TT Type ascription is specifically when adding a : T type annotation after an expression. Not in the parameters to a closure or a function, and not on a variable binding. Only when it follows an expression which I do not see any cases of on the page you linked.

@ottworks
Copy link

ottworks commented Jul 3, 2016

Oops. I accidentally typo'd a semicolon as a colon:

let plus_two = |x| {
    let mut result: i32 = x;

    result += 1:
    result += 1;

    result
};
assert_eq!(4, plus_two(2));

@hoodie
Copy link
Contributor

hoodie commented Jul 3, 2016

that happens, how about an RFC to replace all semicolons with a less confusable character?

let plus_two = |x| {
    let mut result: i32 = x✓

    result += 1✓
    result += 1✓

    result
}assert_eq!(4, plus_two(2))

makes the language even look safer, everything is checked after all 😀

@ottworks
Copy link

ottworks commented Jul 3, 2016

Should use emoji tbh. A language for the future.

@nrc
Copy link
Member

nrc commented Aug 17, 2016

re the soundness question in the OP, this comment is probably the best summary.

@nrc nrc added B-RFC-approved Blocker: Approved by a merged RFC but not yet implemented. B-RFC-implemented Blocker: Approved by a merged RFC and implemented. labels Aug 29, 2016
@crumblingstatue
Copy link
Contributor

Before stabilizing this feature, I would like to see how this potentially impacts optional and named arguments, namely the expr : type syntax in function call argument position.

Could named arguments use this syntax if it wasn't for type ascription?

If yes, and no other syntaxes could be devised that are as optimal as name : type, I'd strongly recommend changing the syntax for type ascription, in favor of allowing the optimal syntax for named arguments.

I believe named arguments would be much more widely used than type ascription, and the syntax optimization for ergonomics should go there instead.

Ref rust-lang/rfcs#323

@misha-krainik
Copy link

misha-krainik commented Mar 19, 2019

@nikomatsakis could we improve error message in this case

proto::Status:ok()

I got

error[E0658]: type ascription is experimental (see issue #23416)
  --> src/main.rs:6:26
   |
76 |             status: Some(proto::Status:ok())
   |                          ^^^^^^^^^^^^^^^^^^
   |
   = help: add #![feature(type_ascription)] to the crate attributes to enable

According this RFC type ascription should contain : and space between words. But it's spreading on typo mistake with ::

@Centril
Copy link
Contributor

Centril commented Mar 19, 2019

cc @estebank ^-- We discussed this at the all hands on a night out... the idea, if you remember, was that you check for this pattern when the LHS of : is valid under <LHS> and RHS is not an existing type.

@estebank
Copy link
Contributor

@Centril I remember. I made #59150 with a different approach, more hacky but one that would have given the right error above.

@Centril
Copy link
Contributor

Centril commented Mar 19, 2019

@estebank Oh haha; I even reviewed that one... 😅

@Centril Centril changed the title Type ascription (tracking issue for RFC 803) Tracking issue for RFC 803, "Type ascription" May 3, 2019
@estebank
Copy link
Contributor

@Centril #62791 and #62816 should cover all of the (common) type ascription typos (: -> ;, foo(x: bar), foo:bar::baz(1), etc.). With that I believe that "diagnostics being terrible" is no longer a valid reason not to continue with the current syntax.

@pksunkara
Copy link
Contributor

What are the things that needs to be done to make this stable?

@Centril
Copy link
Contributor

Centril commented Feb 2, 2020

It's stuck on finding consensus in rust-lang/rfcs#2522 plus making the necessary changes (+ writing a lot of tests for them) outlined here and there.

@ethindp

This comment has been minimized.

@charmander

This comment has been minimized.

@ethindp
Copy link

ethindp commented Aug 16, 2020 via email

@ijackson
Copy link
Contributor

ijackson commented Nov 2, 2020

It's stuck on finding consensus in rust-lang/rfcs#2522 plus making the necessary changes (+ writing a lot of tests for them) outlined here and there.

RFC 2522 has been postponed. I think that means we should consider whether to stabilise type ascription for expressions as currently implemented. Looking through this issue I found only the following outstanding concerns/objections:

There was also the soundness question summarised here rust-lang/rfcs#803 (comment) . But I think having read #78248 that in fact this soundness issue does not arise because we do not support coercion in type ascription. Supporting coercion might be nice but I don't think it should be a blocker for stablising this feature.

@nikomatsakis
Copy link
Contributor Author

I opened #78248 which describes how (I think...) we can support coercions in a relatively straightforward way. I do think that coercions are an important part of this feature. However, there are some other concerns about the syntax that have been raised. Last time I checked we definitely did not have full consensus on going forward here.

@scottmcm scottmcm added the S-tracking-design-concerns Status: There are blocking design concerns. label Nov 10, 2021
github-actions bot pushed a commit to rust-lang/glacier that referenced this issue Dec 10, 2021
=== stdout ===
=== stderr ===
error: expected identifier, found reserved identifier `_`
 --> /home/runner/work/glacier/glacier/ices/91461.rs:1:7
  |
1 | #[d=n(_:d:,)]
  |       ^ expected identifier, found reserved identifier

error: expected type, found `,`
 --> /home/runner/work/glacier/glacier/ices/91461.rs:1:11
  |
1 | #[d=n(_:d:,)]
  |     -    -^ expected type
  |     |    |
  |     |    tried to parse a type due to this type ascription
  |     while parsing this struct
  |
  = note: `#![feature(type_ascription)]` lets you annotate an expression with a type: `<expr>: <type>`
  = note: see issue #23416 <rust-lang/rust#23416> for more information

error: expected type, found `,`
 --> /home/runner/work/glacier/glacier/ices/91461.rs:1:11
  |
1 | #[d=n(_:d:,)]
  |          -^ expected type
  |          |
  |          tried to parse a type due to this type ascription
  |
  = note: `#![feature(type_ascription)]` lets you annotate an expression with a type: `<expr>: <type>`
  = note: see issue #23416 <rust-lang/rust#23416> for more information

error: expected item after attributes
 --> /home/runner/work/glacier/glacier/ices/91461.rs:1:1
  |
1 | #[d=n(_:d:,)]
  | ^^^^^^^^^^^^^

error: aborting due to 4 previous errors

==============
@Manishearth
Copy link
Member

Superseded by #101728 due to the landing of RFC 3307

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
B-RFC-approved Blocker: Approved by a merged RFC but not yet implemented. B-RFC-implemented Blocker: Approved by a merged RFC and implemented. B-unstable Blocker: Implemented in the nightly compiler and unstable. C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. S-tracking-design-concerns Status: There are blocking design concerns. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests