-
Notifications
You must be signed in to change notification settings - Fork 173
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
Remove references to smart pointers in the AST API #2867
Conversation
expander.pop_context (); | ||
|
||
auto final_fragment = expander.take_expanded_fragment (); | ||
if (final_fragment.should_expand () | ||
&& final_fragment.is_expression_fragment ()) | ||
expr = final_fragment.take_expression_fragment (); |
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 doesn't seem like this would work, since instances of derived classes of Expr
can have different sizes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right. I believe the only way out is to keep reference to the unique pointer for type/expr expansion.
We could also return the unique_ptr after taking it but it looks more convoluted, I'll keep the first changes.
ac21b32
to
c4a39be
Compare
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.
LGTM :) I think this needs to be rebased
Reference to unique pointers are a known anti pattern, only the element shall be taken by reference instead of the whole wrapper. gcc/rust/ChangeLog: * ast/rust-item.h: Change getter function prototype to return a reference directly instead of a reference to the wrapper type. * checks/errors/rust-ast-validation.cc (ASTValidation::visit): Fix the code to accept references instead. * hir/rust-ast-lower-base.cc (ASTLoweringBase::lower_self): Change function implementation to return a reference. * hir/rust-ast-lower-base.h: Accept a reference instead of a unique pointer reference. * resolve/rust-ast-resolve-item.cc (ResolveItem::visit): Adapt the code to a reference instead of a unique pointer. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
This kind of double indirection is pointless and prone to error. This commit change the api of all getters from the AST to use references directly instead of references to unique pointers. gcc/rust/ChangeLog: * ast/rust-ast-collector.cc (TokenCollector::visit): Remove reference to unique pointer and replace it with a direct reference to the wrapped data. * ast/rust-ast.cc (VariadicParam::as_string): Likewise. (BlockExpr::normalize_tail_expr): Likewise. * ast/rust-expr.h: Likewise and add pointer getter in order to allow pointer reseat. * ast/rust-item.h: Likewise and add pointer getter for reseat. * ast/rust-path.h: Likewise. * ast/rust-pattern.h: Likewise. * ast/rust-stmt.h: Likewise. * ast/rust-type.h: Likewise. * expand/rust-cfg-strip.cc (CfgStrip::maybe_strip_struct_fields): Remove references to unique pointers and replace it with direct references to the wrapped object. (CfgStrip::maybe_strip_tuple_fields): Likewise. (CfgStrip::maybe_strip_generic_args): Likewise. (CfgStrip::maybe_strip_qualified_path_type): Likewise. (CfgStrip::visit): Likewise. * expand/rust-expand-visitor.cc (ExpandVisitor::maybe_expand_expr): Likewise. (ExpandVisitor::maybe_expand_type): Likewise. (ExpandVisitor::visit): Likewise. * expand/rust-expand-visitor.h: Likewise. * hir/rust-ast-lower-base.cc (ASTLoweringBase::lower_binding): Likewise. (ASTLoweringBase::lower_generic_args): Likewise. (ASTLoweringBase::lower_self): Likewise. (ASTLoweringBase::lower_type_no_bounds): Likewise. (ASTLoweringBase::lower_bound): Likewise. (ASTLoweringBase::lower_range_pattern_bound): Likewise. * hir/rust-ast-lower-base.h: Likewise. * hir/rust-ast-lower-block.h: Likewise. * hir/rust-ast-lower-enumitem.h: Likewise. * hir/rust-ast-lower-expr.cc (ASTLoweringExpr::translate): Likewise. (ASTLoweringExpr::visit): Likewise. * hir/rust-ast-lower-expr.h: Likewise. * hir/rust-ast-lower-extern.h: Likewise. * hir/rust-ast-lower-implitem.cc (ASTLowerImplItem::translate): Likewise. (ASTLowerImplItem::visit): Likewise. (ASTLowerTraitItem::translate): Likewise. (ASTLowerTraitItem::visit): Likewise. * hir/rust-ast-lower-implitem.h: Likewise. * hir/rust-ast-lower-item.cc (ASTLoweringItem::translate): Likewise. (ASTLoweringItem::visit): Likewise. * hir/rust-ast-lower-item.h: Likewise. * hir/rust-ast-lower-pattern.cc (ASTLoweringPattern::translate): Likewise. (ASTLoweringPattern::visit): Likewise. * hir/rust-ast-lower-pattern.h: Likewise. * hir/rust-ast-lower-stmt.cc (ASTLoweringStmt::visit): Likewise. * hir/rust-ast-lower-struct-field-expr.h: Likewise. * hir/rust-ast-lower-type.cc (ASTLowerTypePath::visit): Likewise. (ASTLowerQualifiedPathInType::visit): Likewise. (ASTLoweringType::translate): Likewise. (ASTLoweringType::visit): Likewise. (ASTLowerGenericParam::translate): Likewise. (ASTLowerGenericParam::visit): Likewise. (ASTLoweringTypeBounds::translate): Likewise. (ASTLoweringTypeBounds::visit): Likewise. (ASTLowerWhereClauseItem::visit): Likewise. * hir/rust-ast-lower-type.h: Likewise. * hir/rust-ast-lower.cc (ASTLowering::go): Likewise. (ASTLoweringBlock::visit): Likewise. (ASTLoweringIfBlock::visit): Likewise. (ASTLoweringIfLetBlock::visit): Likewise. (ASTLowerStructExprField::visit): Likewise. (ASTLoweringExprWithBlock::visit): Likewise. (ASTLoweringBase::lower_qual_path_type): Likewise. (ASTLoweringBase::lower_closure_param): Likewise. * resolve/rust-ast-resolve-base.cc (ResolverBase::resolve_visibility): Likewise. * resolve/rust-ast-resolve-expr.cc (ResolveExpr::go): Likewise. (ResolveExpr::visit): Likewise. (ResolveExpr::resolve_closure_param): Likewise. * resolve/rust-ast-resolve-expr.h: Likewise. * resolve/rust-ast-resolve-implitem.h: Likewise. * resolve/rust-ast-resolve-item.cc (ResolveTraitItems::visit): Likewise. (ResolveItem::go): Likewise. (ResolveItem::visit): Likewise. (ResolveItem::resolve_impl_item): Likewise. (ResolveItem::resolve_extern_item): Likewise. (ResolveImplItems::go): Likewise. (ResolveExternItem::go): Likewise. (ResolveExternItem::visit): Likewise. * resolve/rust-ast-resolve-item.h: Likewise. * resolve/rust-ast-resolve-path.cc (ResolvePath::go): Likewise. (ResolvePath::resolve_path): Likewise. * resolve/rust-ast-resolve-path.h: Likewise. * resolve/rust-ast-resolve-pattern.cc (PatternDeclaration::go): Likewise. (PatternDeclaration::visit): Likewise. (resolve_range_pattern_bound): Likewise. * resolve/rust-ast-resolve-pattern.h: Likewise. * resolve/rust-ast-resolve-stmt.cc (ResolveStmt::visit): Likewise. * resolve/rust-ast-resolve-stmt.h: Likewise. * resolve/rust-ast-resolve-struct-expr-field.cc (ResolveStructExprField::go): Likewise. (ResolveStructExprField::visit): Likewise. * resolve/rust-ast-resolve-struct-expr-field.h: Likewise. * resolve/rust-ast-resolve-toplevel.h: Likewise. * resolve/rust-ast-resolve-type.cc (ResolveType::visit): Likewise. (ResolveRelativeTypePath::go): Likewise. (ResolveRelativeQualTypePath::resolve_qual_seg): Likewise. (ResolveTypeToCanonicalPath::go): Likewise. (ResolveTypeToCanonicalPath::visit): Likewise. (ResolveGenericArgs::resolve_disambiguated_generic): Likewise. (ResolveGenericArgs::go): Likewise. * resolve/rust-ast-resolve-type.h: Likewise. * resolve/rust-ast-resolve.cc (NameResolution::go): Likewise. * resolve/rust-default-resolver.cc (DefaultResolver::visit): Likewise. * resolve/rust-early-name-resolver.cc (EarlyNameResolver::resolve_qualified_path_type): Likewise. (EarlyNameResolver::visit): Likewise. * resolve/rust-toplevel-name-resolver-2.0.cc (TopLevel::visit): Likewise. * checks/errors/rust-ast-validation.cc: Likewise. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
137f8c1
to
e8cc906
Compare
Remove references to smart pointer as those could be replaced with references to the underlying object.