Remove AstDeref.
It's a "utility trait to reduce boilerplate" implemented for `P` and `AstNodeWrapper`, but removing it gives a net reduction of twenty lines of code. It's also simpler to just implement `HasNodeId`/`HasAttrs`/`HasTokens` directly on types instead of via `AstDeref`. (I decided to make this change when doing some related refactoring and the error messages involving `AstDeref` and `HasAttrs` were hard to understand; removing it helped a lot.)
This commit is contained in:
@@ -308,7 +308,6 @@ impl ParenthesizedArgs {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
use crate::AstDeref;
|
|
||||||
pub use crate::node_id::{CRATE_NODE_ID, DUMMY_NODE_ID, NodeId};
|
pub use crate::node_id::{CRATE_NODE_ID, DUMMY_NODE_ID, NodeId};
|
||||||
|
|
||||||
/// Modifiers on a trait bound like `~const`, `?` and `!`.
|
/// Modifiers on a trait bound like `~const`, `?` and `!`.
|
||||||
@@ -2349,7 +2348,7 @@ impl Ty {
|
|||||||
pub fn is_maybe_parenthesised_infer(&self) -> bool {
|
pub fn is_maybe_parenthesised_infer(&self) -> bool {
|
||||||
match &self.kind {
|
match &self.kind {
|
||||||
TyKind::Infer => true,
|
TyKind::Infer => true,
|
||||||
TyKind::Paren(inner) => inner.ast_deref().is_maybe_parenthesised_infer(),
|
TyKind::Paren(inner) => inner.is_maybe_parenthesised_infer(),
|
||||||
_ => false,
|
_ => false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,34 +13,6 @@ use crate::{
|
|||||||
Ty, Variant, Visibility, WherePredicate,
|
Ty, Variant, Visibility, WherePredicate,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// A utility trait to reduce boilerplate.
|
|
||||||
/// Standard `Deref(Mut)` cannot be reused due to coherence.
|
|
||||||
pub trait AstDeref {
|
|
||||||
type Target;
|
|
||||||
fn ast_deref(&self) -> &Self::Target;
|
|
||||||
fn ast_deref_mut(&mut self) -> &mut Self::Target;
|
|
||||||
}
|
|
||||||
|
|
||||||
macro_rules! impl_not_ast_deref {
|
|
||||||
($($T:ty),+ $(,)?) => {
|
|
||||||
$(
|
|
||||||
impl !AstDeref for $T {}
|
|
||||||
)+
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
impl_not_ast_deref!(AssocItem, Expr, ForeignItem, Item, Stmt);
|
|
||||||
|
|
||||||
impl<T> AstDeref for P<T> {
|
|
||||||
type Target = T;
|
|
||||||
fn ast_deref(&self) -> &Self::Target {
|
|
||||||
self
|
|
||||||
}
|
|
||||||
fn ast_deref_mut(&mut self) -> &mut Self::Target {
|
|
||||||
self
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A trait for AST nodes having an ID.
|
/// A trait for AST nodes having an ID.
|
||||||
pub trait HasNodeId {
|
pub trait HasNodeId {
|
||||||
fn node_id(&self) -> NodeId;
|
fn node_id(&self) -> NodeId;
|
||||||
@@ -81,12 +53,12 @@ impl_has_node_id!(
|
|||||||
WherePredicate,
|
WherePredicate,
|
||||||
);
|
);
|
||||||
|
|
||||||
impl<T: AstDeref<Target: HasNodeId>> HasNodeId for T {
|
impl<T: HasNodeId> HasNodeId for P<T> {
|
||||||
fn node_id(&self) -> NodeId {
|
fn node_id(&self) -> NodeId {
|
||||||
self.ast_deref().node_id()
|
(**self).node_id()
|
||||||
}
|
}
|
||||||
fn node_id_mut(&mut self) -> &mut NodeId {
|
fn node_id_mut(&mut self) -> &mut NodeId {
|
||||||
self.ast_deref_mut().node_id_mut()
|
(**self).node_id_mut()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -138,15 +110,6 @@ impl_has_tokens_none!(
|
|||||||
WherePredicate
|
WherePredicate
|
||||||
);
|
);
|
||||||
|
|
||||||
impl<T: AstDeref<Target: HasTokens>> HasTokens for T {
|
|
||||||
fn tokens(&self) -> Option<&LazyAttrTokenStream> {
|
|
||||||
self.ast_deref().tokens()
|
|
||||||
}
|
|
||||||
fn tokens_mut(&mut self) -> Option<&mut Option<LazyAttrTokenStream>> {
|
|
||||||
self.ast_deref_mut().tokens_mut()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T: HasTokens> HasTokens for Option<T> {
|
impl<T: HasTokens> HasTokens for Option<T> {
|
||||||
fn tokens(&self) -> Option<&LazyAttrTokenStream> {
|
fn tokens(&self) -> Option<&LazyAttrTokenStream> {
|
||||||
self.as_ref().and_then(|inner| inner.tokens())
|
self.as_ref().and_then(|inner| inner.tokens())
|
||||||
@@ -156,6 +119,15 @@ impl<T: HasTokens> HasTokens for Option<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T: HasTokens> HasTokens for P<T> {
|
||||||
|
fn tokens(&self) -> Option<&LazyAttrTokenStream> {
|
||||||
|
(**self).tokens()
|
||||||
|
}
|
||||||
|
fn tokens_mut(&mut self) -> Option<&mut Option<LazyAttrTokenStream>> {
|
||||||
|
(**self).tokens_mut()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl HasTokens for StmtKind {
|
impl HasTokens for StmtKind {
|
||||||
fn tokens(&self) -> Option<&LazyAttrTokenStream> {
|
fn tokens(&self) -> Option<&LazyAttrTokenStream> {
|
||||||
match self {
|
match self {
|
||||||
@@ -273,13 +245,13 @@ impl_has_attrs!(
|
|||||||
);
|
);
|
||||||
impl_has_attrs_none!(Attribute, AttrItem, Block, Pat, Path, Ty, Visibility);
|
impl_has_attrs_none!(Attribute, AttrItem, Block, Pat, Path, Ty, Visibility);
|
||||||
|
|
||||||
impl<T: AstDeref<Target: HasAttrs>> HasAttrs for T {
|
impl<T: HasAttrs> HasAttrs for P<T> {
|
||||||
const SUPPORTS_CUSTOM_INNER_ATTRS: bool = T::Target::SUPPORTS_CUSTOM_INNER_ATTRS;
|
const SUPPORTS_CUSTOM_INNER_ATTRS: bool = T::SUPPORTS_CUSTOM_INNER_ATTRS;
|
||||||
fn attrs(&self) -> &[Attribute] {
|
fn attrs(&self) -> &[Attribute] {
|
||||||
self.ast_deref().attrs()
|
(**self).attrs()
|
||||||
}
|
}
|
||||||
fn visit_attrs(&mut self, f: impl FnOnce(&mut AttrVec)) {
|
fn visit_attrs(&mut self, f: impl FnOnce(&mut AttrVec)) {
|
||||||
self.ast_deref_mut().visit_attrs(f)
|
(**self).visit_attrs(f);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -343,13 +315,22 @@ impl<Wrapped, Tag> AstNodeWrapper<Wrapped, Tag> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Wrapped, Tag> AstDeref for AstNodeWrapper<Wrapped, Tag> {
|
impl<Wrapped: HasNodeId, Tag> HasNodeId for AstNodeWrapper<Wrapped, Tag> {
|
||||||
type Target = Wrapped;
|
fn node_id(&self) -> NodeId {
|
||||||
fn ast_deref(&self) -> &Self::Target {
|
self.wrapped.node_id()
|
||||||
&self.wrapped
|
|
||||||
}
|
}
|
||||||
fn ast_deref_mut(&mut self) -> &mut Self::Target {
|
fn node_id_mut(&mut self) -> &mut NodeId {
|
||||||
&mut self.wrapped
|
self.wrapped.node_id_mut()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<Wrapped: HasAttrs, Tag> HasAttrs for AstNodeWrapper<Wrapped, Tag> {
|
||||||
|
const SUPPORTS_CUSTOM_INNER_ATTRS: bool = Wrapped::SUPPORTS_CUSTOM_INNER_ATTRS;
|
||||||
|
fn attrs(&self) -> &[Attribute] {
|
||||||
|
self.wrapped.attrs()
|
||||||
|
}
|
||||||
|
fn visit_attrs(&mut self, f: impl FnOnce(&mut AttrVec)) {
|
||||||
|
self.wrapped.visit_attrs(f);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ pub mod tokenstream;
|
|||||||
pub mod visit;
|
pub mod visit;
|
||||||
|
|
||||||
pub use self::ast::*;
|
pub use self::ast::*;
|
||||||
pub use self::ast_traits::{AstDeref, AstNodeWrapper, HasAttrs, HasNodeId, HasTokens};
|
pub use self::ast_traits::{AstNodeWrapper, HasAttrs, HasNodeId, HasTokens};
|
||||||
|
|
||||||
/// Requirements for a `StableHashingContext` to be used in this crate.
|
/// Requirements for a `StableHashingContext` to be used in this crate.
|
||||||
/// This is a hack to allow using the `HashStable_Generic` derive macro
|
/// This is a hack to allow using the `HashStable_Generic` derive macro
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ use std::env::VarError;
|
|||||||
|
|
||||||
use rustc_ast::token::{self, LitKind};
|
use rustc_ast::token::{self, LitKind};
|
||||||
use rustc_ast::tokenstream::TokenStream;
|
use rustc_ast::tokenstream::TokenStream;
|
||||||
use rustc_ast::{AstDeref, ExprKind, GenericArg, Mutability};
|
use rustc_ast::{ExprKind, GenericArg, Mutability};
|
||||||
use rustc_expand::base::{DummyResult, ExpandResult, ExtCtxt, MacEager, MacroExpanderResult};
|
use rustc_expand::base::{DummyResult, ExpandResult, ExtCtxt, MacEager, MacroExpanderResult};
|
||||||
use rustc_span::{Ident, Span, Symbol, kw, sym};
|
use rustc_span::{Ident, Span, Symbol, kw, sym};
|
||||||
use thin_vec::thin_vec;
|
use thin_vec::thin_vec;
|
||||||
@@ -148,13 +148,13 @@ pub(crate) fn expand_env<'cx>(
|
|||||||
cx.dcx().emit_err(errors::EnvNotDefined::CargoEnvVar {
|
cx.dcx().emit_err(errors::EnvNotDefined::CargoEnvVar {
|
||||||
span,
|
span,
|
||||||
var: *symbol,
|
var: *symbol,
|
||||||
var_expr: var_expr.ast_deref(),
|
var_expr: &var_expr,
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
cx.dcx().emit_err(errors::EnvNotDefined::CustomEnvVar {
|
cx.dcx().emit_err(errors::EnvNotDefined::CustomEnvVar {
|
||||||
span,
|
span,
|
||||||
var: *symbol,
|
var: *symbol,
|
||||||
var_expr: var_expr.ast_deref(),
|
var_expr: &var_expr,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user