-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
MIR: support user-given type annotations on fns, structs, and enums #53225
Conversation
2c90a18
to
3575f96
Compare
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
e36ff25
to
d4f5d1a
Compare
Hmm, it occurs to me that this code might not be handling normalizations very well. I'm inclined to land it anyway and open an issue for follow-up. It'll require some tweaks to the subtyping code that NLL is using to get right, and I'd sort of like to see if we can get away without it and instead push all the way to lazy normalizaton =) |
src/librustc/ty/context.rs
Outdated
@@ -371,6 +371,18 @@ pub struct TypeckTables<'tcx> { | |||
/// other items. | |||
node_substs: ItemLocalMap<&'tcx Substs<'tcx>>, | |||
|
|||
/// Stores the substitutions that the user explicit gave (if any) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: need adverb "explicitly"
src/libsyntax/feature_gate.rs
Outdated
@@ -858,6 +858,12 @@ pub const BUILTIN_ATTRIBUTES: &'static [(&'static str, AttributeType, AttributeG | |||
is just used for rustc unit tests \ | |||
and will never be stable", | |||
cfg_fn!(rustc_attrs))), | |||
("rustc_dump_user_substs", Whitelisted, Gated(Stability::Unstable, | |||
"rustc_attrs", | |||
"the `#[rustc_error]` attribute \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you should be saying #[rustc_dump_user_substs]
here, not #[rustc_error]
.
#![feature(nll)] | ||
#![feature(rustc_attrs)] | ||
|
||
trait Bazoom<T> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You might want to just comment here that the names T
and U
are significant in the sense that the comments for each test reference them to identify how the user-written type is yielding the particular components of the user substs that is printed by the test.
(or at least, when I first started reading the comments, I asked "wait, what is T
? And U
???" Because I hadn't even registered the names in my head when I read the trait
and impl
definitions here.
@@ -344,7 +344,8 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> { | |||
func: Operand::Constant(box Constant { | |||
span: test.span, | |||
ty: mty, | |||
literal: method | |||
user_ty: None, // FIXME |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you elaborate (perhaps in this FIXME comment, or perhaps just in a response to me here) on which kind of input expressions you expect need to reflect a user-type annotation? If I understand this part of the source correctly, this is where we are generating code to compare a pattern literal to the correspond part of the match
input; does Rust currently have a way to express a user-written type annotation on such a pattern literal? Or are you just anticipating rust-lang/rfcs#354 or rust-lang/rfcs#2522 ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TBH I don't know, but it was a constant that derived from user input. Can we use e.g. associated constants in match? If so, then this might apply, since I guess you could do something like <T as Trait<'a>>::BAR
(also, I'm not entirely how we should handle that, either. But that's for the next PR. =)
let a = 22; | ||
let b = 44; | ||
let c = 66; | ||
<_ as Bazoom<_>>::method::<&'static u32>(&a, b, &c); //~ ERROR |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm. For some reason, the use of the acronum "UFCS" on this test name made me think that it was going to (at least) be testing instances where the user type annotation was being injected at various points in the <_ as Bazoom<_>>::
part of the code you show here. (Isn't method::<&'static u32>
and such covered pretty well elsewhere? Not that you shouldn't test it; just surprised it got priority.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah never mind, your followup commits address that there are three different positions to cover in the UFCS form <One<'a> as Two<'b>>::method::<Three<'c>>(...)
(and it looks like you both rename this test to reflect that numbering and add the cases for positions One
and Two
).
r=me once you address minor points above and resolve the merge conflict |
d4f5d1a
to
88faf5f
Compare
@bors r=pnkfelix |
📌 Commit 88faf5fff5e45526bbcf2c00607f6cb1605983cb has been approved by |
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
⌛ Testing commit 88faf5fff5e45526bbcf2c00607f6cb1605983cb with merge 161b7f51df6c198ad83a8704f3c1ccaf16660861... |
💔 Test failed - status-travis |
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
Uh...
Oh, maybe due to the previous failure and a cancellation or something. |
@nikomatsakis I think this is travis-ci/travis-ci#4924, I've seen @kennytm make PRs that linked to this in the past. |
The UI test failure is likely legit though. Could you reproduce when merged with master? ---- [ui] ui/nll/user-annotations/dump-fn-method.rs stdout ----
diff of stderr:
1 error: user substs: Canonical { variables: [], value: [u32] }
- --> $DIR/dump-fn-method.rs:35:13
+ --> $DIR/dump-fn-method.rs:36:13
3 |
4 LL | let x = foo::<u32>; //~ ERROR [u32]
5 | ^^^^^^^^^^
6
7 error: user substs: Canonical { variables: [CanonicalVarInfo { kind: Ty(General) }, CanonicalVarInfo { kind: Ty(General) }], value: [?0, u32, ?1] }
- --> $DIR/dump-fn-method.rs:39:13
+ --> $DIR/dump-fn-method.rs:42:13
9 |
10 LL | let x = <_ as Bazoom<u32>>::method::<_>; //~ ERROR [?0, u32, ?1]
11 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12
13 error: user substs: Canonical { variables: [], value: [u8, u16, u32] }
- --> $DIR/dump-fn-method.rs:43:13
+ --> $DIR/dump-fn-method.rs:46:13
15 |
16 LL | let x = <u8 as Bazoom<u16>>::method::<u32>; //~ ERROR [u8, u16, u32]
17 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
18
19 error: user substs: Canonical { variables: [CanonicalVarInfo { kind: Ty(General) }, CanonicalVarInfo { kind: Ty(General) }], value: [?0, ?1, u32] }
- --> $DIR/dump-fn-method.rs:49:5
+ --> $DIR/dump-fn-method.rs:54:5
21 |
22 LL | y.method::<u32>(44, 66); //~ ERROR [?0, ?1, u32]
23 | ^^^^^^^^^^^^^^^^^^^^^^^ |
the 3 is because the type arguments are in the 3rd position
88faf5f
to
ed73a32
Compare
@bors r=pnkfelix |
📌 Commit ed73a32 has been approved by |
MIR: support user-given type annotations on fns, structs, and enums This branch adds tooling to track user-given type annotations on functions, structs, and enum variant expressions. The user-given types are passed onto NLL which then enforces them. cc #47184 — not a complete fix, as there are more cases to cover r? @eddyb cc @rust-lang/wg-compiler-nll
☀️ Test successful - status-appveyor, status-travis |
This branch adds tooling to track user-given type annotations on functions, structs, and enum variant expressions. The user-given types are passed onto NLL which then enforces them.
cc #47184 — not a complete fix, as there are more cases to cover
r? @eddyb
cc @rust-lang/wg-compiler-nll