Skip to content

Commit 1a2c479

Browse files
committed
Auto merge of #164708 - JonathanBrouwer:rollup-Td0LIV7, r=JonathanBrouwer
Rollup of 6 pull requests Successful merges: - #164383 (handle higher-ranked preds in imperfect derives diagnostic) - #164620 (Wrap expected_ty for cfi-{diag,recover} in with_no_trimmed_paths) - #164684 (Remove redundant `PartialOrd` chaining methods) - #158076 (Offload safe mutable args with `Region` and `PartitioningStrategy`) - #164680 (Add regression test for async fn declared with #[eii]) - #164696 (Add `#[default]` for `newtype_index!`)
2 parents fa0b453 + 0e758be commit 1a2c479

39 files changed

Lines changed: 968 additions & 211 deletions

‎compiler/rustc_codegen_llvm/src/builder.rs‎

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2081,18 +2081,20 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> {
20812081
);
20822082

20832083
let mut expected_ty = String::from("fn(");
2084-
for (i, arg) in fn_abi.args.iter().enumerate() {
2085-
if i > 0 {
2086-
expected_ty.push_str(", ");
2084+
ty::print::with_no_trimmed_paths!({
2085+
for (i, arg) in fn_abi.args.iter().enumerate() {
2086+
if i > 0 {
2087+
expected_ty.push_str(", ");
2088+
}
2089+
use std::fmt::Write;
2090+
write!(&mut expected_ty, "{}", arg.layout.ty).unwrap();
20872091
}
2088-
use std::fmt::Write;
2089-
write!(&mut expected_ty, "{}", arg.layout.ty).unwrap();
2090-
}
2091-
expected_ty.push(')');
2092-
if !fn_abi.ret.layout.ty.is_unit() {
2093-
use std::fmt::Write;
2094-
write!(&mut expected_ty, " -> {}", fn_abi.ret.layout.ty).unwrap();
2095-
}
2092+
expected_ty.push(')');
2093+
if !fn_abi.ret.layout.ty.is_unit() {
2094+
use std::fmt::Write;
2095+
write!(&mut expected_ty, " -> {}", fn_abi.ret.layout.ty).unwrap();
2096+
}
2097+
});
20962098

20972099
// 4 for cfi-icall (indirect call)
20982100
let check_kind = 4;

‎compiler/rustc_hir_typeck/src/intrinsicck.rs‎

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
use hir::HirId;
22
use rustc_abi::Primitive::Pointer;
33
use rustc_abi::VariantIdx;
4+
use rustc_data_structures::fx::FxHashSet;
45
use rustc_errors::codes::*;
56
use rustc_errors::struct_span_code_err;
67
use rustc_hir as hir;
78
use rustc_index::Idx;
89
use rustc_middle::ty::layout::{LayoutError, SizeSkeleton};
10+
use rustc_middle::ty::offload_meta::is_region_ty;
911
use rustc_middle::ty::{self, Ty, TyCtxt, Unnormalized};
1012
use rustc_span::def_id::LocalDefId;
1113
use rustc_span::{ErrorGuaranteed, bug};
@@ -134,6 +136,28 @@ fn check_transmute<'tcx>(
134136
}
135137
}
136138

139+
fn contains_nested_offload_region<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> bool {
140+
let mut visited = FxHashSet::default();
141+
let mut stack = vec![ty];
142+
let mut is_root = true;
143+
144+
while let Some(ty) = stack.pop() {
145+
if !is_root && is_region_ty(tcx, ty) {
146+
return true;
147+
}
148+
is_root = false;
149+
150+
if let ty::Adt(def, args) = *ty.kind()
151+
&& visited.insert(def.did())
152+
{
153+
stack.extend(def.all_fields().map(|field| field.ty(tcx, args).skip_norm_wip()));
154+
}
155+
stack.extend(ty.walk().skip(1).filter_map(|arg| arg.as_type()));
156+
}
157+
158+
false
159+
}
160+
137161
fn check_offload<'tcx>(
138162
tcx: TyCtxt<'tcx>,
139163
typing_env: ty::TypingEnv<'tcx>,
@@ -205,7 +229,23 @@ fn check_offload<'tcx>(
205229
{
206230
let norm_input_ty = normalize(input_ty);
207231
let norm_arg_ty = normalize(arg_ty);
208-
if norm_input_ty != norm_arg_ty {
232+
233+
if contains_nested_offload_region(tcx, norm_input_ty)
234+
|| contains_nested_offload_region(tcx, norm_arg_ty)
235+
{
236+
let guar = tcx
237+
.sess
238+
.dcx()
239+
.struct_span_err(
240+
span,
241+
format!(
242+
"offload kernel argument {i} contains a `Region` nested inside another \
243+
type. Pass the `Region` by value so it can be mapped like a slice"
244+
),
245+
)
246+
.emit_err();
247+
result = Err(guar);
248+
} else if norm_input_ty != norm_arg_ty {
209249
let guar = tcx
210250
.sess
211251
.dcx()

‎compiler/rustc_index_macros/src/lib.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ mod newtype;
2626
///
2727
/// Accepted attributes for customization:
2828
/// - `#[stable_hash]`: derives `StableHash`.
29+
/// - `#[default]`: derives `std::default::Default` returning `Self::ZERO`.
2930
/// - `#[encodable]`: derives `Encodable`/`Decodable`.
3031
/// - `#[orderable]`: derives `PartialOrd`/`Ord`, plus step-related methods.
3132
/// - `#[debug_format = "Foo({})"]`: derives `Debug` with particular output.

‎compiler/rustc_index_macros/src/newtype.rs‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ impl Parse for Newtype {
2424
let mut encodable = false;
2525
let mut ord = false;
2626
let mut stable_hash = false;
27+
let mut default = false;
2728
let mut gate_rustc_only = quote! {};
2829
let mut gate_rustc_only_cfg = quote! { all() };
2930

@@ -46,6 +47,10 @@ impl Parse for Newtype {
4647
stable_hash = true;
4748
false
4849
}
50+
"default" => {
51+
default = true;
52+
false
53+
}
4954
"max" => {
5055
let Meta::NameValue(MetaNameValue { value: Expr::Lit(lit), .. }) = &attr.meta
5156
else {
@@ -185,6 +190,19 @@ impl Parse for Newtype {
185190
quote! {}
186191
};
187192

193+
let default_impl = if default {
194+
quote! {
195+
impl std::default::Default for #name {
196+
#[inline]
197+
fn default() -> Self {
198+
Self::ZERO
199+
}
200+
}
201+
}
202+
} else {
203+
quote! {}
204+
};
205+
188206
let debug_impl = quote! {
189207
impl ::std::fmt::Debug for #name {
190208
fn fmt(&self, fmt: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
@@ -323,6 +341,8 @@ impl Parse for Newtype {
323341

324342
#stable_hash_impl
325343

344+
#default_impl
345+
326346
impl From<#name> for u32 {
327347
#[inline]
328348
fn from(v: #name) -> u32 {

‎compiler/rustc_middle/src/ty/offload_meta.rs‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use bitflags::bitflags;
22
use rustc_abi::{BackendRepr, TyAbiInterface};
3+
use rustc_span::sym;
34
use rustc_target::callconv::ArgAbi;
45

56
use crate::ty::{self, PseudoCanonicalInput, Ty, TyCtxt, TypingEnv};
@@ -75,6 +76,25 @@ impl OffloadMetadata {
7576
where
7677
Ty<'tcx>: TyAbiInterface<'tcx, C>,
7778
{
79+
if let Some(elem_ty) = region_element_ty(tcx, ty) {
80+
let ptr = OffloadMetadata::from_ty(tcx, Ty::new_slice(tcx, elem_ty));
81+
let len = OffloadMetadata::from_ty(tcx, tcx.types.usize);
82+
// `Region` is a `{ ptr, len }` pair, but field order is not guaranteed.
83+
return arg_abi
84+
.layout
85+
.fields
86+
.index_by_increasing_offset()
87+
.filter(|&i| arg_abi.layout.field(cx, i).size.bytes() != 0)
88+
.map(|i| {
89+
if arg_abi.layout.field(cx, i).ty == tcx.types.usize {
90+
(len, tcx.types.usize)
91+
} else {
92+
(ptr, Ty::new_mut_ptr(tcx, elem_ty))
93+
}
94+
})
95+
.collect();
96+
}
97+
7898
match arg_abi.layout.backend_repr {
7999
BackendRepr::ScalarPair { a: _, b: _, b_offset: _ } => (0..2)
80100
.map(|i| {
@@ -87,6 +107,22 @@ impl OffloadMetadata {
87107
}
88108
}
89109

110+
pub fn is_region_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> bool {
111+
matches!(
112+
ty.kind(),
113+
ty::Adt(def, _) if Some(def.did()) == tcx.get_diagnostic_item(sym::offload_region)
114+
)
115+
}
116+
117+
fn region_element_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
118+
if is_region_ty(tcx, ty) {
119+
let ty::Adt(_, args) = ty.kind() else { unreachable!() };
120+
Some(args.type_at(1))
121+
} else {
122+
None
123+
}
124+
}
125+
90126
// FIXME(Sa4dUs): implement a solid logic to determine the payload size
91127
fn get_payload_size<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> OffloadSize {
92128
match ty.kind() {

‎compiler/rustc_span/src/hygiene.rs‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,10 @@ impl fmt::Debug for ExpnId {
134134
rustc_index::newtype_index! {
135135
/// A unique ID associated with a macro invocation and expansion.
136136
#[debug_format = "expn{}"]
137+
#[default]
137138
pub struct LocalExpnId {}
138139
}
139140

140-
impl Default for LocalExpnId {
141-
fn default() -> LocalExpnId {
142-
LocalExpnId::ROOT
143-
}
144-
}
145-
146141
// To ensure correctness of incremental compilation,
147142
// `LocalExpnId` must not implement `Ord` or `PartialOrd`.
148143
// See https://github.com/rust-lang/rust/issues/90317.

‎compiler/rustc_span/src/symbol.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1515,6 +1515,7 @@ symbols! {
15151515
offload,
15161516
offload_get_num_devices,
15171517
offload_kernel,
1518+
offload_region,
15181519
offset,
15191520
offset_of,
15201521
offset_of_enum,

‎compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6788,7 +6788,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
67886788
tcx,
67896789
ObligationCause::dummy(),
67906790
param_env,
6791-
ty::TraitRef::new(tcx, trait_def_id, trait_args),
6791+
parent_trait_pred.rebind(ty::TraitRef::new(tcx, trait_def_id, trait_args)),
67926792
);
67936793
self.predicate_may_hold(&obligation)
67946794
})

‎compiler/rustc_type_ir/src/lib.rs‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,7 @@ rustc_index::newtype_index! {
375375
/// type -- an idealized representative of "types in general" that we
376376
/// use for checking generic functions.
377377
#[stable_hash]
378+
#[default]
378379
#[encodable]
379380
#[orderable]
380381
#[debug_format = "U{}"]
@@ -422,12 +423,6 @@ impl UniverseIndex {
422423
}
423424
}
424425

425-
impl Default for UniverseIndex {
426-
fn default() -> Self {
427-
Self::ROOT
428-
}
429-
}
430-
431426
rustc_index::newtype_index! {
432427
#[stable_hash]
433428
#[encodable]

‎library/core/src/array/mod.rs‎

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -430,20 +430,12 @@ const impl<T: [const] PartialOrd, const N: usize> PartialOrd for [T; N] {
430430
}
431431

432432
#[inline]
433-
fn __chaining_lt(&self, other: &[T; N]) -> ControlFlow<bool> {
434-
<[T] as PartialOrd>::__chaining_lt(self, other)
433+
fn __chaining_less(&self, other: &[T; N]) -> ControlFlow<bool> {
434+
<[T] as PartialOrd>::__chaining_less(self, other)
435435
}
436436
#[inline]
437-
fn __chaining_le(&self, other: &[T; N]) -> ControlFlow<bool> {
438-
<[T] as PartialOrd>::__chaining_le(self, other)
439-
}
440-
#[inline]
441-
fn __chaining_ge(&self, other: &[T; N]) -> ControlFlow<bool> {
442-
<[T] as PartialOrd>::__chaining_ge(self, other)
443-
}
444-
#[inline]
445-
fn __chaining_gt(&self, other: &[T; N]) -> ControlFlow<bool> {
446-
<[T] as PartialOrd>::__chaining_gt(self, other)
437+
fn __chaining_greater(&self, other: &[T; N]) -> ControlFlow<bool> {
438+
<[T] as PartialOrd>::__chaining_greater(self, other)
447439
}
448440
}
449441

0 commit comments

Comments
 (0)