Skip to content

Commit 082e4ca

Browse files
committed
Auto merge of #96428 - GuillaumeGomez:rollup-4noqr33, r=GuillaumeGomez
Rollup of 8 pull requests Successful merges: - #94022 (Clarify that `Cow::into_owned` returns owned data) - #94703 (Fix codegen bug in "ptx-kernel" abi related to arg passing) - #95949 (Implement Default for AssertUnwindSafe) - #96361 (Switch JS code to ES6) - #96372 (Suggest calling method on nested field when struct is missing method) - #96386 (simplify `describe_field` func in borrowck's diagnostics part) - #96400 (Correct documentation for `Rvalue::ShallowInitBox`) - #96415 (Remove references to git.io) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 3d237ab + 223f107 commit 082e4ca

File tree

21 files changed

+721
-284
lines changed

21 files changed

+721
-284
lines changed

compiler/rustc_borrowck/src/diagnostics/mod.rs

+10-19
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ crate use outlives_suggestion::OutlivesSuggestionBuilder;
4040
crate use region_errors::{ErrorConstraintInfo, RegionErrorKind, RegionErrors};
4141
crate use region_name::{RegionName, RegionNameSource};
4242
crate use rustc_const_eval::util::CallKind;
43+
use rustc_middle::mir::tcx::PlaceTy;
4344

4445
pub(super) struct IncludingDowncast(pub(super) bool);
4546

@@ -329,30 +330,20 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
329330

330331
/// End-user visible description of the `field`nth field of `base`
331332
fn describe_field(&self, place: PlaceRef<'tcx>, field: Field) -> String {
332-
// FIXME Place2 Make this work iteratively
333-
match place {
334-
PlaceRef { local, projection: [] } => {
335-
let local = &self.body.local_decls[local];
336-
self.describe_field_from_ty(local.ty, field, None)
337-
}
333+
let place_ty = match place {
334+
PlaceRef { local, projection: [] } => PlaceTy::from_ty(self.body.local_decls[local].ty),
338335
PlaceRef { local, projection: [proj_base @ .., elem] } => match elem {
339-
ProjectionElem::Deref => {
340-
self.describe_field(PlaceRef { local, projection: proj_base }, field)
341-
}
342-
ProjectionElem::Downcast(_, variant_index) => {
343-
let base_ty = place.ty(self.body, self.infcx.tcx).ty;
344-
self.describe_field_from_ty(base_ty, field, Some(*variant_index))
345-
}
346-
ProjectionElem::Field(_, field_type) => {
347-
self.describe_field_from_ty(*field_type, field, None)
348-
}
349-
ProjectionElem::Index(..)
336+
ProjectionElem::Deref
337+
| ProjectionElem::Index(..)
350338
| ProjectionElem::ConstantIndex { .. }
351339
| ProjectionElem::Subslice { .. } => {
352-
self.describe_field(PlaceRef { local, projection: proj_base }, field)
340+
PlaceRef { local, projection: proj_base }.ty(self.body, self.infcx.tcx)
353341
}
342+
ProjectionElem::Downcast(..) => place.ty(self.body, self.infcx.tcx),
343+
ProjectionElem::Field(_, field_type) => PlaceTy::from_ty(*field_type),
354344
},
355-
}
345+
};
346+
self.describe_field_from_ty(place_ty.ty, field, place_ty.variant_index)
356347
}
357348

358349
/// End-user visible description of the `field_index`nth field of `ty`

compiler/rustc_middle/src/mir/mod.rs

-2
Original file line numberDiff line numberDiff line change
@@ -2581,8 +2581,6 @@ pub enum Rvalue<'tcx> {
25812581
/// This is different from a normal transmute because dataflow analysis will treat the box as
25822582
/// initialized but its content as uninitialized. Like other pointer casts, this in general
25832583
/// affects alias analysis.
2584-
///
2585-
/// Disallowed after drop elaboration.
25862584
ShallowInitBox(Operand<'tcx>, Ty<'tcx>),
25872585
}
25882586

compiler/rustc_middle/src/ty/layout.rs

+16
Original file line numberDiff line numberDiff line change
@@ -2592,6 +2592,22 @@ where
25922592

25932593
pointee_info
25942594
}
2595+
2596+
fn is_adt(this: TyAndLayout<'tcx>) -> bool {
2597+
matches!(this.ty.kind(), ty::Adt(..))
2598+
}
2599+
2600+
fn is_never(this: TyAndLayout<'tcx>) -> bool {
2601+
this.ty.kind() == &ty::Never
2602+
}
2603+
2604+
fn is_tuple(this: TyAndLayout<'tcx>) -> bool {
2605+
matches!(this.ty.kind(), ty::Tuple(..))
2606+
}
2607+
2608+
fn is_unit(this: TyAndLayout<'tcx>) -> bool {
2609+
matches!(this.ty.kind(), ty::Tuple(list) if list.len() == 0)
2610+
}
25952611
}
25962612

25972613
impl<'tcx> ty::Instance<'tcx> {

compiler/rustc_middle/src/ty/list.rs

+4
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ impl<T> List<T> {
6161
static EMPTY_SLICE: InOrder<usize, MaxAlign> = InOrder(0, MaxAlign);
6262
unsafe { &*(&EMPTY_SLICE as *const _ as *const List<T>) }
6363
}
64+
65+
pub fn len(&self) -> usize {
66+
self.len
67+
}
6468
}
6569

6670
impl<T: Copy> List<T> {

compiler/rustc_target/src/abi/call/mod.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,13 @@ impl<'a, Ty> FnAbi<'a, Ty> {
696696
"sparc" => sparc::compute_abi_info(cx, self),
697697
"sparc64" => sparc64::compute_abi_info(cx, self),
698698
"nvptx" => nvptx::compute_abi_info(self),
699-
"nvptx64" => nvptx64::compute_abi_info(self),
699+
"nvptx64" => {
700+
if cx.target_spec().adjust_abi(abi) == spec::abi::Abi::PtxKernel {
701+
nvptx64::compute_ptx_kernel_abi_info(cx, self)
702+
} else {
703+
nvptx64::compute_abi_info(self)
704+
}
705+
}
700706
"hexagon" => hexagon::compute_abi_info(self),
701707
"riscv32" | "riscv64" => riscv::compute_abi_info(cx, self),
702708
"wasm32" | "wasm64" => {
+39-8
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,35 @@
1-
// Reference: PTX Writer's Guide to Interoperability
2-
// https://docs.nvidia.com/cuda/ptx-writers-guide-to-interoperability
3-
4-
use crate::abi::call::{ArgAbi, FnAbi};
1+
use crate::abi::call::{ArgAbi, FnAbi, PassMode, Reg, Size, Uniform};
2+
use crate::abi::{HasDataLayout, TyAbiInterface};
53

64
fn classify_ret<Ty>(ret: &mut ArgAbi<'_, Ty>) {
75
if ret.layout.is_aggregate() && ret.layout.size.bits() > 64 {
86
ret.make_indirect();
9-
} else {
10-
ret.extend_integer_width_to(64);
117
}
128
}
139

1410
fn classify_arg<Ty>(arg: &mut ArgAbi<'_, Ty>) {
1511
if arg.layout.is_aggregate() && arg.layout.size.bits() > 64 {
1612
arg.make_indirect();
17-
} else {
18-
arg.extend_integer_width_to(64);
13+
}
14+
}
15+
16+
fn classify_arg_kernel<'a, Ty, C>(_cx: &C, arg: &mut ArgAbi<'a, Ty>)
17+
where
18+
Ty: TyAbiInterface<'a, C> + Copy,
19+
C: HasDataLayout,
20+
{
21+
if matches!(arg.mode, PassMode::Pair(..)) && (arg.layout.is_adt() || arg.layout.is_tuple()) {
22+
let align_bytes = arg.layout.align.abi.bytes();
23+
24+
let unit = match align_bytes {
25+
1 => Reg::i8(),
26+
2 => Reg::i16(),
27+
4 => Reg::i32(),
28+
8 => Reg::i64(),
29+
16 => Reg::i128(),
30+
_ => unreachable!("Align is given as power of 2 no larger than 16 bytes"),
31+
};
32+
arg.cast_to(Uniform { unit, total: Size::from_bytes(2 * align_bytes) });
1933
}
2034
}
2135

@@ -31,3 +45,20 @@ pub fn compute_abi_info<Ty>(fn_abi: &mut FnAbi<'_, Ty>) {
3145
classify_arg(arg);
3246
}
3347
}
48+
49+
pub fn compute_ptx_kernel_abi_info<'a, Ty, C>(cx: &C, fn_abi: &mut FnAbi<'a, Ty>)
50+
where
51+
Ty: TyAbiInterface<'a, C> + Copy,
52+
C: HasDataLayout,
53+
{
54+
if !fn_abi.ret.layout.is_unit() && !fn_abi.ret.layout.is_never() {
55+
panic!("Kernels should not return anything other than () or !");
56+
}
57+
58+
for arg in &mut fn_abi.args {
59+
if arg.is_ignore() {
60+
continue;
61+
}
62+
classify_arg_kernel(cx, arg);
63+
}
64+
}

compiler/rustc_target/src/abi/mod.rs

+32
Original file line numberDiff line numberDiff line change
@@ -1355,6 +1355,10 @@ pub trait TyAbiInterface<'a, C>: Sized {
13551355
cx: &C,
13561356
offset: Size,
13571357
) -> Option<PointeeInfo>;
1358+
fn is_adt(this: TyAndLayout<'a, Self>) -> bool;
1359+
fn is_never(this: TyAndLayout<'a, Self>) -> bool;
1360+
fn is_tuple(this: TyAndLayout<'a, Self>) -> bool;
1361+
fn is_unit(this: TyAndLayout<'a, Self>) -> bool;
13581362
}
13591363

13601364
impl<'a, Ty> TyAndLayout<'a, Ty> {
@@ -1396,6 +1400,34 @@ impl<'a, Ty> TyAndLayout<'a, Ty> {
13961400
_ => false,
13971401
}
13981402
}
1403+
1404+
pub fn is_adt<C>(self) -> bool
1405+
where
1406+
Ty: TyAbiInterface<'a, C>,
1407+
{
1408+
Ty::is_adt(self)
1409+
}
1410+
1411+
pub fn is_never<C>(self) -> bool
1412+
where
1413+
Ty: TyAbiInterface<'a, C>,
1414+
{
1415+
Ty::is_never(self)
1416+
}
1417+
1418+
pub fn is_tuple<C>(self) -> bool
1419+
where
1420+
Ty: TyAbiInterface<'a, C>,
1421+
{
1422+
Ty::is_tuple(self)
1423+
}
1424+
1425+
pub fn is_unit<C>(self) -> bool
1426+
where
1427+
Ty: TyAbiInterface<'a, C>,
1428+
{
1429+
Ty::is_unit(self)
1430+
}
13991431
}
14001432

14011433
impl<'a, Ty> TyAndLayout<'a, Ty> {

compiler/rustc_target/src/asm/mips.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ impl MipsInlineAsmRegClass {
4343
}
4444
}
4545

46-
// The reserved registers are somewhat taken from <https://git.io/JUR1k#L150>.
46+
// The reserved registers are somewhat taken from
47+
// <https://github.com/llvm/llvm-project/blob/deb8f8bcf31540c657716ea5242183b0792702a1/llvm/lib/Target/Mips/MipsRegisterInfo.cpp#L150>.
4748
def_regs! {
4849
Mips MipsInlineAsmReg MipsInlineAsmRegClass {
4950
r2: reg = ["$2"],

compiler/rustc_typeck/src/check/expr.rs

+16-20
Original file line numberDiff line numberDiff line change
@@ -2285,14 +2285,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
22852285
// try to add a suggestion in case the field is a nested field of a field of the Adt
22862286
if let Some((fields, substs)) = self.get_field_candidates(span, expr_t) {
22872287
for candidate_field in fields.iter() {
2288-
if let Some(field_path) = self.check_for_nested_field(
2288+
if let Some(mut field_path) = self.check_for_nested_field_satisfying(
22892289
span,
2290-
field,
2290+
&|candidate_field, _| candidate_field.ident(self.tcx()) == field,
22912291
candidate_field,
22922292
substs,
22932293
vec![],
22942294
self.tcx.parent_module(id).to_def_id(),
22952295
) {
2296+
// field_path includes `field` that we're looking for, so pop it.
2297+
field_path.pop();
2298+
22962299
let field_path_str = field_path
22972300
.iter()
22982301
.map(|id| id.name.to_ident_string())
@@ -2312,7 +2315,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
23122315
err
23132316
}
23142317

2315-
fn get_field_candidates(
2318+
crate fn get_field_candidates(
23162319
&self,
23172320
span: Span,
23182321
base_t: Ty<'tcx>,
@@ -2337,49 +2340,42 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
23372340

23382341
/// This method is called after we have encountered a missing field error to recursively
23392342
/// search for the field
2340-
fn check_for_nested_field(
2343+
crate fn check_for_nested_field_satisfying(
23412344
&self,
23422345
span: Span,
2343-
target_field: Ident,
2346+
matches: &impl Fn(&ty::FieldDef, Ty<'tcx>) -> bool,
23442347
candidate_field: &ty::FieldDef,
23452348
subst: SubstsRef<'tcx>,
23462349
mut field_path: Vec<Ident>,
23472350
id: DefId,
23482351
) -> Option<Vec<Ident>> {
23492352
debug!(
2350-
"check_for_nested_field(span: {:?}, candidate_field: {:?}, field_path: {:?}",
2353+
"check_for_nested_field_satisfying(span: {:?}, candidate_field: {:?}, field_path: {:?}",
23512354
span, candidate_field, field_path
23522355
);
23532356

2354-
if candidate_field.ident(self.tcx) == target_field {
2355-
Some(field_path)
2356-
} else if field_path.len() > 3 {
2357+
if field_path.len() > 3 {
23572358
// For compile-time reasons and to avoid infinite recursion we only check for fields
23582359
// up to a depth of three
23592360
None
23602361
} else {
23612362
// recursively search fields of `candidate_field` if it's a ty::Adt
2362-
23632363
field_path.push(candidate_field.ident(self.tcx).normalize_to_macros_2_0());
23642364
let field_ty = candidate_field.ty(self.tcx, subst);
23652365
if let Some((nested_fields, subst)) = self.get_field_candidates(span, field_ty) {
23662366
for field in nested_fields.iter() {
2367-
let accessible = field.vis.is_accessible_from(id, self.tcx);
2368-
if accessible {
2369-
let ident = field.ident(self.tcx).normalize_to_macros_2_0();
2370-
if ident == target_field {
2367+
if field.vis.is_accessible_from(id, self.tcx) {
2368+
if matches(candidate_field, field_ty) {
23712369
return Some(field_path);
2372-
}
2373-
let field_path = field_path.clone();
2374-
if let Some(path) = self.check_for_nested_field(
2370+
} else if let Some(field_path) = self.check_for_nested_field_satisfying(
23752371
span,
2376-
target_field,
2372+
matches,
23772373
field,
23782374
subst,
2379-
field_path,
2375+
field_path.clone(),
23802376
id,
23812377
) {
2382-
return Some(path);
2378+
return Some(field_path);
23832379
}
23842380
}
23852381
}

compiler/rustc_typeck/src/check/method/suggest.rs

+41-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use rustc_trait_selection::traits::{
2828
use std::cmp::Ordering;
2929
use std::iter;
3030

31-
use super::probe::Mode;
31+
use super::probe::{Mode, ProbeScope};
3232
use super::{CandidateSource, MethodError, NoMatchData};
3333

3434
impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
@@ -1129,6 +1129,46 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11291129
label_span_not_found();
11301130
}
11311131

1132+
if let SelfSource::MethodCall(expr) = source
1133+
&& let Some((fields, substs)) = self.get_field_candidates(span, actual)
1134+
{
1135+
let call_expr =
1136+
self.tcx.hir().expect_expr(self.tcx.hir().get_parent_node(expr.hir_id));
1137+
for candidate_field in fields.iter() {
1138+
if let Some(field_path) = self.check_for_nested_field_satisfying(
1139+
span,
1140+
&|_, field_ty| {
1141+
self.lookup_probe(
1142+
span,
1143+
item_name,
1144+
field_ty,
1145+
call_expr,
1146+
ProbeScope::AllTraits,
1147+
)
1148+
.is_ok()
1149+
},
1150+
candidate_field,
1151+
substs,
1152+
vec![],
1153+
self.tcx.parent_module(expr.hir_id).to_def_id(),
1154+
) {
1155+
let field_path_str = field_path
1156+
.iter()
1157+
.map(|id| id.name.to_ident_string())
1158+
.collect::<Vec<String>>()
1159+
.join(".");
1160+
debug!("field_path_str: {:?}", field_path_str);
1161+
1162+
err.span_suggestion_verbose(
1163+
item_name.span.shrink_to_lo(),
1164+
"one of the expressions' fields has a method of the same name",
1165+
format!("{field_path_str}."),
1166+
Applicability::MaybeIncorrect,
1167+
);
1168+
}
1169+
}
1170+
}
1171+
11321172
bound_spans.sort();
11331173
bound_spans.dedup();
11341174
for (span, msg) in bound_spans.into_iter() {

library/alloc/src/borrow.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,7 @@ impl<B: ?Sized + ToOwned> Cow<'_, B> {
292292
///
293293
/// # Examples
294294
///
295-
/// Calling `into_owned` on a `Cow::Borrowed` clones the underlying data
296-
/// and becomes a `Cow::Owned`:
295+
/// Calling `into_owned` on a `Cow::Borrowed` returns a clone of the borrowed data:
297296
///
298297
/// ```
299298
/// use std::borrow::Cow;
@@ -307,7 +306,8 @@ impl<B: ?Sized + ToOwned> Cow<'_, B> {
307306
/// );
308307
/// ```
309308
///
310-
/// Calling `into_owned` on a `Cow::Owned` is a no-op:
309+
/// Calling `into_owned` on a `Cow::Owned` returns the owned data. The data is moved out of the
310+
/// `Cow` without being cloned.
311311
///
312312
/// ```
313313
/// use std::borrow::Cow;

0 commit comments

Comments
 (0)