Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 11 additions & 26 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -651,8 +651,8 @@ impl Pat {
PatKind::MacCall(mac) => TyKind::MacCall(mac.clone()),
// `&mut? P` can be reinterpreted as `&mut? T` where `T` is `P` reparsed as a type.
PatKind::Ref(pat, pinned, mutbl) => pat.to_ty().map(|ty| match pinned {
Pinnedness::Not => TyKind::Ref(None, MutTy { ty, mutbl: *mutbl }),
Pinnedness::Pinned => TyKind::PinnedRef(None, MutTy { ty, mutbl: *mutbl }),
Pinnedness::Not => TyKind::Ref(None, ty, *mutbl),
Pinnedness::Pinned => TyKind::PinnedRef(None, ty, *mutbl),
})?,
// A slice/array pattern `[P]` can be reparsed as `[T]`, an unsized array,
// when `P` can be reparsed as a type `T`.
Expand Down Expand Up @@ -1507,7 +1507,7 @@ impl Expr {
ExprKind::Paren(expr) => expr.to_ty().map(TyKind::Paren)?,

ExprKind::AddrOf(BorrowKind::Ref, mutbl, expr) => {
expr.to_ty().map(|ty| TyKind::Ref(None, MutTy { ty, mutbl: *mutbl }))?
expr.to_ty().map(|ty| TyKind::Ref(None, ty, *mutbl))?
}

ExprKind::Repeat(expr, expr_len) => {
Expand Down Expand Up @@ -2336,14 +2336,6 @@ impl LitKind {
}
}

// N.B., If you change this, you'll probably want to change the corresponding
// type structure in `middle/ty.rs` as well.
#[derive(Clone, Encodable, Decodable, Debug, Walkable)]
pub struct MutTy {
pub ty: Box<Ty>,
pub mutbl: Mutability,
}

/// Represents a function's signature in a trait declaration,
/// trait implementation, or free function.
#[derive(Clone, Encodable, Decodable, Debug)]
Expand Down Expand Up @@ -2470,8 +2462,7 @@ impl From<Box<Ty>> for Ty {
impl Ty {
pub fn peel_refs(&self) -> &Self {
let mut final_ty = self;
while let TyKind::Ref(_, MutTy { ty, .. }) | TyKind::Ptr(MutTy { ty, .. }) = &final_ty.kind
{
while let TyKind::Ref(_, ty, _) | TyKind::Ptr(ty, ..) = &final_ty.kind {
final_ty = ty;
}
final_ty
Expand Down Expand Up @@ -2528,13 +2519,13 @@ pub enum TyKind {
/// A fixed length array (`[T; n]`).
Array(Box<Ty>, AnonConst),
/// A raw pointer (`*const T` or `*mut T`).
Ptr(MutTy),
Ptr(Box<Ty>, Mutability),
/// A reference (`&'a T` or `&'a mut T`).
Ref(#[visitable(extra = LifetimeCtxt::Ref)] Option<Lifetime>, MutTy),
Ref(#[visitable(extra = LifetimeCtxt::Ref)] Option<Lifetime>, Box<Ty>, Mutability),
/// A pinned reference (`&'a pin const T` or `&'a pin mut T`).
///
/// Desugars into `Pin<&'a T>` or `Pin<&'a mut T>`.
PinnedRef(#[visitable(extra = LifetimeCtxt::Ref)] Option<Lifetime>, MutTy),
PinnedRef(#[visitable(extra = LifetimeCtxt::Ref)] Option<Lifetime>, Box<Ty>, Mutability),
/// A function pointer type (e.g., `fn(usize) -> bool`).
FnPtr(Box<FnPtrTy>),
/// An unsafe existential lifetime binder (e.g., `unsafe<'a> &'a ()`).
Expand Down Expand Up @@ -2979,12 +2970,10 @@ impl Param {
if ident.name == kw::SelfLower {
return match self.ty.kind {
TyKind::ImplicitSelf => Some(respan(self.pat.span, SelfKind::Value(mutbl))),
TyKind::Ref(lt, MutTy { ref ty, mutbl }) if ty.kind.is_implicit_self() => {
TyKind::Ref(lt, ref ty, mutbl) if ty.kind.is_implicit_self() => {
Some(respan(self.pat.span, SelfKind::Region(lt, mutbl)))
}
TyKind::PinnedRef(lt, MutTy { ref ty, mutbl })
if ty.kind.is_implicit_self() =>
{
TyKind::PinnedRef(lt, ref ty, mutbl) if ty.kind.is_implicit_self() => {
Some(respan(self.pat.span, SelfKind::Pinned(lt, mutbl)))
}
_ => Some(respan(
Expand Down Expand Up @@ -3016,17 +3005,13 @@ impl Param {
SelfKind::Value(mutbl) => (mutbl, infer_ty),
SelfKind::Region(lt, mutbl) => (
Mutability::Not,
Box::new(Ty {
id: DUMMY_NODE_ID,
kind: TyKind::Ref(lt, MutTy { ty: infer_ty, mutbl }),
span,
}),
Box::new(Ty { id: DUMMY_NODE_ID, kind: TyKind::Ref(lt, infer_ty, mutbl), span }),
),
SelfKind::Pinned(lt, mutbl) => (
Mutability::Not,
Box::new(Ty {
id: DUMMY_NODE_ID,
kind: TyKind::PinnedRef(lt, MutTy { ty: infer_ty, mutbl }),
kind: TyKind::PinnedRef(lt, infer_ty, mutbl),
span,
}),
),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast/src/expand/autodiff_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ pub fn valid_ty_for_activity(ty: &Box<Ty>, activity: DiffActivity) -> bool {
if matches!(activity, Active | ActiveOnly) {
return true;
}
matches!(ty.kind, TyKind::Ptr(_) | TyKind::Ref(..))
matches!(ty.kind, TyKind::Ptr(..) | TyKind::Ref(..))
&& matches!(activity, Duplicated | DuplicatedOnly)
}
pub fn valid_input_activity(mode: DiffMode, activity: DiffActivity) -> bool {
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_ast/src/util/classify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,10 @@ fn type_trailing_braced_mac_call(mut ty: &ast::Ty) -> Option<&ast::MacCall> {
break (mac.args.delim == Delimiter::Brace).then_some(mac);
}

ast::TyKind::Ptr(mut_ty)
| ast::TyKind::Ref(_, mut_ty)
| ast::TyKind::PinnedRef(_, mut_ty) => {
ty = &mut_ty.ty;
ast::TyKind::Ptr(inner_ty, _)
| ast::TyKind::Ref(_, inner_ty, _)
| ast::TyKind::PinnedRef(_, inner_ty, _) => {
ty = inner_ty;
}

ast::TyKind::UnsafeBinder(binder) => {
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_ast/src/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,6 @@ macro_rules! common_visitor_and_walkers {
MethodCall,
ModKind,
ModSpans,
MutTy,
NormalAttr,
Parens,
ParenthesizedArgs,
Expand Down
20 changes: 8 additions & 12 deletions compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1527,14 +1527,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
TyKind::Infer => hir::TyKind::Infer(()),
TyKind::Err(guar) => hir::TyKind::Err(*guar),
TyKind::Slice(ty) => hir::TyKind::Slice(self.lower_ty_alloc(ty, itctx)),
TyKind::Ptr(mt) => hir::TyKind::Ptr(self.lower_mt(mt, itctx)),
TyKind::Ref(region, mt) => {
TyKind::Ptr(ty, mutbl) => hir::TyKind::Ptr(self.lower_ty_alloc(ty, itctx), *mutbl),
TyKind::Ref(region, ty, mutbl) => {
let lifetime = self.lower_ty_direct_lifetime(t, *region);
hir::TyKind::Ref(lifetime, self.lower_mt(mt, itctx))
hir::TyKind::Ref(lifetime, self.lower_ty_alloc(ty, itctx), *mutbl)
}
TyKind::PinnedRef(region, mt) => {
TyKind::PinnedRef(region, ty, mutbl) => {
let lifetime = self.lower_ty_direct_lifetime(t, *region);
let kind = hir::TyKind::Ref(lifetime, self.lower_mt(mt, itctx));
let kind = hir::TyKind::Ref(lifetime, self.lower_ty_alloc(ty, itctx), *mutbl);
let span = self.lower_span(t.span);
let arg = hir::Ty { kind, span, hir_id: self.next_id() };
let args = self.arena.alloc(hir::GenericArgs {
Expand Down Expand Up @@ -1998,10 +1998,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
// Given we are only considering `ImplicitSelf` types, we needn't consider
// the case where we have a mutable pattern to a reference as that would
// no longer be an `ImplicitSelf`.
TyKind::Ref(_, mt) | TyKind::PinnedRef(_, mt)
if mt.ty.kind.is_implicit_self() =>
TyKind::Ref(_, ty, mutbl) | TyKind::PinnedRef(_, ty, mutbl)
if ty.kind.is_implicit_self() =>
{
match mt.mutbl {
match mutbl {
hir::Mutability::Not => hir::ImplicitSelfKind::RefImm,
hir::Mutability::Mut => hir::ImplicitSelfKind::RefMut,
}
Expand Down Expand Up @@ -2473,10 +2473,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
.emit();
}

fn lower_mt(&mut self, mt: &MutTy, itctx: ImplTraitContext) -> hir::MutTy<'hir> {
hir::MutTy { ty: self.lower_ty_alloc(&mt.ty, itctx), mutbl: mt.mutbl }
}

#[instrument(level = "debug", skip(self), ret)]
fn lower_param_bounds(
&mut self,
Expand Down
20 changes: 9 additions & 11 deletions compiler/rustc_ast_pretty/src/pprust/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1346,20 +1346,23 @@ impl<'a> State<'a> {
self.print_type(ty);
self.word("]");
}
ast::TyKind::Ptr(mt) => {
ast::TyKind::Ptr(ty, mutbl) => {
self.word("*");
self.print_mt(mt, true);
self.print_mutability(*mutbl, true);
self.print_type(ty);
}
ast::TyKind::Ref(lifetime, mt) => {
ast::TyKind::Ref(lifetime, ty, mutbl) => {
self.word("&");
self.print_opt_lifetime(lifetime);
self.print_mt(mt, false);
self.print_mutability(*mutbl, false);
self.print_type(ty);
}
ast::TyKind::PinnedRef(lifetime, mt) => {
ast::TyKind::PinnedRef(lifetime, ty, mutbl) => {
self.word("&");
self.print_opt_lifetime(lifetime);
self.word("pin ");
self.print_mt(mt, true);
self.print_mutability(*mutbl, true);
self.print_type(ty);
}
ast::TyKind::Never => {
self.word("!");
Expand Down Expand Up @@ -2229,11 +2232,6 @@ impl<'a> State<'a> {
}
}

fn print_mt(&mut self, mt: &ast::MutTy, print_const: bool) {
self.print_mutability(mt.mutbl, print_const);
self.print_type(&mt.ty)
}

fn print_param(&mut self, input: &ast::Param, is_closure: bool) {
let ib = self.ibox(INDENT_UNIT);

Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4600,7 +4600,7 @@ impl<'diag, 'tcx> MirBorrowckCtxt<'_, 'diag, 'tcx> {
// `return_region`. Then use the `rustc_hir` type to get only
// the lifetime span.
match &fn_decl.inputs[index].kind {
hir::TyKind::Ref(lifetime, _) => {
hir::TyKind::Ref(lifetime, ..) => {
// With access to the lifetime, we can get
// the span of it.
arguments.push((*argument, lifetime.ident.span));
Expand All @@ -4615,7 +4615,7 @@ impl<'diag, 'tcx> MirBorrowckCtxt<'_, 'diag, 'tcx> {
.hir_node_by_def_id(alias_to)
.expect_item()
.expect_impl()
&& let hir::TyKind::Ref(lifetime, _) = self_ty.kind
&& let hir::TyKind::Ref(lifetime, ..) = self_ty.kind
{
arguments.push((*argument, lifetime.ident.span));
}
Expand All @@ -4637,7 +4637,7 @@ impl<'diag, 'tcx> MirBorrowckCtxt<'_, 'diag, 'tcx> {
let return_ty = sig.output().skip_binder();
let mut return_span = fn_decl.output.span();
if let hir::FnRetTy::Return(ty) = &fn_decl.output
&& let hir::TyKind::Ref(lifetime, _) = ty.kind
&& let hir::TyKind::Ref(lifetime, ..) = ty.kind
{
return_span = lifetime.ident.span;
}
Expand Down
5 changes: 2 additions & 3 deletions compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -927,8 +927,7 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, 'tcx> {
if let Node::TraitItem(ti) = self.infcx.tcx.hir_node_by_def_id(f_in_trait)
&& let hir::TraitItemKind::Fn(sig, _) = ti.kind
&& let Some(ty) = sig.decl.inputs.get(local.index() - 1)
&& let hir::TyKind::Ref(_, mut_ty) = ty.kind
&& let hir::Mutability::Not = mut_ty.mutbl
&& let hir::TyKind::Ref(_, _, hir::Mutability::Not) = ty.kind
&& sig.decl.implicit_self().has_implicit_self()
{
Some(ty.span)
Expand Down Expand Up @@ -2020,7 +2019,7 @@ fn get_mut_span_in_struct_field<'tcx>(
// Now we're dealing with the actual struct that we're going to suggest a change to,
// we can expect a field that is an immutable reference to a type.
&& let hir::Node::Field(field) = tcx.hir_node_by_def_id(field.did.as_local()?)
&& let hir::TyKind::Ref(lt, hir::MutTy { mutbl: hir::Mutability::Not, ty }) = field.ty.kind
&& let hir::TyKind::Ref(lt, ty, hir::Mutability::Not) = field.ty.kind
{
return Some(lt.ident.span.between(ty.span));
}
Expand Down
11 changes: 7 additions & 4 deletions compiler/rustc_borrowck/src/diagnostics/region_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,10 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, 'tcx> {
//
// &
// - let's call the lifetime of this reference `'1`
(ty::Ref(region, referent_ty, _), hir::TyKind::Ref(_lifetime, referent_hir_ty)) => {
(
ty::Ref(region, referent_ty, _),
hir::TyKind::Ref(_lifetime, referent_hir_ty, _),
) => {
if region.as_var() == needle_fr {
// Just grab the first character, the `&`.
let source_map = self.infcx.tcx.sess.source_map();
Expand All @@ -631,7 +634,7 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, 'tcx> {
}

// Otherwise, let's descend into the referent types.
search_stack.push((*referent_ty, referent_hir_ty.ty));
search_stack.push((*referent_ty, referent_hir_ty));
}

// Match up something like `Foo<'1>`
Expand Down Expand Up @@ -669,8 +672,8 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, 'tcx> {
search_stack.push((*elem_ty, elem_hir_ty));
}

(ty::RawPtr(mut_ty, _), hir::TyKind::Ptr(mut_hir_ty)) => {
search_stack.push((*mut_ty, mut_hir_ty.ty));
(ty::RawPtr(inner_ty, _), hir::TyKind::Ptr(hir_ty, _)) => {
search_stack.push((*inner_ty, hir_ty));
}

_ => {
Expand Down
21 changes: 13 additions & 8 deletions compiler/rustc_builtin_macros/src/autodiff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -473,11 +473,11 @@ mod llvm_enzyme {
fn assure_mut_ref(ty: &ast::Ty) -> ast::Ty {
let mut ty = ty.clone();
match ty.kind {
TyKind::Ptr(ref mut mut_ty) => {
mut_ty.mutbl = ast::Mutability::Mut;
TyKind::Ptr(_, ref mut mutbl) => {
*mutbl = ast::Mutability::Mut;
}
TyKind::Ref(_, ref mut mut_ty) => {
mut_ty.mutbl = ast::Mutability::Mut;
TyKind::Ref(_, _, ref mut mutbl) => {
*mutbl = ast::Mutability::Mut;
}
_ => {
panic!("unsupported type: {:?}", ty);
Expand Down Expand Up @@ -510,10 +510,15 @@ mod llvm_enzyme {
.map(|param| {
let ty = match &param.ty.kind {
TyKind::ImplicitSelf => self_ty(),
TyKind::Ref(lt, mt) if matches!(mt.ty.kind, TyKind::ImplicitSelf) => ecx
.ty(span, TyKind::Ref(*lt, ast::MutTy { ty: self_ty(), mutbl: mt.mutbl })),
TyKind::Ptr(mt) if matches!(mt.ty.kind, TyKind::ImplicitSelf) => {
ecx.ty(span, TyKind::Ptr(ast::MutTy { ty: self_ty(), mutbl: mt.mutbl }))
TyKind::Ref(lt, inner_ty, mutbl)
if matches!(inner_ty.kind, TyKind::ImplicitSelf) =>
{
ecx.ty(span, TyKind::Ref(*lt, self_ty(), *mutbl))
}
TyKind::Ptr(inner_ty, mutbl)
if matches!(inner_ty.kind, TyKind::ImplicitSelf) =>
{
ecx.ty(span, TyKind::Ptr(self_ty(), *mutbl))
}
_ => param.ty.clone(),
};
Expand Down
8 changes: 2 additions & 6 deletions compiler/rustc_expand/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,6 @@ impl<'a> ExtCtxt<'a> {
})
}

pub fn ty_mt(&self, ty: Box<ast::Ty>, mutbl: ast::Mutability) -> ast::MutTy {
ast::MutTy { ty, mutbl }
}

pub fn ty(&self, span: Span, kind: ast::TyKind) -> Box<ast::Ty> {
Box::new(ast::Ty { id: ast::DUMMY_NODE_ID, span, kind })
}
Expand Down Expand Up @@ -127,11 +123,11 @@ impl<'a> ExtCtxt<'a> {
lifetime: Option<ast::Lifetime>,
mutbl: ast::Mutability,
) -> Box<ast::Ty> {
self.ty(span, ast::TyKind::Ref(lifetime, self.ty_mt(ty, mutbl)))
self.ty(span, ast::TyKind::Ref(lifetime, ty, mutbl))
}

pub fn ty_ptr(&self, span: Span, ty: Box<ast::Ty>, mutbl: ast::Mutability) -> Box<ast::Ty> {
self.ty(span, ast::TyKind::Ptr(self.ty_mt(ty, mutbl)))
self.ty(span, ast::TyKind::Ptr(ty, mutbl))
}

pub fn ty_unit(&self, span: Span) -> Box<ast::Ty> {
Expand Down
Loading
Loading