-
Notifications
You must be signed in to change notification settings - Fork 13.4k
#37653 support default impl
for specialization
#37860
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
Changes from 4 commits
116e983
6427fdc
b0fca5f
715811d
b48eb5e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1326,7 +1326,13 @@ impl<'a> LoweringContext<'a> { | |
hir::ItemDefaultImpl(self.lower_unsafety(unsafety), | ||
trait_ref) | ||
} | ||
ItemKind::Impl(unsafety, polarity, ref generics, ref ifce, ref ty, ref impl_items) => { | ||
ItemKind::Impl(unsafety, | ||
polarity, | ||
defaultness, | ||
ref generics, | ||
ref ifce, | ||
ref ty, | ||
ref impl_items) => { | ||
let new_impl_items = impl_items.iter() | ||
.map(|item| self.lower_impl_item_ref(item)) | ||
.collect(); | ||
|
@@ -1340,6 +1346,7 @@ impl<'a> LoweringContext<'a> { | |
|
||
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 commentThe reason will be displayed to describe this comment to others. Learn more. This line seemingly refers to some comment ( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Originally I've copied the |
||
self.lower_generics(generics), | ||
ifce, | ||
self.lower_ty(ty), | ||
|
@@ -1355,6 +1362,9 @@ impl<'a> LoweringContext<'a> { | |
} | ||
ItemKind::MacroDef(..) | ItemKind::Mac(..) => panic!("Shouldn't still be around"), | ||
} | ||
|
||
// [1] `defaultness.has_value()` is necer called for an `impl`, always `true` in order to | ||
// not cause an assertion failure inside the `lower_defaultness` function | ||
} | ||
|
||
fn lower_trait_item(&mut self, i: &TraitItem) -> hir::TraitItem { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -678,12 +678,14 @@ impl<'a> State<'a> { | |
} | ||
hir::ItemImpl(unsafety, | ||
polarity, | ||
defaultness, | ||
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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. I've changed the parser-lalr.y accordingly to the |
||
self.print_unsafety(unsafety)?; | ||
self.word_nbsp("impl")?; | ||
|
||
|
@@ -820,6 +822,14 @@ impl<'a> State<'a> { | |
} | ||
} | ||
|
||
pub fn print_defaultness(&mut self, defaultness: hir::Defaultness) -> io::Result<()> { | ||
match defaultness { | ||
hir::Defaultness::Default { .. } => self.word_nbsp("default")?, | ||
hir::Defaultness::Final => (), | ||
} | ||
Ok(()) | ||
} | ||
|
||
pub fn print_struct(&mut self, | ||
struct_def: &hir::VariantData, | ||
generics: &hir::Generics, | ||
|
@@ -931,11 +941,7 @@ impl<'a> State<'a> { | |
self.hardbreak_if_not_bol()?; | ||
self.maybe_print_comment(ii.span.lo)?; | ||
self.print_outer_attributes(&ii.attrs)?; | ||
|
||
match ii.defaultness { | ||
hir::Defaultness::Default { .. } => self.word_nbsp("default")?, | ||
hir::Defaultness::Final => (), | ||
} | ||
self.print_defaultness(ii.defaultness)?; | ||
|
||
match ii.node { | ||
hir::ImplItemKind::Const(ref ty, expr) => { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -706,14 +706,15 @@ impl<'a, 'b: 'a, 'tcx: 'b> EntryBuilder<'a, 'b, '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 commentThe reason will be displayed to describe this comment to others. Learn more. Are you sure about this being Final? Does this mean that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @nagisa I've read the discussion about
It doesn't address default trait implementations.
and add it to the task list. EDIT: I've placed an error during parsing to avoid There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I’m not sure this PR is a right place to discuss There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree. After this PR I'll have a better look at |
||
parent_impl: None, | ||
coerce_unsized_info: None, | ||
trait_ref: tcx.impl_trait_ref(def_id).map(|trait_ref| self.lazy(&trait_ref)), | ||
}; | ||
|
||
EntryKind::DefaultImpl(self.lazy(&data)) | ||
} | ||
hir::ItemImpl(_, polarity, ..) => { | ||
hir::ItemImpl(_, polarity, defaultness, ..) => { | ||
let trait_ref = tcx.impl_trait_ref(def_id); | ||
let parent = if let Some(trait_ref) = trait_ref { | ||
let trait_def = tcx.lookup_trait_def(trait_ref.def_id); | ||
|
@@ -740,6 +741,7 @@ impl<'a, 'b: 'a, 'tcx: 'b> EntryBuilder<'a, 'b, 'tcx> { | |
|
||
let data = ImplData { | ||
polarity: polarity, | ||
defaultness: defaultness, | ||
parent_impl: parent, | ||
coerce_unsized_info: coerce_unsized_info, | ||
trait_ref: trait_ref.map(|trait_ref| self.lazy(&trait_ref)), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1142,7 +1142,9 @@ 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()); | ||
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 commentThe 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
One approach would be to change the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @nagisa
Then in typecheck just call Do you think it could be ok? |
||
} | ||
} | ||
|
||
|
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