From 383825b024c55b22c37dd8b679641275f3676cd7 Mon Sep 17 00:00:00 2001 From: Peh <20146907+randomicon00@users.noreply.github.com> Date: Mon, 23 May 2022 21:15:20 +0100 Subject: [PATCH 01/30] fix: generic bounds to impl Trait desugaring in trait objects --- compiler/rustc_ast/src/ast.rs | 2 + compiler/rustc_ast/src/mut_visit.rs | 5 ++- .../rustc_ast_passes/src/ast_validation.rs | 2 + compiler/rustc_parse/src/parser/path.rs | 9 +++- compiler/rustc_resolve/src/def_collector.rs | 44 +++++++++++++++---- compiler/rustc_resolve/src/lib.rs | 5 +++ 6 files changed, 56 insertions(+), 11 deletions(-) diff --git a/compiler/rustc_ast/src/ast.rs b/compiler/rustc_ast/src/ast.rs index 0deeb6849a216..bcae5dba8b1c2 100644 --- a/compiler/rustc_ast/src/ast.rs +++ b/compiler/rustc_ast/src/ast.rs @@ -1911,6 +1911,8 @@ pub struct AssocConstraint { pub gen_args: Option, pub kind: AssocConstraintKind, pub span: Span, + + pub impl_trait_id: NodeId, } /// The kinds of an `AssocConstraint`. diff --git a/compiler/rustc_ast/src/mut_visit.rs b/compiler/rustc_ast/src/mut_visit.rs index 1a93da8788a1f..8d9fb4b5ba4be 100644 --- a/compiler/rustc_ast/src/mut_visit.rs +++ b/compiler/rustc_ast/src/mut_visit.rs @@ -430,11 +430,14 @@ pub fn noop_flat_map_arm(mut arm: Arm, vis: &mut T) -> SmallVec<[ } pub fn noop_visit_constraint( - AssocConstraint { id, ident, gen_args, kind, span }: &mut AssocConstraint, + AssocConstraint { id, ident, gen_args, kind, span, impl_trait_id }: &mut AssocConstraint, vis: &mut T, ) { vis.visit_id(id); vis.visit_ident(ident); + + vis.visit_id(impl_trait_id); + if let Some(ref mut gen_args) = gen_args { vis.visit_generic_args(gen_args); } diff --git a/compiler/rustc_ast_passes/src/ast_validation.rs b/compiler/rustc_ast_passes/src/ast_validation.rs index 14e5d2ae62337..1f44fabbc4d11 100644 --- a/compiler/rustc_ast_passes/src/ast_validation.rs +++ b/compiler/rustc_ast_passes/src/ast_validation.rs @@ -1726,6 +1726,8 @@ fn deny_equality_constraints( term: predicate.rhs_ty.clone().into(), }, span: ident.span, + + impl_trait_id: rustc_ast::node_id::DUMMY_NODE_ID, }); // Add `` to `Foo`. match &mut assoc_path.segments[len].args { diff --git a/compiler/rustc_parse/src/parser/path.rs b/compiler/rustc_parse/src/parser/path.rs index 5c6fb376cd41a..715d64b7cee59 100644 --- a/compiler/rustc_parse/src/parser/path.rs +++ b/compiler/rustc_parse/src/parser/path.rs @@ -550,7 +550,14 @@ impl<'a> Parser<'a> { self.sess.gated_spans.gate(sym::associated_type_bounds, span); } let constraint = - AssocConstraint { id: ast::DUMMY_NODE_ID, ident, gen_args, kind, span }; + AssocConstraint { + id: ast::DUMMY_NODE_ID, + ident, gen_args, + kind, + span, + + impl_trait_id: ast::DUMMY_NODE_ID, + }; Ok(Some(AngleBracketedArg::Constraint(constraint))) } else { Ok(Some(AngleBracketedArg::Arg(arg))) diff --git a/compiler/rustc_resolve/src/def_collector.rs b/compiler/rustc_resolve/src/def_collector.rs index f0861103098d2..3882998c4d2ab 100644 --- a/compiler/rustc_resolve/src/def_collector.rs +++ b/compiler/rustc_resolve/src/def_collector.rs @@ -1,5 +1,5 @@ use crate::{ImplTraitContext, Resolver}; -use rustc_ast::visit::{self, FnKind}; +use rustc_ast::visit::{self, FnKind, BoundKind}; use rustc_ast::walk_list; use rustc_ast::*; use rustc_ast_lowering::ResolverAstLowering; @@ -285,19 +285,30 @@ impl<'a, 'b> visit::Visitor<'a> for DefCollector<'a, 'b> { TyKind::MacCall(..) => self.visit_macro_invoc(ty.id), TyKind::ImplTrait(node_id, _) => { let parent_def = match self.impl_trait_context { - ImplTraitContext::Universal(item_def) => self.resolver.create_def( - item_def, - node_id, - DefPathData::ImplTrait, - self.expansion.to_expn_id(), - ty.span, - ), + ImplTraitContext::Universal(item_def) | ImplTraitContext::UniversalInDyn(item_def) => { + let def_id = self.resolver.create_def( + item_def, + node_id, + DefPathData::ImplTrait, + self.expansion.to_expn_id(), + ty.span, + ); + self.resolver.impl_trait_context.insert(def_id, ImplTraitContext::Universal(item_def)); + def_id + } ImplTraitContext::Existential => { - self.create_def(node_id, DefPathData::ImplTrait, ty.span) + let def_id = self.create_def(node_id, DefPathData::ImplTrait, ty.span); + self.resolver.impl_trait_context.insert(def_id, ImplTraitContext::Existential); + def_id } }; self.with_parent(parent_def, |this| visit::walk_ty(this, ty)) } + TyKind::TraitObject(ref bounds, ..) => { + self.with_impl_trait(ImplTraitContext::UniversalInDyn(self.parent_def), |this| { + walk_list!(this, visit_param_bound, bounds, BoundKind::TraitObject); + }); + } _ => visit::walk_ty(self, ty), } } @@ -352,4 +363,19 @@ impl<'a, 'b> visit::Visitor<'a> for DefCollector<'a, 'b> { visit::walk_crate(self, krate) } } + + fn visit_assoc_constraint(&mut self, constraint: &'a AssocConstraint) { + if let ImplTraitContext::UniversalInDyn(item_def) = self.impl_trait_context { + let node_id = constraint.impl_trait_id; + self.resolver.create_def( + item_def, + node_id, + DefPathData::ImplTrait, + self.expansion.to_expn_id(), + constraint.span, + ); + } + + visit::walk_assoc_constraint(self, constraint); + } } diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index 6c0148a17a1b8..3df9684108550 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -165,6 +165,7 @@ impl<'a> ParentScope<'a> { enum ImplTraitContext { Existential, Universal(LocalDefId), + UniversalInDyn(LocalDefId), } #[derive(Eq)] @@ -1042,6 +1043,8 @@ pub struct Resolver<'a> { confused_type_with_std_module: FxHashMap, access_levels: AccessLevels, + + impl_trait_context: FxHashMap, } /// Nothing really interesting here; it just provides memory for the rest of the crate. @@ -1398,6 +1401,8 @@ impl<'a> Resolver<'a> { proc_macros: Default::default(), confused_type_with_std_module: Default::default(), access_levels: Default::default(), + + impl_trait_context: FxHashMap::default(), }; let root_parent_scope = ParentScope::module(graph_root, &resolver); From 95dc803d1446046d29dd103b6af01750249c4402 Mon Sep 17 00:00:00 2001 From: Peh <20146907+randomicon00@users.noreply.github.com> Date: Mon, 23 May 2022 21:19:58 +0100 Subject: [PATCH 02/30] fix: generic bounds to impl Trait desugaring in trait objects --- compiler/rustc_ast/src/mut_visit.rs | 1 - compiler/rustc_parse/src/parser/path.rs | 11 +++++------ 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/compiler/rustc_ast/src/mut_visit.rs b/compiler/rustc_ast/src/mut_visit.rs index 8d9fb4b5ba4be..ee17db01701b8 100644 --- a/compiler/rustc_ast/src/mut_visit.rs +++ b/compiler/rustc_ast/src/mut_visit.rs @@ -435,7 +435,6 @@ pub fn noop_visit_constraint( ) { vis.visit_id(id); vis.visit_ident(ident); - vis.visit_id(impl_trait_id); if let Some(ref mut gen_args) = gen_args { diff --git a/compiler/rustc_parse/src/parser/path.rs b/compiler/rustc_parse/src/parser/path.rs index 715d64b7cee59..ba23a004db813 100644 --- a/compiler/rustc_parse/src/parser/path.rs +++ b/compiler/rustc_parse/src/parser/path.rs @@ -550,12 +550,11 @@ impl<'a> Parser<'a> { self.sess.gated_spans.gate(sym::associated_type_bounds, span); } let constraint = - AssocConstraint { - id: ast::DUMMY_NODE_ID, - ident, gen_args, - kind, - span, - + AssocConstraint { + id: ast::DUMMY_NODE_ID, + ident, gen_args, + kind, + span, impl_trait_id: ast::DUMMY_NODE_ID, }; Ok(Some(AngleBracketedArg::Constraint(constraint))) From 192b6f69edd828011ee01315944491e222ee629a Mon Sep 17 00:00:00 2001 From: Peh <20146907+randomicon00@users.noreply.github.com> Date: Mon, 23 May 2022 21:24:10 +0100 Subject: [PATCH 03/30] fix: generic bounds to impl Trait desugaring in trait objects --- compiler/rustc_resolve/src/def_collector.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_resolve/src/def_collector.rs b/compiler/rustc_resolve/src/def_collector.rs index 3882998c4d2ab..6b7c61de6b879 100644 --- a/compiler/rustc_resolve/src/def_collector.rs +++ b/compiler/rustc_resolve/src/def_collector.rs @@ -375,7 +375,7 @@ impl<'a, 'b> visit::Visitor<'a> for DefCollector<'a, 'b> { constraint.span, ); } - + visit::walk_assoc_constraint(self, constraint); } } From 8d1a48c9ce2808b81a7104a8a28e7adbe2ff576b Mon Sep 17 00:00:00 2001 From: Peh <20146907+randomicon00@users.noreply.github.com> Date: Tue, 24 May 2022 21:05:42 +0100 Subject: [PATCH 04/30] use assoc constaint impl_context_id to create binding --- compiler/rustc_ast_lowering/src/lib.rs | 3 ++- compiler/rustc_parse/src/parser/path.rs | 16 ++++++++-------- compiler/rustc_resolve/src/def_collector.rs | 13 +++++++++---- 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index c143266f6c1de..925618c10ad20 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -1034,7 +1034,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { ); self.with_dyn_type_scope(false, |this| { - let node_id = this.resolver.next_node_id(); + //let node_id = this.resolver.next_node_id(); + let node_id = constraint.impl_trait_id; let ty = this.lower_ty( &Ty { id: node_id, diff --git a/compiler/rustc_parse/src/parser/path.rs b/compiler/rustc_parse/src/parser/path.rs index ba23a004db813..fb9f7ac29b784 100644 --- a/compiler/rustc_parse/src/parser/path.rs +++ b/compiler/rustc_parse/src/parser/path.rs @@ -549,14 +549,14 @@ impl<'a> Parser<'a> { if let AssocConstraintKind::Bound { .. } = kind { self.sess.gated_spans.gate(sym::associated_type_bounds, span); } - let constraint = - AssocConstraint { - id: ast::DUMMY_NODE_ID, - ident, gen_args, - kind, - span, - impl_trait_id: ast::DUMMY_NODE_ID, - }; + let constraint = AssocConstraint { + id: ast::DUMMY_NODE_ID, + ident, + gen_args, + kind, + span, + impl_trait_id: ast::DUMMY_NODE_ID, + }; Ok(Some(AngleBracketedArg::Constraint(constraint))) } else { Ok(Some(AngleBracketedArg::Arg(arg))) diff --git a/compiler/rustc_resolve/src/def_collector.rs b/compiler/rustc_resolve/src/def_collector.rs index 6b7c61de6b879..cd9ca5e206d34 100644 --- a/compiler/rustc_resolve/src/def_collector.rs +++ b/compiler/rustc_resolve/src/def_collector.rs @@ -1,5 +1,5 @@ use crate::{ImplTraitContext, Resolver}; -use rustc_ast::visit::{self, FnKind, BoundKind}; +use rustc_ast::visit::{self, BoundKind, FnKind}; use rustc_ast::walk_list; use rustc_ast::*; use rustc_ast_lowering::ResolverAstLowering; @@ -285,7 +285,8 @@ impl<'a, 'b> visit::Visitor<'a> for DefCollector<'a, 'b> { TyKind::MacCall(..) => self.visit_macro_invoc(ty.id), TyKind::ImplTrait(node_id, _) => { let parent_def = match self.impl_trait_context { - ImplTraitContext::Universal(item_def) | ImplTraitContext::UniversalInDyn(item_def) => { + ImplTraitContext::Universal(item_def) + | ImplTraitContext::UniversalInDyn(item_def) => { let def_id = self.resolver.create_def( item_def, node_id, @@ -293,12 +294,16 @@ impl<'a, 'b> visit::Visitor<'a> for DefCollector<'a, 'b> { self.expansion.to_expn_id(), ty.span, ); - self.resolver.impl_trait_context.insert(def_id, ImplTraitContext::Universal(item_def)); + self.resolver + .impl_trait_context + .insert(def_id, ImplTraitContext::Universal(item_def)); def_id } ImplTraitContext::Existential => { let def_id = self.create_def(node_id, DefPathData::ImplTrait, ty.span); - self.resolver.impl_trait_context.insert(def_id, ImplTraitContext::Existential); + self.resolver + .impl_trait_context + .insert(def_id, ImplTraitContext::Existential); def_id } }; From 711fc0e5140eb4d5c505735796afc55e1d790940 Mon Sep 17 00:00:00 2001 From: Peh <20146907+randomicon00@users.noreply.github.com> Date: Tue, 24 May 2022 21:36:49 +0100 Subject: [PATCH 05/30] use impl_trait_id to create impl trait def in ast lowering --- compiler/rustc_ast_lowering/src/lib.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index 925618c10ad20..aa39794495a76 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -1024,7 +1024,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { // Desugar `AssocTy: Bounds` into `AssocTy = impl Bounds`. We do this by // constructing the HIR for `impl bounds...` and then lowering that. - let impl_trait_node_id = self.resolver.next_node_id(); + //let impl_trait_node_id = self.resolver.next_node_id(); + let impl_trait_node_id = constraint.impl_trait_id; self.resolver.create_def( parent_def_id, impl_trait_node_id, @@ -1034,8 +1035,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { ); self.with_dyn_type_scope(false, |this| { - //let node_id = this.resolver.next_node_id(); - let node_id = constraint.impl_trait_id; + let node_id = this.resolver.next_node_id(); let ty = this.lower_ty( &Ty { id: node_id, From a02dc2f5dfdf9f41aa24817037f72c5037c7fa5d Mon Sep 17 00:00:00 2001 From: Peh <20146907+randomicon00@users.noreply.github.com> Date: Wed, 25 May 2022 21:57:10 +0100 Subject: [PATCH 06/30] attempt to fix ICE No HirId for DefId plus comments --- compiler/rustc_ast_lowering/src/lib.rs | 18 +++++++++++++----- compiler/rustc_resolve/src/def_collector.rs | 14 ++++++++++++-- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index aa39794495a76..9cd07c88907a2 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -977,7 +977,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { AssocConstraintKind::Bound { ref bounds } => { let mut parent_def_id = self.current_hir_id_owner; // Piggy-back on the `impl Trait` context to figure out the correct behavior. - let (desugar_to_impl_trait, itctx) = match itctx { + let (/*desugar_to_impl_trait*/_, itctx) = match itctx { // We are in the return position: // // fn foo() -> impl Iterator @@ -1020,22 +1020,30 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { _ => (false, itctx), }; - if desugar_to_impl_trait { + //if desugar_to_impl_trait { + if self.resolver.opt_local_def_id(constraint.impl_trait_id).is_some() { // Desugar `AssocTy: Bounds` into `AssocTy = impl Bounds`. We do this by // constructing the HIR for `impl bounds...` and then lowering that. + + // temporary added to avoid error unused parent_def_id + debug!("{:?}", parent_def_id); + // impl_trait_node_id already exists! No need to create it, return it. //let impl_trait_node_id = self.resolver.next_node_id(); let impl_trait_node_id = constraint.impl_trait_id; - self.resolver.create_def( + + // definition has been already created in def_collector assoc constraint + /*self.resolver.create_def( parent_def_id, impl_trait_node_id, DefPathData::ImplTrait, ExpnId::root(), constraint.span, - ); - + );*/ self.with_dyn_type_scope(false, |this| { let node_id = this.resolver.next_node_id(); + // impl_trait_node_id gets lowered in lower_ty_direct here: + // hir_id: self.lower_node_id(def_node_id), (line 1300) let ty = this.lower_ty( &Ty { id: node_id, diff --git a/compiler/rustc_resolve/src/def_collector.rs b/compiler/rustc_resolve/src/def_collector.rs index cd9ca5e206d34..8e4ed7f4dbb05 100644 --- a/compiler/rustc_resolve/src/def_collector.rs +++ b/compiler/rustc_resolve/src/def_collector.rs @@ -285,8 +285,8 @@ impl<'a, 'b> visit::Visitor<'a> for DefCollector<'a, 'b> { TyKind::MacCall(..) => self.visit_macro_invoc(ty.id), TyKind::ImplTrait(node_id, _) => { let parent_def = match self.impl_trait_context { - ImplTraitContext::Universal(item_def) - | ImplTraitContext::UniversalInDyn(item_def) => { + ImplTraitContext::Universal(item_def) => { + //| ImplTraitContext::UniversalInDyn(item_def) => { let def_id = self.resolver.create_def( item_def, node_id, @@ -306,6 +306,7 @@ impl<'a, 'b> visit::Visitor<'a> for DefCollector<'a, 'b> { .insert(def_id, ImplTraitContext::Existential); def_id } + ImplTraitContext::UniversalInDyn(_) => self.parent_def, }; self.with_parent(parent_def, |this| visit::walk_ty(this, ty)) } @@ -380,6 +381,15 @@ impl<'a, 'b> visit::Visitor<'a> for DefCollector<'a, 'b> { constraint.span, ); } + /*if let ImplTraitContext::UniversalInDyn(_) = self.impl_trait_context { + let node_id = constraint.impl_trait_id; + self.create_def( + node_id, + DefPathData::ImplTrait, + //self.expansion.to_expn_id(), + constraint.span, + ); + }*/ visit::walk_assoc_constraint(self, constraint); } From 3c4e763c32e55ceed9c58c637f2251424fc37305 Mon Sep 17 00:00:00 2001 From: Peh <20146907+randomicon00@users.noreply.github.com> Date: Wed, 25 May 2022 21:59:20 +0100 Subject: [PATCH 07/30] fix tidy errors --- compiler/rustc_ast_lowering/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index 9cd07c88907a2..cfeef8f663a4b 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -1024,14 +1024,14 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { if self.resolver.opt_local_def_id(constraint.impl_trait_id).is_some() { // Desugar `AssocTy: Bounds` into `AssocTy = impl Bounds`. We do this by // constructing the HIR for `impl bounds...` and then lowering that. - + // temporary added to avoid error unused parent_def_id debug!("{:?}", parent_def_id); // impl_trait_node_id already exists! No need to create it, return it. //let impl_trait_node_id = self.resolver.next_node_id(); let impl_trait_node_id = constraint.impl_trait_id; - + // definition has been already created in def_collector assoc constraint /*self.resolver.create_def( parent_def_id, From 305ef51c24291eda0d4ae7e0f23e94a5a761e067 Mon Sep 17 00:00:00 2001 From: Peh <20146907+randomicon00@users.noreply.github.com> Date: Wed, 25 May 2022 22:00:40 +0100 Subject: [PATCH 08/30] fix tidy errors --- compiler/rustc_ast_lowering/src/lib.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index cfeef8f663a4b..9431adfcab4b8 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -1024,14 +1024,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { if self.resolver.opt_local_def_id(constraint.impl_trait_id).is_some() { // Desugar `AssocTy: Bounds` into `AssocTy = impl Bounds`. We do this by // constructing the HIR for `impl bounds...` and then lowering that. - // temporary added to avoid error unused parent_def_id debug!("{:?}", parent_def_id); - // impl_trait_node_id already exists! No need to create it, return it. //let impl_trait_node_id = self.resolver.next_node_id(); let impl_trait_node_id = constraint.impl_trait_id; - // definition has been already created in def_collector assoc constraint /*self.resolver.create_def( parent_def_id, From 032d3d301da1cf9736f518f37b5b4060330a59dd Mon Sep 17 00:00:00 2001 From: Peh <20146907+randomicon00@users.noreply.github.com> Date: Wed, 25 May 2022 22:05:20 +0100 Subject: [PATCH 09/30] fix tidy errors --- compiler/rustc_ast_lowering/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index 9431adfcab4b8..5cdcaed496569 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -1029,7 +1029,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { // impl_trait_node_id already exists! No need to create it, return it. //let impl_trait_node_id = self.resolver.next_node_id(); let impl_trait_node_id = constraint.impl_trait_id; - // definition has been already created in def_collector assoc constraint + // definition has been already created in def_collector assoc constraint /*self.resolver.create_def( parent_def_id, impl_trait_node_id, @@ -1039,7 +1039,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { );*/ self.with_dyn_type_scope(false, |this| { let node_id = this.resolver.next_node_id(); - // impl_trait_node_id gets lowered in lower_ty_direct here: + // impl_trait_node_id gets lowered in lower_ty_direct here: // hir_id: self.lower_node_id(def_node_id), (line 1300) let ty = this.lower_ty( &Ty { From 48f8caed73a2117437ea15d43b474656e5e44adf Mon Sep 17 00:00:00 2001 From: Peh <20146907+randomicon00@users.noreply.github.com> Date: Wed, 25 May 2022 22:28:06 +0100 Subject: [PATCH 10/30] add desugaring from def_collector --- compiler/rustc_ast_lowering/src/lib.rs | 2 +- compiler/rustc_resolve/src/def_collector.rs | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index 5cdcaed496569..af2be60a81004 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -977,7 +977,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { AssocConstraintKind::Bound { ref bounds } => { let mut parent_def_id = self.current_hir_id_owner; // Piggy-back on the `impl Trait` context to figure out the correct behavior. - let (/*desugar_to_impl_trait*/_, itctx) = match itctx { + let (/*desugar_to_impl_trait*/ _, itctx) = match itctx { // We are in the return position: // // fn foo() -> impl Iterator diff --git a/compiler/rustc_resolve/src/def_collector.rs b/compiler/rustc_resolve/src/def_collector.rs index 8e4ed7f4dbb05..dea1776d6024d 100644 --- a/compiler/rustc_resolve/src/def_collector.rs +++ b/compiler/rustc_resolve/src/def_collector.rs @@ -286,7 +286,7 @@ impl<'a, 'b> visit::Visitor<'a> for DefCollector<'a, 'b> { TyKind::ImplTrait(node_id, _) => { let parent_def = match self.impl_trait_context { ImplTraitContext::Universal(item_def) => { - //| ImplTraitContext::UniversalInDyn(item_def) => { + //| ImplTraitContext::UniversalInDyn(item_def) => { let def_id = self.resolver.create_def( item_def, node_id, @@ -373,13 +373,14 @@ impl<'a, 'b> visit::Visitor<'a> for DefCollector<'a, 'b> { fn visit_assoc_constraint(&mut self, constraint: &'a AssocConstraint) { if let ImplTraitContext::UniversalInDyn(item_def) = self.impl_trait_context { let node_id = constraint.impl_trait_id; - self.resolver.create_def( + let def_id = self.resolver.create_def( item_def, node_id, DefPathData::ImplTrait, self.expansion.to_expn_id(), constraint.span, ); + self.resolver.impl_trait_context.insert(def_id, ImplTraitContext::UniversalInDyn(item_def)); } /*if let ImplTraitContext::UniversalInDyn(_) = self.impl_trait_context { let node_id = constraint.impl_trait_id; From d8138a2485c4991ea52755b92d7a510d2229ad46 Mon Sep 17 00:00:00 2001 From: Peh <20146907+randomicon00@users.noreply.github.com> Date: Thu, 2 Jun 2022 20:58:36 +0100 Subject: [PATCH 11/30] additional changes --- compiler/rustc_ast_lowering/src/lib.rs | 12 ++++++++++-- compiler/rustc_resolve/src/def_collector.rs | 18 +++++++++++------- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index af2be60a81004..63b833500c5bc 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -1030,13 +1030,14 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { //let impl_trait_node_id = self.resolver.next_node_id(); let impl_trait_node_id = constraint.impl_trait_id; // definition has been already created in def_collector assoc constraint - /*self.resolver.create_def( + self.resolver.create_def( parent_def_id, impl_trait_node_id, DefPathData::ImplTrait, ExpnId::root(), constraint.span, - );*/ + ); + self.lower_node_id(impl_trait_node_id); self.with_dyn_type_scope(false, |this| { let node_id = this.resolver.next_node_id(); // impl_trait_node_id gets lowered in lower_ty_direct here: @@ -1279,8 +1280,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { parent_def_id, ) => { // Add a definition for the in-band `Param`. + + // get the def_id that we created in the desugaring + // self.create_def(..) let def_id = self.resolver.local_def_id(def_node_id); + // lower param bounds `impl Bound1 + Bound2` let hir_bounds = self.lower_param_bounds( bounds, ImplTraitContext::Universal( @@ -1291,6 +1296,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { ); // Set the name to `impl Bound1 + Bound2`. let ident = Ident::from_str_and_span(&pprust::ty_to_string(t), span); + // add the new generic param GenericParam desugaring of the + // impl Bound1 (GenericParameters in place of impl Traits) in_band_ty_params.push(hir::GenericParam { hir_id: self.lower_node_id(def_node_id), name: ParamName::Plain(self.lower_ident(ident)), @@ -1359,6 +1366,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { // frequently opened issues show. let opaque_ty_span = self.mark_span_with_reason(DesugaringKind::OpaqueTy, span, None); + // impl_trait_node_id let opaque_ty_def_id = self.resolver.local_def_id(opaque_ty_node_id); let mut collected_lifetimes = FxHashMap::default(); diff --git a/compiler/rustc_resolve/src/def_collector.rs b/compiler/rustc_resolve/src/def_collector.rs index dea1776d6024d..3c66e6879bdea 100644 --- a/compiler/rustc_resolve/src/def_collector.rs +++ b/compiler/rustc_resolve/src/def_collector.rs @@ -371,7 +371,7 @@ impl<'a, 'b> visit::Visitor<'a> for DefCollector<'a, 'b> { } fn visit_assoc_constraint(&mut self, constraint: &'a AssocConstraint) { - if let ImplTraitContext::UniversalInDyn(item_def) = self.impl_trait_context { + /*if let ImplTraitContext::UniversalInDyn(item_def) = self.impl_trait_context { let node_id = constraint.impl_trait_id; let def_id = self.resolver.create_def( item_def, @@ -380,17 +380,21 @@ impl<'a, 'b> visit::Visitor<'a> for DefCollector<'a, 'b> { self.expansion.to_expn_id(), constraint.span, ); - self.resolver.impl_trait_context.insert(def_id, ImplTraitContext::UniversalInDyn(item_def)); - } - /*if let ImplTraitContext::UniversalInDyn(_) = self.impl_trait_context { + self.resolver + .impl_trait_context + .insert(def_id, ImplTraitContext::UniversalInDyn(item_def)); + }*/ + if let ImplTraitContext::UniversalInDyn(_) = self.impl_trait_context { let node_id = constraint.impl_trait_id; - self.create_def( + let def_id = self.create_def( node_id, DefPathData::ImplTrait, - //self.expansion.to_expn_id(), constraint.span, ); - }*/ + self.resolver + .impl_trait_context + .insert(def_id, ImplTraitContext::UniversalInDyn(def_id)); + } visit::walk_assoc_constraint(self, constraint); } From 3f55d239f5471660182e4711c515ba58ba4df1c0 Mon Sep 17 00:00:00 2001 From: Peh <20146907+randomicon00@users.noreply.github.com> Date: Thu, 2 Jun 2022 21:01:11 +0100 Subject: [PATCH 12/30] tidy fixes --- compiler/rustc_ast_lowering/src/lib.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index 63b833500c5bc..74fdfe53ab06a 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -1280,9 +1280,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { parent_def_id, ) => { // Add a definition for the in-band `Param`. - + // get the def_id that we created in the desugaring - // self.create_def(..) + // self.create_def(..) let def_id = self.resolver.local_def_id(def_node_id); // lower param bounds `impl Bound1 + Bound2` @@ -1366,7 +1366,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { // frequently opened issues show. let opaque_ty_span = self.mark_span_with_reason(DesugaringKind::OpaqueTy, span, None); - // impl_trait_node_id + // impl_trait_node_id let opaque_ty_def_id = self.resolver.local_def_id(opaque_ty_node_id); let mut collected_lifetimes = FxHashMap::default(); From a9ff3f3ce00357723c2ebe644fd14c4b1e9f0e42 Mon Sep 17 00:00:00 2001 From: Peh <20146907+randomicon00@users.noreply.github.com> Date: Thu, 2 Jun 2022 21:08:09 +0100 Subject: [PATCH 13/30] changes to def_collector --- compiler/rustc_resolve/src/def_collector.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/compiler/rustc_resolve/src/def_collector.rs b/compiler/rustc_resolve/src/def_collector.rs index 3c66e6879bdea..077b57305790d 100644 --- a/compiler/rustc_resolve/src/def_collector.rs +++ b/compiler/rustc_resolve/src/def_collector.rs @@ -386,11 +386,7 @@ impl<'a, 'b> visit::Visitor<'a> for DefCollector<'a, 'b> { }*/ if let ImplTraitContext::UniversalInDyn(_) = self.impl_trait_context { let node_id = constraint.impl_trait_id; - let def_id = self.create_def( - node_id, - DefPathData::ImplTrait, - constraint.span, - ); + let def_id = self.create_def(node_id, DefPathData::ImplTrait, constraint.span); self.resolver .impl_trait_context .insert(def_id, ImplTraitContext::UniversalInDyn(def_id)); From ba2aa482090ea8adc260d85b19b193474c101ddf Mon Sep 17 00:00:00 2001 From: Peh <20146907+randomicon00@users.noreply.github.com> Date: Mon, 6 Jun 2022 20:45:11 +0100 Subject: [PATCH 14/30] fix UniveralInDyn and replace desugar_to_impl_trait --- compiler/rustc_ast_lowering/src/lib.rs | 9 ++-- compiler/rustc_resolve/src/def_collector.rs | 50 +++++++++------------ 2 files changed, 25 insertions(+), 34 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index 74fdfe53ab06a..fe8b97dd64dfe 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -977,7 +977,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { AssocConstraintKind::Bound { ref bounds } => { let mut parent_def_id = self.current_hir_id_owner; // Piggy-back on the `impl Trait` context to figure out the correct behavior. - let (/*desugar_to_impl_trait*/ _, itctx) = match itctx { + let (_desugar_to_impl_trait /*_*/, itctx) = match itctx { // We are in the return position: // // fn foo() -> impl Iterator @@ -1026,10 +1026,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { // constructing the HIR for `impl bounds...` and then lowering that. // temporary added to avoid error unused parent_def_id debug!("{:?}", parent_def_id); - // impl_trait_node_id already exists! No need to create it, return it. - //let impl_trait_node_id = self.resolver.next_node_id(); let impl_trait_node_id = constraint.impl_trait_id; - // definition has been already created in def_collector assoc constraint + self.resolver.create_def( parent_def_id, impl_trait_node_id, @@ -1037,11 +1035,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { ExpnId::root(), constraint.span, ); - self.lower_node_id(impl_trait_node_id); + self.with_dyn_type_scope(false, |this| { let node_id = this.resolver.next_node_id(); // impl_trait_node_id gets lowered in lower_ty_direct here: - // hir_id: self.lower_node_id(def_node_id), (line 1300) let ty = this.lower_ty( &Ty { id: node_id, diff --git a/compiler/rustc_resolve/src/def_collector.rs b/compiler/rustc_resolve/src/def_collector.rs index 077b57305790d..21af4b3708665 100644 --- a/compiler/rustc_resolve/src/def_collector.rs +++ b/compiler/rustc_resolve/src/def_collector.rs @@ -1,5 +1,5 @@ use crate::{ImplTraitContext, Resolver}; -use rustc_ast::visit::{self, BoundKind, FnKind}; +use rustc_ast::visit::{self, FnKind}; use rustc_ast::walk_list; use rustc_ast::*; use rustc_ast_lowering::ResolverAstLowering; @@ -285,9 +285,10 @@ impl<'a, 'b> visit::Visitor<'a> for DefCollector<'a, 'b> { TyKind::MacCall(..) => self.visit_macro_invoc(ty.id), TyKind::ImplTrait(node_id, _) => { let parent_def = match self.impl_trait_context { - ImplTraitContext::Universal(item_def) => { - //| ImplTraitContext::UniversalInDyn(item_def) => { - let def_id = self.resolver.create_def( + ImplTraitContext::Universal(item_def) + | ImplTraitContext::UniversalInDyn(item_def) => { + // if universal in dyn? + let def_id = self.resolver.create_def( item_def, node_id, DefPathData::ImplTrait, @@ -306,13 +307,12 @@ impl<'a, 'b> visit::Visitor<'a> for DefCollector<'a, 'b> { .insert(def_id, ImplTraitContext::Existential); def_id } - ImplTraitContext::UniversalInDyn(_) => self.parent_def, }; self.with_parent(parent_def, |this| visit::walk_ty(this, ty)) } - TyKind::TraitObject(ref bounds, ..) => { + TyKind::TraitObject (..) => { self.with_impl_trait(ImplTraitContext::UniversalInDyn(self.parent_def), |this| { - walk_list!(this, visit_param_bound, bounds, BoundKind::TraitObject); + visit::walk_ty(this, ty) }); } _ => visit::walk_ty(self, ty), @@ -371,27 +371,21 @@ impl<'a, 'b> visit::Visitor<'a> for DefCollector<'a, 'b> { } fn visit_assoc_constraint(&mut self, constraint: &'a AssocConstraint) { - /*if let ImplTraitContext::UniversalInDyn(item_def) = self.impl_trait_context { - let node_id = constraint.impl_trait_id; - let def_id = self.resolver.create_def( - item_def, - node_id, - DefPathData::ImplTrait, - self.expansion.to_expn_id(), - constraint.span, - ); - self.resolver - .impl_trait_context - .insert(def_id, ImplTraitContext::UniversalInDyn(item_def)); - }*/ - if let ImplTraitContext::UniversalInDyn(_) = self.impl_trait_context { - let node_id = constraint.impl_trait_id; - let def_id = self.create_def(node_id, DefPathData::ImplTrait, constraint.span); - self.resolver - .impl_trait_context - .insert(def_id, ImplTraitContext::UniversalInDyn(def_id)); - } - + if let ImplTraitContext::UniversalInDyn(item_def) = self.impl_trait_context { + if let AssocConstraintKind::Bound { .. } = constraint.kind { + let node_id = constraint.impl_trait_id; + let def_id = self.resolver.create_def( + item_def, + node_id, + DefPathData::ImplTrait, + self.expansion.to_expn_id(), + constraint.span, + ); + self.resolver + .impl_trait_context + .insert(def_id, ImplTraitContext::UniversalInDyn(item_def)); + } + } visit::walk_assoc_constraint(self, constraint); } } From e4adc7ebaed6fa6e9ab9a24a5b6dd97009742da1 Mon Sep 17 00:00:00 2001 From: Peh <20146907+randomicon00@users.noreply.github.com> Date: Mon, 6 Jun 2022 20:50:57 +0100 Subject: [PATCH 15/30] fix tidy errors --- compiler/rustc_ast_lowering/src/lib.rs | 5 +---- compiler/rustc_resolve/src/def_collector.rs | 9 ++++----- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index fe8b97dd64dfe..eb97ef8f7ddc7 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -1024,10 +1024,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { if self.resolver.opt_local_def_id(constraint.impl_trait_id).is_some() { // Desugar `AssocTy: Bounds` into `AssocTy = impl Bounds`. We do this by // constructing the HIR for `impl bounds...` and then lowering that. - // temporary added to avoid error unused parent_def_id - debug!("{:?}", parent_def_id); let impl_trait_node_id = constraint.impl_trait_id; - + self.resolver.create_def( parent_def_id, impl_trait_node_id, @@ -1035,7 +1033,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { ExpnId::root(), constraint.span, ); - self.with_dyn_type_scope(false, |this| { let node_id = this.resolver.next_node_id(); // impl_trait_node_id gets lowered in lower_ty_direct here: diff --git a/compiler/rustc_resolve/src/def_collector.rs b/compiler/rustc_resolve/src/def_collector.rs index 21af4b3708665..726b40c1cd708 100644 --- a/compiler/rustc_resolve/src/def_collector.rs +++ b/compiler/rustc_resolve/src/def_collector.rs @@ -285,10 +285,9 @@ impl<'a, 'b> visit::Visitor<'a> for DefCollector<'a, 'b> { TyKind::MacCall(..) => self.visit_macro_invoc(ty.id), TyKind::ImplTrait(node_id, _) => { let parent_def = match self.impl_trait_context { - ImplTraitContext::Universal(item_def) + ImplTraitContext::Universal(item_def) | ImplTraitContext::UniversalInDyn(item_def) => { - // if universal in dyn? - let def_id = self.resolver.create_def( + let def_id = self.resolver.create_def( item_def, node_id, DefPathData::ImplTrait, @@ -310,7 +309,7 @@ impl<'a, 'b> visit::Visitor<'a> for DefCollector<'a, 'b> { }; self.with_parent(parent_def, |this| visit::walk_ty(this, ty)) } - TyKind::TraitObject (..) => { + TyKind::TraitObject(..) => { self.with_impl_trait(ImplTraitContext::UniversalInDyn(self.parent_def), |this| { visit::walk_ty(this, ty) }); @@ -385,7 +384,7 @@ impl<'a, 'b> visit::Visitor<'a> for DefCollector<'a, 'b> { .impl_trait_context .insert(def_id, ImplTraitContext::UniversalInDyn(item_def)); } - } + } visit::walk_assoc_constraint(self, constraint); } } From 573e1e890216b1231450f2fc9e4cf02ce2d7b9ba Mon Sep 17 00:00:00 2001 From: Peh <20146907+randomicon00@users.noreply.github.com> Date: Mon, 6 Jun 2022 21:21:39 +0100 Subject: [PATCH 16/30] remove comments --- compiler/rustc_ast_lowering/src/lib.rs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index eb97ef8f7ddc7..da3ff2248df0d 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -1274,12 +1274,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { parent_def_id, ) => { // Add a definition for the in-band `Param`. - - // get the def_id that we created in the desugaring - // self.create_def(..) let def_id = self.resolver.local_def_id(def_node_id); - // lower param bounds `impl Bound1 + Bound2` let hir_bounds = self.lower_param_bounds( bounds, ImplTraitContext::Universal( @@ -1290,8 +1286,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { ); // Set the name to `impl Bound1 + Bound2`. let ident = Ident::from_str_and_span(&pprust::ty_to_string(t), span); - // add the new generic param GenericParam desugaring of the - // impl Bound1 (GenericParameters in place of impl Traits) in_band_ty_params.push(hir::GenericParam { hir_id: self.lower_node_id(def_node_id), name: ParamName::Plain(self.lower_ident(ident)), From fdd03bc511d324219301bf5317b7b78f2ab5adb7 Mon Sep 17 00:00:00 2001 From: Peh <20146907+randomicon00@users.noreply.github.com> Date: Mon, 6 Jun 2022 21:26:16 +0100 Subject: [PATCH 17/30] fix impltrait and def --- compiler/rustc_ast_lowering/src/lib.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index da3ff2248df0d..05681117d443c 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -1024,6 +1024,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { if self.resolver.opt_local_def_id(constraint.impl_trait_id).is_some() { // Desugar `AssocTy: Bounds` into `AssocTy = impl Bounds`. We do this by // constructing the HIR for `impl bounds...` and then lowering that. + + let parent_def_id = parent_def_id; let impl_trait_node_id = constraint.impl_trait_id; self.resolver.create_def( From 44a113549956687884bad91565ee746188364810 Mon Sep 17 00:00:00 2001 From: Peh <20146907+randomicon00@users.noreply.github.com> Date: Mon, 6 Jun 2022 21:27:30 +0100 Subject: [PATCH 18/30] fix tidy errors --- compiler/rustc_ast_lowering/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index 05681117d443c..fb79577659f20 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -1024,7 +1024,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { if self.resolver.opt_local_def_id(constraint.impl_trait_id).is_some() { // Desugar `AssocTy: Bounds` into `AssocTy = impl Bounds`. We do this by // constructing the HIR for `impl bounds...` and then lowering that. - + let parent_def_id = parent_def_id; let impl_trait_node_id = constraint.impl_trait_id; From d942c143081f5a5724152555f33e8315a5a22b5f Mon Sep 17 00:00:00 2001 From: Peh <20146907+randomicon00@users.noreply.github.com> Date: Mon, 6 Jun 2022 21:37:32 +0100 Subject: [PATCH 19/30] solve conflicts --- compiler/rustc_ast_lowering/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index fb79577659f20..7658084af719b 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -1027,7 +1027,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { let parent_def_id = parent_def_id; let impl_trait_node_id = constraint.impl_trait_id; - + self.resolver.create_def( parent_def_id, impl_trait_node_id, From 4cab12349dd29a7e08668d7592fc1b863a210c05 Mon Sep 17 00:00:00 2001 From: Peh <20146907+randomicon00@users.noreply.github.com> Date: Mon, 6 Jun 2022 21:39:01 +0100 Subject: [PATCH 20/30] fix conflicts --- compiler/rustc_ast_lowering/src/lib.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index 7658084af719b..87b89529e05b9 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -1025,9 +1025,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { // Desugar `AssocTy: Bounds` into `AssocTy = impl Bounds`. We do this by // constructing the HIR for `impl bounds...` and then lowering that. - let parent_def_id = parent_def_id; let impl_trait_node_id = constraint.impl_trait_id; - + self.resolver.create_def( parent_def_id, impl_trait_node_id, From 3b67462c7e0f231c041c2d55866016969140e9d6 Mon Sep 17 00:00:00 2001 From: Peh <20146907+randomicon00@users.noreply.github.com> Date: Mon, 6 Jun 2022 22:15:14 +0100 Subject: [PATCH 21/30] fix build error --- compiler/rustc_ast_lowering/src/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index 7a573f0c359e6..36711e11a1388 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -888,7 +888,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { } AssocConstraintKind::Bound { ref bounds } => { // Piggy-back on the `impl Trait` context to figure out the correct behavior. - let (_desugar_to_impl_trait /*_*/, itctx) = match itctx { + let (_desugar_to_impl_trait, itctx) = match itctx { // We are in the return position: // // fn foo() -> impl Iterator @@ -933,6 +933,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { // Desugar `AssocTy: Bounds` into `AssocTy = impl Bounds`. We do this by // constructing the HIR for `impl bounds...` and then lowering that. + let parent_def_id = self.current_hir_id_owner; let impl_trait_node_id = constraint.impl_trait_id; self.resolver.create_def( From 1e694724d120c43e6b021c2e50c85b98441a5935 Mon Sep 17 00:00:00 2001 From: Peh <20146907+randomicon00@users.noreply.github.com> Date: Mon, 6 Jun 2022 22:15:14 +0100 Subject: [PATCH 22/30] fix build error --- compiler/rustc_ast_lowering/src/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index 7a573f0c359e6..36711e11a1388 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -888,7 +888,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { } AssocConstraintKind::Bound { ref bounds } => { // Piggy-back on the `impl Trait` context to figure out the correct behavior. - let (_desugar_to_impl_trait /*_*/, itctx) = match itctx { + let (_desugar_to_impl_trait, itctx) = match itctx { // We are in the return position: // // fn foo() -> impl Iterator @@ -933,6 +933,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { // Desugar `AssocTy: Bounds` into `AssocTy = impl Bounds`. We do this by // constructing the HIR for `impl bounds...` and then lowering that. + let parent_def_id = self.current_hir_id_owner; let impl_trait_node_id = constraint.impl_trait_id; self.resolver.create_def( From 6deece1c5444fa52663b59e58a08fb4418527117 Mon Sep 17 00:00:00 2001 From: Peh <20146907+randomicon00@users.noreply.github.com> Date: Mon, 6 Jun 2022 21:37:32 +0100 Subject: [PATCH 23/30] solve conflicts --- compiler/rustc_ast_lowering/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index 36711e11a1388..540ac0564760d 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -935,7 +935,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { let parent_def_id = self.current_hir_id_owner; let impl_trait_node_id = constraint.impl_trait_id; - + self.resolver.create_def( parent_def_id, impl_trait_node_id, From adaa6b0f09715dbf3f7760da9c6440a9b1d2afdd Mon Sep 17 00:00:00 2001 From: Peh <20146907+randomicon00@users.noreply.github.com> Date: Mon, 6 Jun 2022 22:36:25 +0100 Subject: [PATCH 24/30] fix tidy errors --- compiler/rustc_ast_lowering/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index 540ac0564760d..36711e11a1388 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -935,7 +935,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { let parent_def_id = self.current_hir_id_owner; let impl_trait_node_id = constraint.impl_trait_id; - + self.resolver.create_def( parent_def_id, impl_trait_node_id, From 61f667774df4a841005bc593aa56d5943ba299a4 Mon Sep 17 00:00:00 2001 From: Peh <20146907+randomicon00@users.noreply.github.com> Date: Tue, 7 Jun 2022 20:15:42 +0100 Subject: [PATCH 25/30] remove duplicate def creation --- compiler/rustc_ast_lowering/src/lib.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index 36711e11a1388..d6b9540ac1b5b 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -933,16 +933,16 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { // Desugar `AssocTy: Bounds` into `AssocTy = impl Bounds`. We do this by // constructing the HIR for `impl bounds...` and then lowering that. - let parent_def_id = self.current_hir_id_owner; + //let parent_def_id = self.current_hir_id_owner; let impl_trait_node_id = constraint.impl_trait_id; - self.resolver.create_def( + /*self.resolver.create_def( parent_def_id, impl_trait_node_id, DefPathData::ImplTrait, ExpnId::root(), constraint.span, - ); + );*/ self.with_dyn_type_scope(false, |this| { let node_id = this.resolver.next_node_id(); // impl_trait_node_id gets lowered in lower_ty_direct here: From 2743ece3ed52cb6fe1dd51a40d6cda26d7b238b1 Mon Sep 17 00:00:00 2001 From: Peh <20146907+randomicon00@users.noreply.github.com> Date: Tue, 14 Jun 2022 09:03:38 +0100 Subject: [PATCH 26/30] add creation of impl_trait_id for opaque type --- compiler/rustc_ast_lowering/src/lib.rs | 9 ++-- compiler/rustc_resolve/src/def_collector.rs | 46 +++++++++++++++++++-- compiler/rustc_resolve/src/lib.rs | 1 + 3 files changed, 48 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index d6b9540ac1b5b..71ccf224c4d0c 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -888,7 +888,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { } AssocConstraintKind::Bound { ref bounds } => { // Piggy-back on the `impl Trait` context to figure out the correct behavior. - let (_desugar_to_impl_trait, itctx) = match itctx { + let (desugar_to_impl_trait, itctx) = match itctx { // We are in the return position: // // fn foo() -> impl Iterator @@ -932,7 +932,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { if self.resolver.opt_local_def_id(constraint.impl_trait_id).is_some() { // Desugar `AssocTy: Bounds` into `AssocTy = impl Bounds`. We do this by // constructing the HIR for `impl bounds...` and then lowering that. - + debug!("desug111: {:?}, {:?}", desugar_to_impl_trait, itctx); //let parent_def_id = self.current_hir_id_owner; let impl_trait_node_id = constraint.impl_trait_id; @@ -943,9 +943,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { ExpnId::root(), constraint.span, );*/ + self.with_dyn_type_scope(false, |this| { let node_id = this.resolver.next_node_id(); - // impl_trait_node_id gets lowered in lower_ty_direct here: let ty = this.lower_ty( &Ty { id: node_id, @@ -962,6 +962,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { // Desugar `AssocTy: Bounds` into a type binding where the // later desugars into a trait predicate. let bounds = self.lower_param_bounds(bounds, itctx); + debug!("desug1111 NO!!!!: {:?}, {:?}", desugar_to_impl_trait, itctx); hir::TypeBindingKind::Constraint { bounds } } @@ -1216,7 +1217,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { ImplTraitContext::Universal => { // Add a definition for the in-band `Param`. let def_id = self.resolver.local_def_id(def_node_id); - + let hir_bounds = self.lower_param_bounds(bounds, ImplTraitContext::Universal); // Set the name to `impl Bound1 + Bound2`. diff --git a/compiler/rustc_resolve/src/def_collector.rs b/compiler/rustc_resolve/src/def_collector.rs index 6bc87c14f111a..5d66e0a833886 100644 --- a/compiler/rustc_resolve/src/def_collector.rs +++ b/compiler/rustc_resolve/src/def_collector.rs @@ -154,8 +154,24 @@ impl<'a, 'b> visit::Visitor<'a> for DefCollector<'a, 'b> { self.with_parent(closure_def, |this| walk_list!(this, visit_block, body)); return; } - } + if let FnRetTy::Ty(ref ty) = &sig.decl.output { + if let TyKind::ImplTrait(..) = ty.kind { + //let node_id = self.create_def(node_id, DefPathData::Impl, span); + self.visit_generics(generics); + + self.visit_fn_header(&sig.header); + for param in &sig.decl.inputs { + self.visit_param(param); + } + self.with_impl_trait(ImplTraitContext::ReturnOpaquePositionTy, |this| { + this.visit_fn_ret_ty(&sig.decl.output) + }); + walk_list!(self, visit_block, body); + return; + } + } + } visit::walk_fn(self, fn_kind, span); } @@ -286,7 +302,7 @@ impl<'a, 'b> visit::Visitor<'a> for DefCollector<'a, 'b> { TyKind::ImplTrait(node_id, _) => { let parent_def = match self.impl_trait_context { ImplTraitContext::Universal(item_def) - | ImplTraitContext::UniversalInDyn(item_def) => { + | ImplTraitContext::UniversalInDyn(item_def) => { let def_id = self.resolver.create_def( item_def, node_id, @@ -296,7 +312,7 @@ impl<'a, 'b> visit::Visitor<'a> for DefCollector<'a, 'b> { ); self.resolver .impl_trait_context - .insert(def_id, ImplTraitContext::Universal(item_def)); + .insert(def_id, self.impl_trait_context); def_id } ImplTraitContext::Existential => { @@ -306,7 +322,15 @@ impl<'a, 'b> visit::Visitor<'a> for DefCollector<'a, 'b> { .insert(def_id, ImplTraitContext::Existential); def_id } + ImplTraitContext::ReturnOpaquePositionTy => { + let def_id = self.create_def(node_id, DefPathData::ImplTrait, ty.span); + self.resolver + .impl_trait_context + .insert(def_id, self.impl_trait_context); + def_id + } }; + self.with_parent(parent_def, |this| visit::walk_ty(this, ty)) } TyKind::TraitObject(..) => { @@ -382,7 +406,21 @@ impl<'a, 'b> visit::Visitor<'a> for DefCollector<'a, 'b> { ); self.resolver .impl_trait_context - .insert(def_id, ImplTraitContext::UniversalInDyn(item_def)); + .insert(def_id, self.impl_trait_context); + } + } + + if let ImplTraitContext::ReturnOpaquePositionTy = self.impl_trait_context { + if let AssocConstraintKind::Bound { .. } = constraint.kind { + let node_id = constraint.impl_trait_id; + let def_id = self.create_def( + node_id, + DefPathData::ImplTrait, + constraint.span, + ); + self.resolver + .impl_trait_context + .insert(def_id, self.impl_trait_context); } } visit::walk_assoc_constraint(self, constraint); diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index 7c67ca4387b9f..9bd660bb62ce2 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -165,6 +165,7 @@ enum ImplTraitContext { Existential, Universal(LocalDefId), UniversalInDyn(LocalDefId), + ReturnOpaquePositionTy, } #[derive(Eq)] From 7068ec069e2da8de76b9f2c8c18f4f4879766d29 Mon Sep 17 00:00:00 2001 From: Peh <20146907+randomicon00@users.noreply.github.com> Date: Tue, 14 Jun 2022 09:07:25 +0100 Subject: [PATCH 27/30] fix tidy --- compiler/rustc_ast_lowering/src/lib.rs | 4 ++-- compiler/rustc_resolve/src/def_collector.rs | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index 71ccf224c4d0c..8b2dac5ce90c2 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -943,7 +943,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { ExpnId::root(), constraint.span, );*/ - + self.with_dyn_type_scope(false, |this| { let node_id = this.resolver.next_node_id(); let ty = this.lower_ty( @@ -1217,7 +1217,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { ImplTraitContext::Universal => { // Add a definition for the in-band `Param`. let def_id = self.resolver.local_def_id(def_node_id); - + let hir_bounds = self.lower_param_bounds(bounds, ImplTraitContext::Universal); // Set the name to `impl Bound1 + Bound2`. diff --git a/compiler/rustc_resolve/src/def_collector.rs b/compiler/rustc_resolve/src/def_collector.rs index 5d66e0a833886..a539de7ea62ab 100644 --- a/compiler/rustc_resolve/src/def_collector.rs +++ b/compiler/rustc_resolve/src/def_collector.rs @@ -165,8 +165,8 @@ impl<'a, 'b> visit::Visitor<'a> for DefCollector<'a, 'b> { self.visit_param(param); } self.with_impl_trait(ImplTraitContext::ReturnOpaquePositionTy, |this| { - this.visit_fn_ret_ty(&sig.decl.output) - }); + this.visit_fn_ret_ty(&sig.decl.output) + }); walk_list!(self, visit_block, body); return; } @@ -302,7 +302,7 @@ impl<'a, 'b> visit::Visitor<'a> for DefCollector<'a, 'b> { TyKind::ImplTrait(node_id, _) => { let parent_def = match self.impl_trait_context { ImplTraitContext::Universal(item_def) - | ImplTraitContext::UniversalInDyn(item_def) => { + | ImplTraitContext::UniversalInDyn(item_def) => { let def_id = self.resolver.create_def( item_def, node_id, @@ -327,10 +327,10 @@ impl<'a, 'b> visit::Visitor<'a> for DefCollector<'a, 'b> { self.resolver .impl_trait_context .insert(def_id, self.impl_trait_context); - def_id + def_id } }; - + self.with_parent(parent_def, |this| visit::walk_ty(this, ty)) } TyKind::TraitObject(..) => { @@ -418,7 +418,7 @@ impl<'a, 'b> visit::Visitor<'a> for DefCollector<'a, 'b> { DefPathData::ImplTrait, constraint.span, ); - self.resolver + self.resolver .impl_trait_context .insert(def_id, self.impl_trait_context); } From 1cd97799c7429d00276826f293a069cd9b65409a Mon Sep 17 00:00:00 2001 From: Peh <20146907+randomicon00@users.noreply.github.com> Date: Tue, 14 Jun 2022 09:08:48 +0100 Subject: [PATCH 28/30] fix tidy 2 --- compiler/rustc_ast_lowering/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index 8b2dac5ce90c2..7cb82d8e00200 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -943,7 +943,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { ExpnId::root(), constraint.span, );*/ - + self.with_dyn_type_scope(false, |this| { let node_id = this.resolver.next_node_id(); let ty = this.lower_ty( From 05ad36a4106e088e31106f2de60c17f25fd9ebf9 Mon Sep 17 00:00:00 2001 From: Peh <20146907+randomicon00@users.noreply.github.com> Date: Fri, 24 Jun 2022 22:20:49 +0100 Subject: [PATCH 29/30] fix: add return type to impl_trait_id --- compiler/rustc_ast_lowering/src/lib.rs | 5 +- compiler/rustc_resolve/src/def_collector.rs | 108 +++++++++++--------- compiler/rustc_resolve/src/lib.rs | 1 + 3 files changed, 64 insertions(+), 50 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index 7cb82d8e00200..61d7908b31583 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -928,7 +928,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { _ => (false, itctx), }; - //if desugar_to_impl_trait { + //if desugar_to_impl_trait if self.resolver.opt_local_def_id(constraint.impl_trait_id).is_some() { // Desugar `AssocTy: Bounds` into `AssocTy = impl Bounds`. We do this by // constructing the HIR for `impl bounds...` and then lowering that. @@ -943,7 +943,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { ExpnId::root(), constraint.span, );*/ - self.with_dyn_type_scope(false, |this| { let node_id = this.resolver.next_node_id(); let ty = this.lower_ty( @@ -962,7 +961,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { // Desugar `AssocTy: Bounds` into a type binding where the // later desugars into a trait predicate. let bounds = self.lower_param_bounds(bounds, itctx); - debug!("desug1111 NO!!!!: {:?}, {:?}", desugar_to_impl_trait, itctx); + debug!("desug1111 NO: {:?}, {:?}", desugar_to_impl_trait, itctx); hir::TypeBindingKind::Constraint { bounds } } diff --git a/compiler/rustc_resolve/src/def_collector.rs b/compiler/rustc_resolve/src/def_collector.rs index a539de7ea62ab..9b91abb6fee9e 100644 --- a/compiler/rustc_resolve/src/def_collector.rs +++ b/compiler/rustc_resolve/src/def_collector.rs @@ -1,5 +1,5 @@ use crate::{ImplTraitContext, Resolver}; -use rustc_ast::visit::{self, FnKind}; +use rustc_ast::visit::{self, BoundKind, FnKind}; use rustc_ast::walk_list; use rustc_ast::*; use rustc_ast_lowering::ResolverAstLowering; @@ -117,18 +117,36 @@ impl<'a, 'b> visit::Visitor<'a> for DefCollector<'a, 'b> { let def = self.create_def(i.id, def_data, i.span); self.with_parent(def, |this| { - this.with_impl_trait(ImplTraitContext::Existential, |this| { - match i.kind { - ItemKind::Struct(ref struct_def, _) | ItemKind::Union(ref struct_def, _) => { - // If this is a unit or tuple-like struct, register the constructor. - if let Some(ctor_hir_id) = struct_def.ctor_id() { - this.create_def(ctor_hir_id, DefPathData::Ctor, i.span); + if let ItemKind::TyAlias(box TyAlias { ref generics, ref bounds, ref ty, .. }) = i.kind + { + this.with_impl_trait(ImplTraitContext::Existential, |this| { + //visit::walk_item(this, i); + this.visit_vis(&i.vis); + this.visit_ident(i.ident); + + this.visit_generics(generics); + walk_list!(this, visit_param_bound, bounds, BoundKind::Bound); + + this.with_impl_trait(ImplTraitContext::TyAliasOpaqueTy, |this| { + walk_list!(this, visit_ty, ty) + }); + walk_list!(this, visit_attribute, &i.attrs); + }) + } else { + this.with_impl_trait(ImplTraitContext::Existential, |this| { + match i.kind { + ItemKind::Struct(ref struct_def, _) + | ItemKind::Union(ref struct_def, _) => { + // If this is a unit or tuple-like struct, register the constructor. + if let Some(ctor_hir_id) = struct_def.ctor_id() { + this.create_def(ctor_hir_id, DefPathData::Ctor, i.span); + } } + _ => {} } - _ => {} - } - visit::walk_item(this, i); - }) + visit::walk_item(this, i); + }) + } }); } @@ -310,23 +328,17 @@ impl<'a, 'b> visit::Visitor<'a> for DefCollector<'a, 'b> { self.expansion.to_expn_id(), ty.span, ); - self.resolver - .impl_trait_context - .insert(def_id, self.impl_trait_context); + self.resolver.impl_trait_context.insert(def_id, self.impl_trait_context); def_id } - ImplTraitContext::Existential => { + ImplTraitContext::Existential | ImplTraitContext::TyAliasOpaqueTy => { let def_id = self.create_def(node_id, DefPathData::ImplTrait, ty.span); - self.resolver - .impl_trait_context - .insert(def_id, ImplTraitContext::Existential); + self.resolver.impl_trait_context.insert(def_id, self.impl_trait_context); def_id } ImplTraitContext::ReturnOpaquePositionTy => { let def_id = self.create_def(node_id, DefPathData::ImplTrait, ty.span); - self.resolver - .impl_trait_context - .insert(def_id, self.impl_trait_context); + self.resolver.impl_trait_context.insert(def_id, self.impl_trait_context); def_id } }; @@ -394,35 +406,37 @@ impl<'a, 'b> visit::Visitor<'a> for DefCollector<'a, 'b> { } fn visit_assoc_constraint(&mut self, constraint: &'a AssocConstraint) { - if let ImplTraitContext::UniversalInDyn(item_def) = self.impl_trait_context { - if let AssocConstraintKind::Bound { .. } = constraint.kind { - let node_id = constraint.impl_trait_id; - let def_id = self.resolver.create_def( - item_def, - node_id, - DefPathData::ImplTrait, - self.expansion.to_expn_id(), - constraint.span, - ); - self.resolver - .impl_trait_context - .insert(def_id, self.impl_trait_context); + match self.impl_trait_context { + ImplTraitContext::UniversalInDyn(item_def) => { + if let AssocConstraintKind::Bound { .. } = constraint.kind { + let node_id = constraint.impl_trait_id; + let def_id = self.resolver.create_def( + item_def, + node_id, + DefPathData::ImplTrait, + self.expansion.to_expn_id(), + constraint.span, + ); + self.resolver.impl_trait_context.insert(def_id, self.impl_trait_context); + } } - } - - if let ImplTraitContext::ReturnOpaquePositionTy = self.impl_trait_context { - if let AssocConstraintKind::Bound { .. } = constraint.kind { - let node_id = constraint.impl_trait_id; - let def_id = self.create_def( - node_id, - DefPathData::ImplTrait, - constraint.span, - ); - self.resolver - .impl_trait_context - .insert(def_id, self.impl_trait_context); + ImplTraitContext::ReturnOpaquePositionTy => { + if let AssocConstraintKind::Bound { .. } = constraint.kind { + let node_id = constraint.impl_trait_id; + let def_id = self.create_def(node_id, DefPathData::ImplTrait, constraint.span); + self.resolver.impl_trait_context.insert(def_id, self.impl_trait_context); + } } + ImplTraitContext::TyAliasOpaqueTy => { + if let AssocConstraintKind::Bound { .. } = constraint.kind { + let node_id = constraint.impl_trait_id; + let def_id = self.create_def(node_id, DefPathData::ImplTrait, constraint.span); + self.resolver.impl_trait_context.insert(def_id, self.impl_trait_context); + } + } + _ => {} } + visit::walk_assoc_constraint(self, constraint); } } diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index 9bd660bb62ce2..e9c2ea3c59538 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -166,6 +166,7 @@ enum ImplTraitContext { Universal(LocalDefId), UniversalInDyn(LocalDefId), ReturnOpaquePositionTy, + TyAliasOpaqueTy, } #[derive(Eq)] From 2f17fa21c0dc45b6235b4d44e0e0ec26d3d9e946 Mon Sep 17 00:00:00 2001 From: Peh <20146907+randomicon00@users.noreply.github.com> Date: Wed, 29 Jun 2022 23:04:26 +0100 Subject: [PATCH 30/30] fix: ui tests --- compiler/rustc_ast_lowering/src/lib.rs | 14 ++++++++++---- compiler/rustc_resolve/src/def_collector.rs | 7 ++++++- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index 61d7908b31583..7b6b85ad1843b 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -928,11 +928,17 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { _ => (false, itctx), }; - //if desugar_to_impl_trait - if self.resolver.opt_local_def_id(constraint.impl_trait_id).is_some() { + let desugar = if let ImplTraitContext::Universal = itctx { + self.is_in_dyn_type + } else { + self.resolver.opt_local_def_id(constraint.impl_trait_id).is_some() + }; + //if desugar_to_impl_trait { + //if self.resolver.opt_local_def_id(constraint.impl_trait_id).is_some() { + if desugar { // Desugar `AssocTy: Bounds` into `AssocTy = impl Bounds`. We do this by // constructing the HIR for `impl bounds...` and then lowering that. - debug!("desug111: {:?}, {:?}", desugar_to_impl_trait, itctx); + debug!("desugaring occurs: {:?}, {:?}", desugar_to_impl_trait, itctx); //let parent_def_id = self.current_hir_id_owner; let impl_trait_node_id = constraint.impl_trait_id; @@ -961,7 +967,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { // Desugar `AssocTy: Bounds` into a type binding where the // later desugars into a trait predicate. let bounds = self.lower_param_bounds(bounds, itctx); - debug!("desug1111 NO: {:?}, {:?}", desugar_to_impl_trait, itctx); + debug!("desugaring does not occur: {:?}, {:?}", desugar_to_impl_trait, itctx); hir::TypeBindingKind::Constraint { bounds } } diff --git a/compiler/rustc_resolve/src/def_collector.rs b/compiler/rustc_resolve/src/def_collector.rs index 9b91abb6fee9e..8e156f2d6cb0e 100644 --- a/compiler/rustc_resolve/src/def_collector.rs +++ b/compiler/rustc_resolve/src/def_collector.rs @@ -345,11 +345,16 @@ impl<'a, 'b> visit::Visitor<'a> for DefCollector<'a, 'b> { self.with_parent(parent_def, |this| visit::walk_ty(this, ty)) } - TyKind::TraitObject(..) => { + TyKind::TraitObject(_, TraitObjectSyntax::Dyn) => { self.with_impl_trait(ImplTraitContext::UniversalInDyn(self.parent_def), |this| { visit::walk_ty(this, ty) }); } + TyKind::TraitObject(..) => { + self.with_impl_trait(ImplTraitContext::Universal(self.parent_def), |this| { + visit::walk_ty(this, ty) + }); + } _ => visit::walk_ty(self, ty), } }