Skip to content

Commit f9515fd

Browse files
committed
Auto merge of rust-lang#126473 - matthiaskrgr:rollup-8w2xm09, r=matthiaskrgr
Rollup of 7 pull requests Successful merges: - rust-lang#123769 (Improve escaping of byte, byte str, and c str proc-macro literals) - rust-lang#126054 (`E0229`: Suggest Moving Type Constraints to Type Parameter Declaration) - rust-lang#126135 (add HermitOS support for vectored read/write operations) - rust-lang#126266 (Unify guarantees about the default allocator) - rust-lang#126285 (`UniqueRc`: support allocators and `T: ?Sized`.) - rust-lang#126399 (extend the check for LLVM build) - rust-lang#126426 (const validation: fix ICE on dangling ZST reference) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 63491e1 + aebd794 commit f9515fd

40 files changed

+788
-321
lines changed

Cargo.lock

+8-2
Original file line numberDiff line numberDiff line change
@@ -1694,6 +1694,12 @@ name = "hermit-abi"
16941694
version = "0.3.9"
16951695
source = "registry+https://github.com/rust-lang/crates.io-index"
16961696
checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024"
1697+
1698+
[[package]]
1699+
name = "hermit-abi"
1700+
version = "0.4.0"
1701+
source = "registry+https://github.com/rust-lang/crates.io-index"
1702+
checksum = "fbf6a919d6cf397374f7dfeeea91d974c7c0a7221d0d0f4f20d859d329e53fcc"
16971703
dependencies = [
16981704
"compiler_builtins",
16991705
"rustc-std-workspace-alloc",
@@ -2636,7 +2642,7 @@ version = "1.16.0"
26362642
source = "registry+https://github.com/rust-lang/crates.io-index"
26372643
checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43"
26382644
dependencies = [
2639-
"hermit-abi",
2645+
"hermit-abi 0.3.9",
26402646
"libc",
26412647
]
26422648

@@ -5345,7 +5351,7 @@ dependencies = [
53455351
"dlmalloc",
53465352
"fortanix-sgx-abi",
53475353
"hashbrown",
5348-
"hermit-abi",
5354+
"hermit-abi 0.4.0",
53495355
"libc",
53505356
"miniz_oxide",
53515357
"object 0.36.0",

compiler/rustc_const_eval/src/interpret/validity.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use rustc_target::abi::{
2929
use std::hash::Hash;
3030

3131
use super::{
32-
err_ub, format_interp_error, machine::AllocMap, throw_ub, AllocId, CheckInAllocMsg,
32+
err_ub, format_interp_error, machine::AllocMap, throw_ub, AllocId, AllocKind, CheckInAllocMsg,
3333
GlobalAlloc, ImmTy, Immediate, InterpCx, InterpResult, MPlaceTy, Machine, MemPlaceMeta, OpTy,
3434
Pointer, Projectable, Scalar, ValueVisitor,
3535
};
@@ -413,8 +413,6 @@ impl<'rt, 'tcx, M: Machine<'tcx>> ValidityVisitor<'rt, 'tcx, M> {
413413
Ub(PointerOutOfBounds { .. }) => DanglingPtrOutOfBounds {
414414
ptr_kind
415415
},
416-
// This cannot happen during const-eval (because interning already detects
417-
// dangling pointers), but it can happen in Miri.
418416
Ub(PointerUseAfterFree(..)) => DanglingPtrUseAfterFree {
419417
ptr_kind,
420418
},
@@ -493,9 +491,17 @@ impl<'rt, 'tcx, M: Machine<'tcx>> ValidityVisitor<'rt, 'tcx, M> {
493491
}
494492
}
495493

496-
// Mutability check.
494+
// Dangling and Mutability check.
495+
let (size, _align, alloc_kind) = self.ecx.get_alloc_info(alloc_id);
496+
if alloc_kind == AllocKind::Dead {
497+
// This can happen for zero-sized references. We can't have *any* references to non-existing
498+
// allocations though, interning rejects them all as the rest of rustc isn't happy with them...
499+
// so we throw an error, even though this isn't really UB.
500+
// A potential future alternative would be to resurrect this as a zero-sized allocation
501+
// (which codegen will then compile to an aligned dummy pointer anyway).
502+
throw_validation_failure!(self.path, DanglingPtrUseAfterFree { ptr_kind });
503+
}
497504
// If this allocation has size zero, there is no actual mutability here.
498-
let (size, _align, _alloc_kind) = self.ecx.get_alloc_info(alloc_id);
499505
if size != Size::ZERO {
500506
let alloc_actual_mutbl = mutability(self.ecx, alloc_id);
501507
// Mutable pointer to immutable memory is no good.

compiler/rustc_hir/src/hir.rs

+24
Original file line numberDiff line numberDiff line change
@@ -2471,6 +2471,15 @@ pub enum AssocItemConstraintKind<'hir> {
24712471
Bound { bounds: &'hir [GenericBound<'hir>] },
24722472
}
24732473

2474+
impl<'hir> AssocItemConstraintKind<'hir> {
2475+
pub fn descr(&self) -> &'static str {
2476+
match self {
2477+
AssocItemConstraintKind::Equality { .. } => "binding",
2478+
AssocItemConstraintKind::Bound { .. } => "constraint",
2479+
}
2480+
}
2481+
}
2482+
24742483
#[derive(Debug, Clone, Copy, HashStable_Generic)]
24752484
pub struct Ty<'hir> {
24762485
pub hir_id: HirId,
@@ -3763,6 +3772,21 @@ impl<'hir> Node<'hir> {
37633772
}
37643773
}
37653774

3775+
/// Get a `hir::Impl` if the node is an impl block for the given `trait_def_id`.
3776+
pub fn impl_block_of_trait(self, trait_def_id: DefId) -> Option<&'hir Impl<'hir>> {
3777+
match self {
3778+
Node::Item(Item { kind: ItemKind::Impl(impl_block), .. })
3779+
if impl_block
3780+
.of_trait
3781+
.and_then(|trait_ref| trait_ref.trait_def_id())
3782+
.is_some_and(|trait_id| trait_id == trait_def_id) =>
3783+
{
3784+
Some(impl_block)
3785+
}
3786+
_ => None,
3787+
}
3788+
}
3789+
37663790
pub fn fn_sig(self) -> Option<&'hir FnSig<'hir>> {
37673791
match self {
37683792
Node::TraitItem(TraitItem { kind: TraitItemKind::Fn(fn_sig, _), .. })

compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs

+62-9
Original file line numberDiff line numberDiff line change
@@ -1217,7 +1217,6 @@ pub fn prohibit_assoc_item_constraint(
12171217
// otherwise suggest the removal of the binding.
12181218
if let Some((def_id, segment, _)) = segment
12191219
&& segment.args().parenthesized == hir::GenericArgsParentheses::No
1220-
&& let hir::AssocItemConstraintKind::Equality { term } = constraint.kind
12211220
{
12221221
// Suggests removal of the offending binding
12231222
let suggest_removal = |e: &mut Diag<'_>| {
@@ -1263,7 +1262,7 @@ pub fn prohibit_assoc_item_constraint(
12631262
if let Ok(suggestion) = tcx.sess.source_map().span_to_snippet(removal_span) {
12641263
e.span_suggestion_verbose(
12651264
removal_span,
1266-
"consider removing this associated item binding",
1265+
format!("consider removing this associated item {}", constraint.kind.descr()),
12671266
suggestion,
12681267
Applicability::MaybeIncorrect,
12691268
);
@@ -1286,19 +1285,73 @@ pub fn prohibit_assoc_item_constraint(
12861285
// Check if the type has a generic param with the same name
12871286
// as the assoc type name in the associated item binding.
12881287
let generics = tcx.generics_of(def_id);
1289-
let matching_param =
1290-
generics.own_params.iter().find(|p| p.name.as_str() == constraint.ident.as_str());
1288+
let matching_param = generics.own_params.iter().find(|p| p.name == constraint.ident.name);
12911289

12921290
// Now emit the appropriate suggestion
12931291
if let Some(matching_param) = matching_param {
1294-
match (&matching_param.kind, term) {
1295-
(GenericParamDefKind::Type { .. }, hir::Term::Ty(ty)) => {
1296-
suggest_direct_use(&mut err, ty.span);
1297-
}
1298-
(GenericParamDefKind::Const { .. }, hir::Term::Const(c)) => {
1292+
match (constraint.kind, &matching_param.kind) {
1293+
(
1294+
hir::AssocItemConstraintKind::Equality { term: hir::Term::Ty(ty) },
1295+
GenericParamDefKind::Type { .. },
1296+
) => suggest_direct_use(&mut err, ty.span),
1297+
(
1298+
hir::AssocItemConstraintKind::Equality { term: hir::Term::Const(c) },
1299+
GenericParamDefKind::Const { .. },
1300+
) => {
12991301
let span = tcx.hir().span(c.hir_id);
13001302
suggest_direct_use(&mut err, span);
13011303
}
1304+
(hir::AssocItemConstraintKind::Bound { bounds }, _) => {
1305+
// Suggest `impl<T: Bound> Trait<T> for Foo` when finding
1306+
// `impl Trait<T: Bound> for Foo`
1307+
1308+
// Get the parent impl block based on the binding we have
1309+
// and the trait DefId
1310+
let impl_block = tcx
1311+
.hir()
1312+
.parent_iter(constraint.hir_id)
1313+
.find_map(|(_, node)| node.impl_block_of_trait(def_id));
1314+
1315+
let type_with_constraints =
1316+
tcx.sess.source_map().span_to_snippet(constraint.span);
1317+
1318+
if let Some(impl_block) = impl_block
1319+
&& let Ok(type_with_constraints) = type_with_constraints
1320+
{
1321+
// Filter out the lifetime parameters because
1322+
// they should be declared before the type parameter
1323+
let lifetimes: String = bounds
1324+
.iter()
1325+
.filter_map(|bound| {
1326+
if let hir::GenericBound::Outlives(lifetime) = bound {
1327+
Some(format!("{lifetime}, "))
1328+
} else {
1329+
None
1330+
}
1331+
})
1332+
.collect();
1333+
// Figure out a span and suggestion string based on
1334+
// whether there are any existing parameters
1335+
let param_decl = if let Some(param_span) =
1336+
impl_block.generics.span_for_param_suggestion()
1337+
{
1338+
(param_span, format!(", {lifetimes}{type_with_constraints}"))
1339+
} else {
1340+
(
1341+
impl_block.generics.span.shrink_to_lo(),
1342+
format!("<{lifetimes}{type_with_constraints}>"),
1343+
)
1344+
};
1345+
let suggestions =
1346+
vec![param_decl, (constraint.span, format!("{}", matching_param.name))];
1347+
1348+
err.multipart_suggestion_verbose(
1349+
format!("declare the type parameter right after the `impl` keyword"),
1350+
suggestions,
1351+
Applicability::MaybeIncorrect,
1352+
);
1353+
}
1354+
}
13021355
_ => suggest_removal(&mut err),
13031356
}
13041357
} else {

compiler/rustc_hir_analysis/src/hir_ty_lowering/generics.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,7 @@ pub(crate) fn check_generic_arg_count(
531531

532532
let num_default_params = expected_max - expected_min;
533533

534+
let mut all_params_are_binded = false;
534535
let gen_args_info = if provided > expected_max {
535536
invalid_args.extend((expected_max..provided).map(|i| i + args_offset));
536537
let num_redundant_args = provided - expected_max;
@@ -547,6 +548,20 @@ pub(crate) fn check_generic_arg_count(
547548
} else {
548549
let num_missing_args = expected_max - provided;
549550

551+
let constraint_names: Vec<_> =
552+
gen_args.constraints.iter().map(|b| b.ident.name).collect();
553+
let param_names: Vec<_> = gen_params
554+
.own_params
555+
.iter()
556+
.filter(|param| !has_self || param.index != 0) // Assumes `Self` will always be the first parameter
557+
.map(|param| param.name)
558+
.collect();
559+
if constraint_names == param_names {
560+
// We set this to true and delay emitting `WrongNumberOfGenericArgs`
561+
// to provide a succinct error for cases like issue #113073
562+
all_params_are_binded = true;
563+
};
564+
550565
GenericArgsInfo::MissingTypesOrConsts {
551566
num_missing_args,
552567
num_default_params,
@@ -567,7 +582,7 @@ pub(crate) fn check_generic_arg_count(
567582
def_id,
568583
)
569584
.diagnostic()
570-
.emit()
585+
.emit_unless(all_params_are_binded)
571586
});
572587

573588
Err(reported)

0 commit comments

Comments
 (0)