Skip to content

Commit 86d8f12

Browse files
committed
Auto merge of #106934 - DrMeepster:offset_of, r=WaffleLapkin
Add offset_of! macro (RFC 3308) Implements rust-lang/rfcs#3308 (tracking issue #106655) by adding the built in macro `core::mem::offset_of`. Two of the future possibilities are also implemented: * Nested field accesses (without array indexing) * DST support (for `Sized` fields) I wrote this a few months ago, before the RFC merged. Now that it's merged, I decided to rebase and finish it. cc `@thomcc` (RFC author)
2 parents 84bab31 + 68c4776 commit 86d8f12

File tree

8 files changed

+22
-3
lines changed

8 files changed

+22
-3
lines changed

clippy_lints/src/loops/never_loop.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,8 @@ fn never_loop_expr(expr: &Expr<'_>, ignore_ids: &mut Vec<HirId>, main_loop_id: H
226226
| InlineAsmOperand::SymStatic { .. } => NeverLoopResult::Otherwise,
227227
})
228228
.fold(NeverLoopResult::Otherwise, combine_seq),
229-
ExprKind::Yield(_, _)
229+
ExprKind::OffsetOf(_, _)
230+
| ExprKind::Yield(_, _)
230231
| ExprKind::Closure { .. }
231232
| ExprKind::Path(_)
232233
| ExprKind::ConstBlock(_)

clippy_lints/src/matches/significant_drop_in_scrutinee.rs

+1
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ impl<'a, 'tcx> Visitor<'tcx> for SigDropHelper<'a, 'tcx> {
342342
ExprKind::DropTemps(_) |
343343
ExprKind::Err(_) |
344344
ExprKind::InlineAsm(_) |
345+
ExprKind::OffsetOf(_, _) |
345346
ExprKind::Let(_) |
346347
ExprKind::Lit(_) |
347348
ExprKind::Loop(_, _, _, _) |

clippy_lints/src/utils/author.rs

+4
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,10 @@ impl<'a, 'tcx> PrintVisitor<'a, 'tcx> {
558558
kind!("InlineAsm(_)");
559559
out!("// unimplemented: `ExprKind::InlineAsm` is not further destructured at the moment");
560560
},
561+
ExprKind::OffsetOf(container, ref fields) => {
562+
bind!(self, container, fields);
563+
kind!("OffsetOf({container}, {fields})");
564+
}
561565
ExprKind::Struct(qpath, fields, base) => {
562566
bind!(self, qpath, fields);
563567
opt_bind!(self, base);

clippy_utils/src/eager_or_lazy.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,8 @@ fn expr_eagerness<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) -> EagernessS
218218
| ExprKind::AddrOf(..)
219219
| ExprKind::Struct(..)
220220
| ExprKind::Repeat(..)
221-
| ExprKind::Block(Block { stmts: [], .. }, _) => (),
221+
| ExprKind::Block(Block { stmts: [], .. }, _)
222+
| ExprKind::OffsetOf(..) => (),
222223

223224
// Assignment might be to a local defined earlier, so don't eagerly evaluate.
224225
// Blocks with multiple statements might be expensive, so don't eagerly evaluate.

clippy_utils/src/hir_utils.rs

+9
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,9 @@ impl HirEqInterExpr<'_, '_, '_> {
301301
(&ExprKind::Unary(l_op, le), &ExprKind::Unary(r_op, re)) => l_op == r_op && self.eq_expr(le, re),
302302
(&ExprKind::Array(l), &ExprKind::Array(r)) => self.eq_exprs(l, r),
303303
(&ExprKind::DropTemps(le), &ExprKind::DropTemps(re)) => self.eq_expr(le, re),
304+
(&ExprKind::OffsetOf(l_container, ref l_fields), &ExprKind::OffsetOf(r_container, ref r_fields)) => {
305+
self.eq_ty(l_container, r_container) && over(l_fields, r_fields, |l, r| l.name == r.name)
306+
},
304307
_ => false,
305308
};
306309
(is_eq && (!self.should_ignore(left) || !self.should_ignore(right)))
@@ -701,6 +704,12 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
701704
}
702705
}
703706
},
707+
ExprKind::OffsetOf(container, fields) => {
708+
self.hash_ty(container);
709+
for field in fields {
710+
self.hash_name(field.name);
711+
}
712+
},
704713
ExprKind::Let(Let { pat, init, ty, .. }) => {
705714
self.hash_expr(init);
706715
if let Some(ty) = ty {

clippy_utils/src/qualify_min_const_fn.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ fn check_rvalue<'tcx>(
194194
))
195195
}
196196
},
197-
Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf, _) | Rvalue::ShallowInitBox(_, _) => Ok(()),
197+
Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(_), _) | Rvalue::ShallowInitBox(_, _) => Ok(()),
198198
Rvalue::UnaryOp(_, operand) => {
199199
let ty = operand.ty(body, tcx);
200200
if ty.is_integral() || ty.is_bool() {

clippy_utils/src/sugg.rs

+2
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ impl<'a> Sugg<'a> {
139139
| hir::ExprKind::Field(..)
140140
| hir::ExprKind::Index(..)
141141
| hir::ExprKind::InlineAsm(..)
142+
| hir::ExprKind::OffsetOf(..)
142143
| hir::ExprKind::ConstBlock(..)
143144
| hir::ExprKind::Lit(..)
144145
| hir::ExprKind::Loop(..)
@@ -197,6 +198,7 @@ impl<'a> Sugg<'a> {
197198
| ast::ExprKind::ForLoop(..)
198199
| ast::ExprKind::Index(..)
199200
| ast::ExprKind::InlineAsm(..)
201+
| ast::ExprKind::OffsetOf(..)
200202
| ast::ExprKind::ConstBlock(..)
201203
| ast::ExprKind::Lit(..)
202204
| ast::ExprKind::IncludedBytes(..)

clippy_utils/src/visitors.rs

+1
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,7 @@ pub fn for_each_unconsumed_temporary<'tcx, B>(
662662
| ExprKind::Path(_)
663663
| ExprKind::Continue(_)
664664
| ExprKind::InlineAsm(_)
665+
| ExprKind::OffsetOf(..)
665666
| ExprKind::Err(_) => (),
666667
}
667668
ControlFlow::Continue(())

0 commit comments

Comments
 (0)