Skip to content

Commit ad8610d

Browse files
committed
Auto merge of rust-lang#88499 - eddyb:layout-off, r=nagisa
Provide `layout_of` automatically (given tcx + param_env + error handling). After rust-lang#88337, there's no longer any uses of `LayoutOf` within `rustc_target` itself, so I realized I could move the trait to `rustc_middle::ty::layout` and redesign it a bit. This is similar to rust-lang#88338 (and supersedes it), but at no ergonomic loss, since there's no funky `C: LayoutOf<Ty = Ty>` -> `Ty: TyAbiInterface<C>` generic `impl` chain, and each `LayoutOf` still corresponds to one `impl` (of `LayoutOfHelpers`) for the specific context. After this PR, this is what's needed to get `trait LayoutOf` (with the `layout_of` method) implemented on some context type: * `TyCtxt`, via `HasTyCtxt` * `ParamEnv`, via `HasParamEnv` * a way to transform `LayoutError`s into the desired error type * an error type of `!` can be paired with having `cx.layout_of(...)` return `TyAndLayout` *without* `Result<...>` around it, such as used by codegen * this is done through a new `LayoutOfHelpers` trait (and so is specifying the type of `cx.layout_of(...)`) When going through this path (and not bypassing it with a manual `impl` of `LayoutOf`), the end result is that only the error case can be customized, the query itself and the success paths are guaranteed to be uniform. (**EDIT**: just noticed that because of the supertrait relationship, you cannot actually implement `LayoutOf` yourself, the blanket `impl` fully covers all possible context types that could ever implement it) Part of the motivation for this shape of API is that I've been working on querifying `FnAbi::of_*`, and what I want/need to introduce for that looks a lot like the setup in this PR - in particular, it's harder to express the `FnAbi` methods in `rustc_target`, since they're much more tied to `rustc` concepts. r? `@nagisa` cc `@oli-obk` `@bjorn3`
2 parents a0152da + 8f7c249 commit ad8610d

11 files changed

+13
-13
lines changed

clippy_lints/src/casts/cast_ptr_alignment.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ use clippy_utils::is_hir_ty_cfg_dependant;
33
use if_chain::if_chain;
44
use rustc_hir::{Expr, ExprKind, GenericArg};
55
use rustc_lint::LateContext;
6+
use rustc_middle::ty::layout::LayoutOf;
67
use rustc_middle::ty::{self, Ty};
78
use rustc_span::symbol::sym;
8-
use rustc_target::abi::LayoutOf;
99

1010
use super::CAST_PTR_ALIGNMENT;
1111

clippy_lints/src/escape.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ use rustc_hir::{self, AssocItemKind, Body, FnDecl, HirId, HirIdSet, Impl, ItemKi
55
use rustc_infer::infer::TyCtxtInferExt;
66
use rustc_lint::{LateContext, LateLintPass};
77
use rustc_middle::mir::FakeReadCause;
8+
use rustc_middle::ty::layout::LayoutOf;
89
use rustc_middle::ty::{self, TraitRef, Ty};
910
use rustc_session::{declare_tool_lint, impl_lint_pass};
1011
use rustc_span::source_map::Span;
1112
use rustc_span::symbol::kw;
12-
use rustc_target::abi::LayoutOf;
1313
use rustc_target::spec::abi::Abi;
1414
use rustc_typeck::expr_use_visitor::{Delegate, ExprUseVisitor, PlaceBase, PlaceWithHirId};
1515

@@ -171,7 +171,8 @@ impl<'a, 'tcx> Delegate<'tcx> for EscapeDelegate<'a, 'tcx> {
171171
// skip if there is a `self` parameter binding to a type
172172
// that contains `Self` (i.e.: `self: Box<Self>`), see #4804
173173
if let Some(trait_self_ty) = self.trait_self_ty {
174-
if map.name(cmt.hir_id) == kw::SelfLower && contains_ty(self.cx.tcx, cmt.place.ty(), trait_self_ty) {
174+
if map.name(cmt.hir_id) == kw::SelfLower && contains_ty(self.cx.tcx, cmt.place.ty(), trait_self_ty)
175+
{
175176
return;
176177
}
177178
}

clippy_lints/src/invalid_upcast_comparisons.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ use std::cmp::Ordering;
22

33
use rustc_hir::{Expr, ExprKind};
44
use rustc_lint::{LateContext, LateLintPass};
5+
use rustc_middle::ty::layout::LayoutOf;
56
use rustc_middle::ty::{self, IntTy, UintTy};
67
use rustc_session::{declare_lint_pass, declare_tool_lint};
78
use rustc_span::Span;
8-
use rustc_target::abi::LayoutOf;
99

1010
use clippy_utils::comparisons::Rel;
1111
use clippy_utils::consts::{constant, Constant};

clippy_lints/src/large_const_arrays.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
use crate::rustc_target::abi::LayoutOf;
21
use clippy_utils::diagnostics::span_lint_and_then;
32
use if_chain::if_chain;
43
use rustc_errors::Applicability;
54
use rustc_hir::{Item, ItemKind};
65
use rustc_lint::{LateContext, LateLintPass};
76
use rustc_middle::mir::interpret::ConstValue;
7+
use rustc_middle::ty::layout::LayoutOf;
88
use rustc_middle::ty::{self, ConstKind};
99
use rustc_session::{declare_tool_lint, impl_lint_pass};
1010
use rustc_span::{BytePos, Pos, Span};

clippy_lints/src/large_enum_variant.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use rustc_errors::Applicability;
66
use rustc_hir::{Item, ItemKind, VariantData};
77
use rustc_lint::{LateContext, LateLintPass};
88
use rustc_middle::lint::in_external_macro;
9+
use rustc_middle::ty::layout::LayoutOf;
910
use rustc_session::{declare_tool_lint, impl_lint_pass};
10-
use rustc_target::abi::LayoutOf;
1111

1212
declare_clippy_lint! {
1313
/// ### What it does

clippy_lints/src/large_stack_arrays.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ use if_chain::if_chain;
44
use rustc_hir::{Expr, ExprKind};
55
use rustc_lint::{LateContext, LateLintPass};
66
use rustc_middle::mir::interpret::ConstValue;
7+
use rustc_middle::ty::layout::LayoutOf;
78
use rustc_middle::ty::{self, ConstKind};
89
use rustc_session::{declare_tool_lint, impl_lint_pass};
910

10-
use crate::rustc_target::abi::LayoutOf;
11-
1211
declare_clippy_lint! {
1312
/// ### What it does
1413
/// Checks for local arrays that may be too large.

clippy_lints/src/methods/manual_saturating_arithmetic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_ast::ast;
66
use rustc_errors::Applicability;
77
use rustc_hir as hir;
88
use rustc_lint::LateContext;
9-
use rustc_target::abi::LayoutOf;
9+
use rustc_middle::ty::layout::LayoutOf;
1010

1111
pub fn check(
1212
cx: &LateContext<'_>,

clippy_lints/src/pass_by_ref_or_value.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ use rustc_hir::intravisit::FnKind;
1313
use rustc_hir::{BindingAnnotation, Body, FnDecl, HirId, Impl, ItemKind, MutTy, Mutability, Node, PatKind};
1414
use rustc_lint::{LateContext, LateLintPass};
1515
use rustc_middle::ty;
16+
use rustc_middle::ty::layout::LayoutOf;
1617
use rustc_session::{declare_tool_lint, impl_lint_pass};
1718
use rustc_span::def_id::LocalDefId;
1819
use rustc_span::{sym, Span};
19-
use rustc_target::abi::LayoutOf;
2020
use rustc_target::spec::abi::Abi;
2121
use rustc_target::spec::Target;
2222

clippy_lints/src/types/vec_box.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ use if_chain::if_chain;
55
use rustc_errors::Applicability;
66
use rustc_hir::{self as hir, def_id::DefId, GenericArg, QPath, TyKind};
77
use rustc_lint::LateContext;
8+
use rustc_middle::ty::layout::LayoutOf;
89
use rustc_middle::ty::TypeFoldable;
910
use rustc_span::symbol::sym;
10-
use rustc_target::abi::LayoutOf;
1111
use rustc_typeck::hir_ty_to_ty;
1212

1313
use super::VEC_BOX;

clippy_lints/src/vec.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use crate::rustc_target::abi::LayoutOf;
21
use clippy_utils::consts::{constant, Constant};
32
use clippy_utils::diagnostics::span_lint_and_sugg;
43
use clippy_utils::higher;
@@ -8,6 +7,7 @@ use if_chain::if_chain;
87
use rustc_errors::Applicability;
98
use rustc_hir::{BorrowKind, Expr, ExprKind, Mutability};
109
use rustc_lint::{LateContext, LateLintPass};
10+
use rustc_middle::ty::layout::LayoutOf;
1111
use rustc_middle::ty::{self, Ty};
1212
use rustc_session::{declare_tool_lint, impl_lint_pass};
1313
use rustc_span::source_map::Span;

clippy_lints/src/zero_sized_map_values.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ use clippy_utils::ty::{is_normalizable, is_type_diagnostic_item};
33
use if_chain::if_chain;
44
use rustc_hir::{self as hir, HirId, ItemKind, Node};
55
use rustc_lint::{LateContext, LateLintPass};
6+
use rustc_middle::ty::layout::LayoutOf as _;
67
use rustc_middle::ty::{Adt, Ty, TypeFoldable};
78
use rustc_session::{declare_lint_pass, declare_tool_lint};
89
use rustc_span::sym;
9-
use rustc_target::abi::LayoutOf as _;
1010
use rustc_typeck::hir_ty_to_ty;
1111

1212
declare_clippy_lint! {

0 commit comments

Comments
 (0)