-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
#37653 support default impl
for specialization
#37860
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @aturon (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
Should I resolve the merge and commit again? |
@giannicic It's best to rebase your branch and force push it, please don't insert extra merges (unless absolutely necessary) |
3c8225a
to
eb19417
Compare
Hi @giannicic! I'm so excited to see this PR! I'm also in the process of moving house this week. I will try my best to review this PR in the next couple of days, but there's a chance I won't be able to get to it until next week. Apologies in advance if that happens -- I'm eager to see this land. |
No problem @aturon ! |
8bb23ee
to
014d87b
Compare
@aturon @giannicic any updates? |
☔ The latest upstream changes (presumably #38692) made this pull request unmergeable. Please resolve the merge conflicts. |
Hi @aturon |
I'd like to try using this in my code (it's nightly only) |
I too am interested in trying this out. Anything blocking it from merging into nightly? |
@aturon ping |
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.
Thanks for working on this. As aturon seems to be occupied with other stuff i took liberty to review this PR myself. Overall great job. I found changes to the parser somewhat dubious but the rest looks great.
Please add a test for variations of pub unsafe default impl and optionally update src/grammar/parser-lalr.y as well to reflect the default impl.
src/librustc/hir/lowering.rs
Outdated
let new_impl_items = impl_items.iter() | ||
.map(|item| self.lower_impl_item_ref(item)) | ||
.collect(); | ||
let ifce = ifce.as_ref().map(|trait_ref| self.lower_trait_ref(trait_ref)); | ||
hir::ItemImpl(self.lower_unsafety(unsafety), | ||
self.lower_impl_polarity(polarity), | ||
self.lower_defaultness(defaultness, true /* [1] */), |
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.
This line seemingly refers to some comment ([1]
) but the footnote text does not seem to exist?
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.
Originally I've copied the lower_defaultness
method from line 1428
where there is a footnote explaining the reason for setting true
in the has_value
field of hir::Defaultness
.
In my case the has_value
method is never called in an impl
's defaultness
but i'll leave it as always true
in order to not cause an assertion failure inside the lower_defaultness
function.
I'm putting a footnote to explain this.
src/librustc/traits/project.rs
Outdated
@@ -953,7 +954,28 @@ fn assemble_candidates_from_impls<'cx, 'gcx, 'tcx>( | |||
// being invoked). | |||
node_item.item.defaultness.has_value() | |||
} else { | |||
node_item.item.defaultness.is_default() | |||
let is_default = match selcx.tcx() |
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.
Checking whether method is default should probably factored out somewhere. Maybe TyCtxt?
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.
Ok, I'm putting the TyCtxt
's method inside src/librustc/traits/util.rs
the method signature is the following:
pub fn impl_is_default(self, node_item_def_id: DefId) -> bool
@@ -668,14 +668,15 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { | |||
hir::ItemDefaultImpl(..) => { | |||
let data = ImplData { | |||
polarity: hir::ImplPolarity::Positive, | |||
defaultness: hir::Defaultness::Final, |
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.
Are you sure about this being Final? Does this mean that default impl Trait for .. {}
is not possible even with this PR?
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.
@nagisa I've read the discussion about UnwindSafe
#40628, this PR however is one of a set of tasks described here #37653,
the 3rd
all items in a default impl are (implicitly) default and hence specializable
It doesn't address default trait implementations.
At least another PR must follow in order to cover the other two points.
so we could define better what @abonander says about
re-addressed this in the context of specialization
and add it to the task list.
What do you think?
EDIT: I've placed an error during parsing to avoid default impl Trait for .. {}
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.
I’m not sure this PR is a right place to discuss UnwindUnsafe
. If you have any ideas, you should post post them on the UnwindUnsafe
issue.
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.
I agree. After this PR I'll have a better look at UnwindUnsafe
issue
src/librustc_typeck/check/mod.rs
Outdated
@@ -1041,7 +1041,23 @@ fn check_specialization_validity<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, | |||
|
|||
if let Some(parent) = parent { | |||
if parent.item.is_final() { | |||
report_forbidden_specialization(tcx, impl_item, parent.node.def_id()); | |||
let is_final = match tcx.map.as_local_node_id(parent.node.def_id()) { |
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.
Similarly should be factored out.
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.
Ok, It uses the impl_is_default
above
src/libsyntax/feature_gate.rs
Outdated
@@ -1136,6 +1136,15 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { | |||
}, | |||
_ => {} | |||
} | |||
|
|||
match defaultness { |
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.
if let hir::Defaultness::Default = ... {
...
}
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.
@nagisa Not sure I've understood this. Why should I check for hir::Defaultness in libsyntax?
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.
What I meant here is that this match
should be replaced with a if let
. hir
here was my mistake, sorry.
@@ -5809,13 +5811,19 @@ impl<'a> Parser<'a> { | |||
maybe_append(attrs, extra_attrs)); | |||
return Ok(Some(item)); | |||
} | |||
if self.check_keyword(keywords::Unsafe) && | |||
self.look_ahead(1, |t| t.is_keyword(keywords::Impl)) | |||
if (self.check_keyword(keywords::Unsafe) && |
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.
It seems wrong to me? At the very least lookahead of 2 tokens is unnecessary.
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.
I'm adding a test taken from src/test/run-pass/specialization/defaultimpl/specialization-basics.rs putting unsafe
to all occurences of default impl
.
Probably it's incorrect to look for default unsafe impl
, I'm changing in order to check for unsafe default impl
.
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.
Probably it's incorrect to look for default unsafe impl, I'm changing in order to check for unsafe default impl.
For all it is worth, the syntax for functions is like this:
[default] [unsafe] fn foo...
so default impl
should match it and be
[default] [unsafe] impl
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.
Okay, I guess 2 look aheads is fine for now, although it is pretty obvious this needs some refactor, which I’ll be doing later anyway.
@nagisa |
Ping @giannicic. Are you still working on this? |
Just wanted to make sure this PR isn't getting lost. |
this commit implements the first step of the `default impl` feature: all items in a `default impl` are (implicitly) `default` and hence specializable. In order to test this feature I've copied all the tests provided for the `default` method implementation (in run-pass/specialization and compile-fail/specialization directories) and moved the `default` keyword from the item to the impl. See referenced issue for further info
rebase after support for llvm-3.7
@nagisa |
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.
So there’s two blockers at the moment:
[default] [unsafe] impl
vs[unsafe] [default] impl
. The PR as is currently uses one in some places and the other in another places. I think it should be[default] [unsafe] impl
just because that’s the order used bydefault unsafe fn
.- The fact that something like typeck has to understand different ways a method could become default. This is a recipe for disaster (read: discrepancies in behaviour for code that is supposedly equivalent in behaviour).
All other comments unrelated to these two problems are nits and do not block landing this PR.
src/grammar/parser-lalr.y
Outdated
@@ -588,27 +594,27 @@ impl_method | |||
// they are ambiguous with traits. We do the same here, regrettably, | |||
// by splitting ty into ty and ty_prim. | |||
item_impl | |||
: maybe_unsafe IMPL generic_params ty_prim_sum maybe_where_clause '{' maybe_inner_attrs maybe_impl_items '}' | |||
: maybe_unsafe maybe_default_impl generic_params ty_prim_sum maybe_where_clause '{' maybe_inner_attrs maybe_impl_items '}' |
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.
See this comment
ref generics, | ||
ref opt_trait, | ||
ref ty, | ||
ref impl_items) => { | ||
self.head("")?; | ||
self.print_visibility(&item.vis)?; | ||
self.print_defaultness(defaultness)?; |
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.
This has a different order compared to the yacc grammar.
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.
I've changed the parser-lalr.y accordingly to the [default] [unsafe] impl
order so this file should be ok now
src/librustc_typeck/check/mod.rs
Outdated
report_forbidden_specialization(tcx, impl_item, parent.node.def_id()); | ||
if !tcx.impl_is_default(parent.node.def_id()) { | ||
report_forbidden_specialization(tcx, impl_item, parent.node.def_id()); | ||
} |
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.
I feel that this change should not exist. That is, typecheck should not have to make the distinction between x
being default because it is in a default impl
and x
being default because it is default fn
:
default impl Foo for Bar { fn x() { ... } fn y() { ... } }
impl Foo for Bar { default fn x() { ... } default fn y() { ... } }
One approach would be to change the is_final
somehow, to automatically check for both scenarios. Another would be to propagate defaultness from impl to its children items during some lowering step (say, from AST to HIR).
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.
@nagisa
When i first start to do this PR I was thinking about some propagation during lowering but I choose to not do it since I should have done something on hir/print.rs in order to show the defaultness info before the lowering occurred. An is_final
method should be placed on the NodeItem struct
, somthing like the following
impl<'a, 'gcx, 'tcx> NodeItem<hir::Defaultness> {
pub fn is_final(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> bool {
self.item.is_final() && !tcx.impl_is_default(self.node.def_id())
}
}
Then in typecheck just call parent.is_final(tcx)
Do you think it could be ok?
src/libsyntax/parse/parser.rs
Outdated
default trait implementations"); | ||
} | ||
_ => {} | ||
} |
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.
I was going to say that parser is certainly the wrong place for such a check, but then I noticed we already have a number of similar checks just above. I guess it is fine then…?
This match
should be an if let
instead.
@@ -5809,13 +5811,19 @@ impl<'a> Parser<'a> { | |||
maybe_append(attrs, extra_attrs)); | |||
return Ok(Some(item)); | |||
} | |||
if self.check_keyword(keywords::Unsafe) && | |||
self.look_ahead(1, |t| t.is_keyword(keywords::Impl)) | |||
if (self.check_keyword(keywords::Unsafe) && |
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.
Okay, I guess 2 look aheads is fine for now, although it is pretty obvious this needs some refactor, which I’ll be doing later anyway.
fc5133d
to
d110627
Compare
@nagisa |
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.
r=me with is_final
method being moved to a method on TyCtxt
. (either in librustc::ty::mod
, librustc::ty::util
or librustc::ty::context
is fine IMO).
src/librustc/traits/util.rs
Outdated
} | ||
|
||
impl<'a, 'gcx, 'tcx> NodeItem<hir::Defaultness> { | ||
pub fn is_final(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> bool { |
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.
I would probably have put this as a method on TyCtxt instead, as that’s how pretty much all such queries are implemented.
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.
Ok, I'm moving it on librustc::ty::util
`[default] [unsafe] impl` and typecheck
Thanks |
@bors r+ Thanks! |
📌 Commit b48eb5e has been approved by |
I gotta say this PR has the best"est" amount of tests I’ve ever seen – a lot. Props for that. |
#37653 support `default impl` for specialization this commit implements the first step of the `default impl` feature: > all items in a `default impl` are (implicitly) `default` and hence > specializable. In order to test this feature I've copied all the tests provided for the `default` method implementation (in run-pass/specialization and compile-fail/specialization directories) and moved the `default` keyword from the item to the impl. See [referenced](#37653) issue for further info r? @aturon
☀️ Test successful - status-appveyor, status-travis |
this commit implements the first step of the
default impl
feature:In order to test this feature I've copied all the tests provided for the
default
method implementation (in run-pass/specialization andcompile-fail/specialization directories) and moved the
default
keywordfrom the item to the impl.
See referenced issue for further info
r? @aturon