diff --git a/compiler/rustc_ast/src/ast.rs b/compiler/rustc_ast/src/ast.rs
index 4489a424c0d5..11afd359e5ad 100644
--- a/compiler/rustc_ast/src/ast.rs
+++ b/compiler/rustc_ast/src/ast.rs
@@ -710,6 +710,12 @@ impl Pat {
}
}
+impl From
> for Pat {
+ fn from(value: P) -> Self {
+ *value
+ }
+}
+
/// A single field in a struct pattern.
///
/// Patterns like the fields of `Foo { x, ref y, ref mut z }`
@@ -1553,17 +1559,23 @@ impl Expr {
)
}
- /// Creates a dummy `P`.
+ /// Creates a dummy `Expr`.
///
/// Should only be used when it will be replaced afterwards or as a return value when an error was encountered.
- pub fn dummy() -> P {
- P(Expr {
+ pub fn dummy() -> Expr {
+ Expr {
id: DUMMY_NODE_ID,
kind: ExprKind::Dummy,
span: DUMMY_SP,
attrs: ThinVec::new(),
tokens: None,
- })
+ }
+ }
+}
+
+impl From> for Expr {
+ fn from(value: P) -> Self {
+ *value
}
}
@@ -2374,6 +2386,12 @@ impl Clone for Ty {
}
}
+impl From> for Ty {
+ fn from(value: P) -> Self {
+ *value
+ }
+}
+
impl Ty {
pub fn peel_refs(&self) -> &Self {
let mut final_ty = self;
diff --git a/compiler/rustc_ast/src/mut_visit.rs b/compiler/rustc_ast/src/mut_visit.rs
index 71a47dcfcba2..07fbe8045fc2 100644
--- a/compiler/rustc_ast/src/mut_visit.rs
+++ b/compiler/rustc_ast/src/mut_visit.rs
@@ -168,7 +168,7 @@ pub trait MutVisitor: Sized + MutVisitorResult {
walk_flat_map_arm(self, arm)
}
- fn visit_pat(&mut self, p: &mut P) {
+ fn visit_pat(&mut self, p: &mut Pat) {
walk_pat(self, p);
}
@@ -176,7 +176,7 @@ pub trait MutVisitor: Sized + MutVisitorResult {
walk_anon_const(self, c);
}
- fn visit_expr(&mut self, e: &mut P) {
+ fn visit_expr(&mut self, e: &mut Expr) {
walk_expr(self, e);
}
@@ -194,7 +194,7 @@ pub trait MutVisitor: Sized + MutVisitorResult {
walk_generic_arg(self, arg);
}
- fn visit_ty(&mut self, t: &mut P) {
+ fn visit_ty(&mut self, t: &mut Ty) {
walk_ty(self, t);
}
diff --git a/compiler/rustc_builtin_macros/src/cfg_eval.rs b/compiler/rustc_builtin_macros/src/cfg_eval.rs
index da01e3e9607b..fe44350863c9 100644
--- a/compiler/rustc_builtin_macros/src/cfg_eval.rs
+++ b/compiler/rustc_builtin_macros/src/cfg_eval.rs
@@ -155,7 +155,7 @@ impl CfgEval<'_> {
impl MutVisitor for CfgEval<'_> {
#[instrument(level = "trace", skip(self))]
- fn visit_expr(&mut self, expr: &mut P) {
+ fn visit_expr(&mut self, expr: &mut ast::Expr) {
self.0.configure_expr(expr, false);
mut_visit::walk_expr(self, expr);
}
diff --git a/compiler/rustc_builtin_macros/src/deriving/coerce_pointee.rs b/compiler/rustc_builtin_macros/src/deriving/coerce_pointee.rs
index 0794192621a9..3a20b39798d7 100644
--- a/compiler/rustc_builtin_macros/src/deriving/coerce_pointee.rs
+++ b/compiler/rustc_builtin_macros/src/deriving/coerce_pointee.rs
@@ -1,5 +1,4 @@
use ast::HasAttrs;
-use ast::ptr::P;
use rustc_ast::mut_visit::MutVisitor;
use rustc_ast::visit::BoundKind;
use rustc_ast::{
@@ -378,11 +377,11 @@ struct TypeSubstitution<'a> {
}
impl<'a> ast::mut_visit::MutVisitor for TypeSubstitution<'a> {
- fn visit_ty(&mut self, ty: &mut P) {
+ fn visit_ty(&mut self, ty: &mut ast::Ty) {
if let Some(name) = ty.kind.is_simple_path()
&& name == self.from_name
{
- **ty = self.to_ty.clone();
+ *ty = self.to_ty.clone();
self.rewritten = true;
} else {
ast::mut_visit::walk_ty(self, ty);
diff --git a/compiler/rustc_expand/src/expand.rs b/compiler/rustc_expand/src/expand.rs
index 3cfeb01ea477..9fd524ef45cd 100644
--- a/compiler/rustc_expand/src/expand.rs
+++ b/compiler/rustc_expand/src/expand.rs
@@ -1768,7 +1768,7 @@ impl InvocationCollectorNode for ast::Crate {
}
}
-impl InvocationCollectorNode for P {
+impl InvocationCollectorNode for ast::Ty {
type OutputTy = P;
const KIND: AstFragmentKind = AstFragmentKind::Ty;
fn to_annotatable(self) -> Annotatable {
@@ -1791,7 +1791,7 @@ impl InvocationCollectorNode for P {
}
}
-impl InvocationCollectorNode for P {
+impl InvocationCollectorNode for ast::Pat {
type OutputTy = P;
const KIND: AstFragmentKind = AstFragmentKind::Pat;
fn to_annotatable(self) -> Annotatable {
@@ -1814,11 +1814,11 @@ impl InvocationCollectorNode for P {
}
}
-impl InvocationCollectorNode for P {
+impl InvocationCollectorNode for ast::Expr {
type OutputTy = P;
const KIND: AstFragmentKind = AstFragmentKind::Expr;
fn to_annotatable(self) -> Annotatable {
- Annotatable::Expr(self)
+ Annotatable::Expr(P(self))
}
fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
fragment.make_expr()
@@ -1955,29 +1955,29 @@ impl DummyAstNode for ast::Crate {
}
}
-impl DummyAstNode for P {
+impl DummyAstNode for ast::Ty {
fn dummy() -> Self {
- P(ast::Ty {
+ ast::Ty {
id: DUMMY_NODE_ID,
kind: TyKind::Dummy,
span: Default::default(),
tokens: Default::default(),
- })
+ }
}
}
-impl DummyAstNode for P {
+impl DummyAstNode for ast::Pat {
fn dummy() -> Self {
- P(ast::Pat {
+ ast::Pat {
id: DUMMY_NODE_ID,
kind: PatKind::Wild,
span: Default::default(),
tokens: Default::default(),
- })
+ }
}
}
-impl DummyAstNode for P {
+impl DummyAstNode for ast::Expr {
fn dummy() -> Self {
ast::Expr::dummy()
}
@@ -1985,7 +1985,7 @@ impl DummyAstNode for P {
impl DummyAstNode for AstNodeWrapper, MethodReceiverTag> {
fn dummy() -> Self {
- AstNodeWrapper::new(ast::Expr::dummy(), MethodReceiverTag)
+ AstNodeWrapper::new(P(ast::Expr::dummy()), MethodReceiverTag)
}
}
@@ -2272,7 +2272,7 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
}
}
- fn visit_node + DummyAstNode>(
+ fn visit_node> + DummyAstNode>(
&mut self,
node: &mut Node,
) {
@@ -2297,6 +2297,7 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
*node = self
.collect_attr((attr, pos, derives), n.to_annotatable(), Node::KIND)
.make_ast::()
+ .into()
}
},
None if node.is_mac_call() => {
@@ -2304,7 +2305,7 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
let (mac, attrs, _) = n.take_mac_call();
self.check_attributes(&attrs, &mac);
- *node = self.collect_bang(mac, Node::KIND).make_ast::()
+ *node = self.collect_bang(mac, Node::KIND).make_ast::().into()
}
None if node.delegation().is_some() => unreachable!(),
None => {
@@ -2414,15 +2415,15 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
self.visit_node(node)
}
- fn visit_ty(&mut self, node: &mut P) {
+ fn visit_ty(&mut self, node: &mut ast::Ty) {
self.visit_node(node)
}
- fn visit_pat(&mut self, node: &mut P) {
+ fn visit_pat(&mut self, node: &mut ast::Pat) {
self.visit_node(node)
}
- fn visit_expr(&mut self, node: &mut P) {
+ fn visit_expr(&mut self, node: &mut ast::Expr) {
// FIXME: Feature gating is performed inconsistently between `Expr` and `OptExpr`.
if let Some(attr) = node.attrs.first() {
self.cfg().maybe_emit_expr_attr_err(attr);
diff --git a/compiler/rustc_expand/src/placeholders.rs b/compiler/rustc_expand/src/placeholders.rs
index 3dcb20c8c768..2c486a02bdf1 100644
--- a/compiler/rustc_expand/src/placeholders.rs
+++ b/compiler/rustc_expand/src/placeholders.rs
@@ -332,9 +332,9 @@ impl MutVisitor for PlaceholderExpander {
}
}
- fn visit_expr(&mut self, expr: &mut P) {
+ fn visit_expr(&mut self, expr: &mut ast::Expr) {
match expr.kind {
- ast::ExprKind::MacCall(_) => *expr = self.remove(expr.id).make_expr(),
+ ast::ExprKind::MacCall(_) => *expr = *self.remove(expr.id).make_expr(),
_ => walk_expr(self, expr),
}
}
@@ -399,16 +399,16 @@ impl MutVisitor for PlaceholderExpander {
stmts
}
- fn visit_pat(&mut self, pat: &mut P) {
+ fn visit_pat(&mut self, pat: &mut ast::Pat) {
match pat.kind {
- ast::PatKind::MacCall(_) => *pat = self.remove(pat.id).make_pat(),
+ ast::PatKind::MacCall(_) => *pat = *self.remove(pat.id).make_pat(),
_ => walk_pat(self, pat),
}
}
- fn visit_ty(&mut self, ty: &mut P) {
+ fn visit_ty(&mut self, ty: &mut ast::Ty) {
match ty.kind {
- ast::TyKind::MacCall(_) => *ty = self.remove(ty.id).make_ty(),
+ ast::TyKind::MacCall(_) => *ty = *self.remove(ty.id).make_ty(),
_ => walk_ty(self, ty),
}
}
diff --git a/compiler/rustc_parse/src/parser/expr.rs b/compiler/rustc_parse/src/parser/expr.rs
index 93489aa8ee94..93c76c47f060 100644
--- a/compiler/rustc_parse/src/parser/expr.rs
+++ b/compiler/rustc_parse/src/parser/expr.rs
@@ -4087,7 +4087,7 @@ impl<'a> CondChecker<'a> {
}
impl MutVisitor for CondChecker<'_> {
- fn visit_expr(&mut self, e: &mut P) {
+ fn visit_expr(&mut self, e: &mut Expr) {
self.depth += 1;
use ForbiddenLetReason::*;
diff --git a/compiler/rustc_parse/src/parser/pat.rs b/compiler/rustc_parse/src/parser/pat.rs
index 7a226136e235..64653ee2a04c 100644
--- a/compiler/rustc_parse/src/parser/pat.rs
+++ b/compiler/rustc_parse/src/parser/pat.rs
@@ -1094,7 +1094,7 @@ impl<'a> Parser<'a> {
fn make_all_value_bindings_mutable(pat: &mut P) -> bool {
struct AddMut(bool);
impl MutVisitor for AddMut {
- fn visit_pat(&mut self, pat: &mut P) {
+ fn visit_pat(&mut self, pat: &mut Pat) {
if let PatKind::Ident(BindingMode(ByRef::No, m @ Mutability::Not), ..) =
&mut pat.kind
{
diff --git a/src/tools/clippy/clippy_lints/src/unnested_or_patterns.rs b/src/tools/clippy/clippy_lints/src/unnested_or_patterns.rs
index b839b6f56728..bd8420917f5e 100644
--- a/src/tools/clippy/clippy_lints/src/unnested_or_patterns.rs
+++ b/src/tools/clippy/clippy_lints/src/unnested_or_patterns.rs
@@ -128,7 +128,7 @@ fn remove_all_parens(pat: &mut P) {
}
impl MutVisitor for Visitor {
- fn visit_pat(&mut self, pat: &mut P) {
+ fn visit_pat(&mut self, pat: &mut Pat) {
let is_inner = mem::replace(&mut self.is_inner, true);
walk_pat(self, pat);
let inner = match &mut pat.kind {
@@ -145,7 +145,7 @@ fn remove_all_parens(pat: &mut P) {
fn insert_necessary_parens(pat: &mut P) {
struct Visitor;
impl MutVisitor for Visitor {
- fn visit_pat(&mut self, pat: &mut P) {
+ fn visit_pat(&mut self, pat: &mut Pat) {
use ast::BindingMode;
walk_pat(self, pat);
let target = match &mut pat.kind {
@@ -167,7 +167,7 @@ fn unnest_or_patterns(pat: &mut P) -> bool {
changed: bool,
}
impl MutVisitor for Visitor {
- fn visit_pat(&mut self, p: &mut P) {
+ fn visit_pat(&mut self, p: &mut Pat) {
// This is a bottom up transformation, so recurse first.
walk_pat(self, p);
diff --git a/tests/ui-fulldeps/pprust-expr-roundtrip.rs b/tests/ui-fulldeps/pprust-expr-roundtrip.rs
index f5cfa9e0bccf..8bca20852add 100644
--- a/tests/ui-fulldeps/pprust-expr-roundtrip.rs
+++ b/tests/ui-fulldeps/pprust-expr-roundtrip.rs
@@ -187,9 +187,9 @@ fn iter_exprs(depth: usize, f: &mut dyn FnMut(P)) {
struct RemoveParens;
impl MutVisitor for RemoveParens {
- fn visit_expr(&mut self, e: &mut P) {
+ fn visit_expr(&mut self, e: &mut Expr) {
match e.kind.clone() {
- ExprKind::Paren(inner) => *e = inner,
+ ExprKind::Paren(inner) => *e = *inner,
_ => {}
};
mut_visit::walk_expr(self, e);
@@ -200,11 +200,11 @@ impl MutVisitor for RemoveParens {
struct AddParens;
impl MutVisitor for AddParens {
- fn visit_expr(&mut self, e: &mut P) {
+ fn visit_expr(&mut self, e: &mut Expr) {
mut_visit::walk_expr(self, e);
let expr = std::mem::replace(e, Expr::dummy());
- e.kind = ExprKind::Paren(expr);
+ e.kind = ExprKind::Paren(P(expr));
}
}
diff --git a/tests/ui-fulldeps/pprust-parenthesis-insertion.rs b/tests/ui-fulldeps/pprust-parenthesis-insertion.rs
index 08bed40abe86..90e07bed40e7 100644
--- a/tests/ui-fulldeps/pprust-parenthesis-insertion.rs
+++ b/tests/ui-fulldeps/pprust-parenthesis-insertion.rs
@@ -43,7 +43,6 @@ use std::process::ExitCode;
use parser::parse_expr;
use rustc_ast::ast::{Expr, ExprKind};
use rustc_ast::mut_visit::{self, MutVisitor};
-use rustc_ast::ptr::P;
use rustc_ast_pretty::pprust;
use rustc_session::parse::ParseSess;
@@ -157,7 +156,7 @@ static EXPRS: &[&str] = &[
struct Unparenthesize;
impl MutVisitor for Unparenthesize {
- fn visit_expr(&mut self, e: &mut P) {
+ fn visit_expr(&mut self, e: &mut Expr) {
while let ExprKind::Paren(paren) = &mut e.kind {
*e = mem::replace(paren, Expr::dummy());
}