-
-
Notifications
You must be signed in to change notification settings - Fork 14.2k
auto-impl: parser support #149335
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
base: main
Are you sure you want to change the base?
auto-impl: parser support #149335
Changes from all commits
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 |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ use rustc_hir::{ | |
| use rustc_index::{IndexSlice, IndexVec}; | ||
| use rustc_middle::span_bug; | ||
| use rustc_middle::ty::{ResolverAstLowering, TyCtxt}; | ||
| use rustc_session::parse::feature_err; | ||
| use rustc_span::edit_distance::find_best_match_for_name; | ||
| use rustc_span::{DUMMY_SP, DesugaringKind, Ident, Span, Symbol, kw, sym}; | ||
| use smallvec::{SmallVec, smallvec}; | ||
|
|
@@ -388,6 +389,7 @@ impl<'hir> LoweringContext<'_, 'hir> { | |
| constness, | ||
| }) | ||
| } | ||
| ItemKind::AutoImpl(box AutoImpl { .. }) => todo!("we should implement lowering to HIR"), | ||
|
Contributor
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. Could you report a regular error instead of an ICE here and for
Contributor
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. Or report a fatal error in lowering and turn |
||
| ItemKind::Trait(box Trait { | ||
| constness, | ||
| is_auto, | ||
|
|
@@ -887,6 +889,48 @@ impl<'hir> LoweringContext<'_, 'hir> { | |
| true, | ||
| ) | ||
| } | ||
| AssocItemKind::AutoImpl(ai) => { | ||
| let tcx = self.tcx; | ||
| if !tcx.features().supertrait_auto_impl() { | ||
| feature_err( | ||
|
Contributor
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. Feature gating once in the parser should be enough. |
||
| &tcx.sess, | ||
| sym::supertrait_auto_impl, | ||
| i.span, | ||
| "feature is under construction", | ||
| ) | ||
| .emit(); | ||
| } | ||
| let generics = tcx.arena.alloc(hir::Generics { | ||
| has_where_clause_predicates: false, | ||
| params: &[], | ||
| predicates: &[], | ||
| where_clause_span: DUMMY_SP, | ||
| span: DUMMY_SP, | ||
| }); | ||
| ( | ||
| Ident::dummy(), | ||
| &*generics, | ||
| hir::TraitItemKind::AutoImpl( | ||
| tcx.arena.alloc(self.lower_poly_trait_ref_inner( | ||
| &ai.generics.params, | ||
| &TraitBoundModifiers { | ||
| constness: BoundConstness::Never, | ||
| asyncness: BoundAsyncness::Normal, | ||
| polarity: match ai.of_trait.polarity { | ||
| ImplPolarity::Positive => BoundPolarity::Positive, | ||
| ImplPolarity::Negative(span) => BoundPolarity::Negative(span), | ||
| }, | ||
| }, | ||
| &ai.of_trait.trait_ref, | ||
| ai.of_trait.trait_ref.path.span, | ||
| RelaxedBoundPolicy::Forbidden(RelaxedBoundForbiddenReason::SuperTrait), | ||
| ImplTraitContext::Disallowed(ImplTraitPosition::Generic), | ||
| )), | ||
| &[], | ||
| ), | ||
| false, | ||
| ) | ||
| } | ||
| AssocItemKind::Type(box TyAlias { | ||
| ident, | ||
| generics, | ||
|
|
@@ -1090,6 +1134,7 @@ impl<'hir> LoweringContext<'_, 'hir> { | |
| ), | ||
| ) | ||
| } | ||
| AssocItemKind::AutoImpl(_) => todo!(), | ||
| AssocItemKind::Delegation(box delegation) => { | ||
| let delegation_results = self.lower_delegation(delegation, i.id, is_in_trait_impl); | ||
| ( | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| use ast::StaticItem; | ||
| use itertools::{Itertools, Position}; | ||
| use rustc_ast::{self as ast, ModKind, TraitAlias}; | ||
| use rustc_span::Ident; | ||
| use rustc_span::{Ident, Span}; | ||
|
|
||
| use crate::pp::BoxMarker; | ||
| use crate::pp::Breaks::Inconsistent; | ||
|
|
@@ -350,6 +350,32 @@ impl<'a> State<'a> { | |
| let empty = item.attrs.is_empty() && items.is_empty(); | ||
| self.bclose(item.span, empty, cb); | ||
| } | ||
| ast::ItemKind::AutoImpl(box ast::AutoImpl { generics, of_trait, items, constness }) => { | ||
| let (cb, ib) = self.head(""); | ||
| self.print_visibility(&item.vis); | ||
|
|
||
| let &ast::TraitImplHeader { defaultness, safety, polarity, ref trait_ref } = | ||
| &**of_trait; | ||
| self.print_defaultness(defaultness); | ||
| self.print_safety(safety); | ||
| self.word("auto"); | ||
| self.word("impl"); | ||
| self.print_generic_params(&generics.params); | ||
| self.print_constness(*constness); | ||
| if let ast::ImplPolarity::Negative(_) = polarity { | ||
| self.word("!"); | ||
| } | ||
| self.print_trait_ref(trait_ref); | ||
|
|
||
| self.space(); | ||
| self.bopen(ib); | ||
| self.print_inner_attributes(&item.attrs); | ||
| for impl_item in items { | ||
| self.print_assoc_item(impl_item); | ||
| } | ||
| let empty = item.attrs.is_empty() && items.is_empty(); | ||
| self.bclose(item.span, empty, cb); | ||
|
Contributor
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.
|
||
| } | ||
| ast::ItemKind::Trait(box ast::Trait { | ||
| constness, | ||
| safety, | ||
|
|
@@ -601,6 +627,9 @@ impl<'a> State<'a> { | |
| self.word(";"); | ||
| } | ||
| } | ||
| ast::AssocItemKind::AutoImpl(ai) => { | ||
| self.print_assoc_auto_impl(&item.attrs, ai, item.span) | ||
| } | ||
| ast::AssocItemKind::Delegation(deleg) => self.print_delegation( | ||
| &item.attrs, | ||
| vis, | ||
|
|
@@ -621,6 +650,29 @@ impl<'a> State<'a> { | |
| self.ann.post(self, AnnNode::SubItem(id)) | ||
| } | ||
|
|
||
| fn print_assoc_auto_impl(&mut self, attrs: &[ast::Attribute], ai: &ast::AutoImpl, span: Span) { | ||
| let (cb, ib) = self.head(""); | ||
| let &ast::TraitImplHeader { defaultness, safety, polarity, ref trait_ref } = &*ai.of_trait; | ||
| self.print_defaultness(defaultness); | ||
| self.print_safety(safety); | ||
| self.word("auto"); | ||
| self.word("impl"); | ||
| self.print_constness(ai.constness); | ||
| if let ast::ImplPolarity::Negative(_) = polarity { | ||
| self.word("!"); | ||
| } | ||
| self.print_trait_ref(trait_ref); | ||
|
|
||
| self.space(); | ||
| self.bopen(ib); | ||
| self.print_inner_attributes(&attrs); | ||
| for impl_item in &ai.items { | ||
| self.print_assoc_item(impl_item); | ||
| } | ||
| let empty = attrs.is_empty() && ai.items.is_empty(); | ||
| self.bclose(span, empty, cb); | ||
| } | ||
|
|
||
| fn print_delegation( | ||
| &mut self, | ||
| attrs: &[ast::Attribute], | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -647,6 +647,8 @@ declare_features! ( | |
| (unstable, string_deref_patterns, "1.67.0", Some(87121)), | ||
| /// Allows `super let` statements. | ||
| (unstable, super_let, "1.88.0", Some(139076)), | ||
| /// Allows supertraits to be implemented with automatic defaults | ||
| (unstable, supertrait_auto_impl, "CURRENT_RUSTC_VERSION", Some(99999)), | ||
|
Contributor
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. There's no tracking issue for the experiment?
Contributor
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. Thanks @petrochenkov for raising this. You're right; this should have a tracking issue documenting the lang experiment and its champion. Talked with @dingxiangfei2009, after he pinged me, and he's filing the tracking issue for this, which we'll nominate and discuss in a meeting. |
||
| /// Allows subtrait items to shadow supertrait items. | ||
| (unstable, supertrait_item_shadowing, "1.86.0", Some(89151)), | ||
| /// Allows the use of target_feature when a function is marked inline(always). | ||
|
|
||
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.
There are generics in
AutoImpl, but this returnsNone.