Skip to content

Commit 3fcf43b

Browse files
committed
Auto merge of rust-lang#98669 - Dylan-DPC:rollup-8uzhcip, r=Dylan-DPC
Rollup of 7 pull requests Successful merges: - rust-lang#98415 (Migrate some `rustc_borrowck` diagnostics to `SessionDiagnostic`) - rust-lang#98479 (Add `fetch_not` method on `AtomicBool`) - rust-lang#98499 (Erase regions in New Abstract Consts) - rust-lang#98516 (library: fix uefi va_list type definition) - rust-lang#98554 (Fix box with custom allocator in miri) - rust-lang#98607 (Clean up arg mismatch diagnostic, generalize tuple wrap suggestion) - rust-lang#98625 (emit Retag for compound types with reference fields) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 66c83ff + 68228be commit 3fcf43b

39 files changed

+511
-271
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -3595,6 +3595,7 @@ dependencies = [
35953595
"rustc_index",
35963596
"rustc_infer",
35973597
"rustc_lexer",
3598+
"rustc_macros",
35983599
"rustc_middle",
35993600
"rustc_mir_dataflow",
36003601
"rustc_serialize",

compiler/rustc_borrowck/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ rustc_hir = { path = "../rustc_hir" }
1919
rustc_index = { path = "../rustc_index" }
2020
rustc_infer = { path = "../rustc_infer" }
2121
rustc_lexer = { path = "../rustc_lexer" }
22+
rustc_macros = { path = "../rustc_macros" }
2223
rustc_middle = { path = "../rustc_middle" }
2324
rustc_const_eval = { path = "../rustc_const_eval" }
2425
rustc_mir_dataflow = { path = "../rustc_mir_dataflow" }

compiler/rustc_borrowck/src/diagnostics/bound_region_errors.rs

+18-9
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ use std::fmt;
1919
use std::rc::Rc;
2020

2121
use crate::region_infer::values::RegionElement;
22+
use crate::session_diagnostics::HigherRankedErrorCause;
23+
use crate::session_diagnostics::HigherRankedLifetimeError;
24+
use crate::session_diagnostics::HigherRankedSubtypeError;
2225
use crate::MirBorrowckCtxt;
2326

2427
#[derive(Clone)]
@@ -69,7 +72,7 @@ impl<'tcx> UniverseInfo<'tcx> {
6972
// up in the existing UI tests. Consider investigating this
7073
// some more.
7174
mbcx.buffer_error(
72-
mbcx.infcx.tcx.sess.struct_span_err(cause.span, "higher-ranked subtype error"),
75+
mbcx.infcx.tcx.sess.create_err(HigherRankedSubtypeError { span: cause.span }),
7376
);
7477
}
7578
}
@@ -216,9 +219,12 @@ impl<'tcx> TypeOpInfo<'tcx> for PredicateQuery<'tcx> {
216219
tcx: TyCtxt<'tcx>,
217220
span: Span,
218221
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
219-
let mut err = tcx.sess.struct_span_err(span, "higher-ranked lifetime error");
220-
err.note(&format!("could not prove {}", self.canonical_query.value.value.predicate));
221-
err
222+
tcx.sess.create_err(HigherRankedLifetimeError {
223+
cause: Some(HigherRankedErrorCause::CouldNotProve {
224+
predicate: self.canonical_query.value.value.predicate.to_string(),
225+
}),
226+
span,
227+
})
222228
}
223229

224230
fn base_universe(&self) -> ty::UniverseIndex {
@@ -263,9 +269,12 @@ where
263269
tcx: TyCtxt<'tcx>,
264270
span: Span,
265271
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
266-
let mut err = tcx.sess.struct_span_err(span, "higher-ranked lifetime error");
267-
err.note(&format!("could not normalize `{}`", self.canonical_query.value.value.value));
268-
err
272+
tcx.sess.create_err(HigherRankedLifetimeError {
273+
cause: Some(HigherRankedErrorCause::CouldNotNormalize {
274+
value: self.canonical_query.value.value.value.to_string(),
275+
}),
276+
span,
277+
})
269278
}
270279

271280
fn base_universe(&self) -> ty::UniverseIndex {
@@ -326,7 +335,7 @@ impl<'tcx> TypeOpInfo<'tcx> for AscribeUserTypeQuery<'tcx> {
326335
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
327336
// FIXME: This error message isn't great, but it doesn't show up in the existing UI tests,
328337
// and is only the fallback when the nice error fails. Consider improving this some more.
329-
tcx.sess.struct_span_err(span, "higher-ranked lifetime error")
338+
tcx.sess.create_err(HigherRankedLifetimeError { cause: None, span })
330339
}
331340

332341
fn base_universe(&self) -> ty::UniverseIndex {
@@ -366,7 +375,7 @@ impl<'tcx> TypeOpInfo<'tcx> for crate::type_check::InstantiateOpaqueType<'tcx> {
366375
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
367376
// FIXME: This error message isn't great, but it doesn't show up in the existing UI tests,
368377
// and is only the fallback when the nice error fails. Consider improving this some more.
369-
tcx.sess.struct_span_err(span, "higher-ranked lifetime error for opaque type!")
378+
tcx.sess.create_err(HigherRankedLifetimeError { cause: None, span })
370379
}
371380

372381
fn base_universe(&self) -> ty::UniverseIndex {

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use rustc_span::symbol::Ident;
2424
use rustc_span::Span;
2525

2626
use crate::borrowck_errors;
27+
use crate::session_diagnostics::GenericDoesNotLiveLongEnough;
2728

2829
use super::{OutlivesSuggestionBuilder, RegionName};
2930
use crate::region_infer::BlameConstraint;
@@ -196,9 +197,11 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
196197
// to report it; we could probably handle it by
197198
// iterating over the universal regions and reporting
198199
// an error that multiple bounds are required.
199-
self.buffer_error(self.infcx.tcx.sess.struct_span_err(
200-
type_test_span,
201-
&format!("`{}` does not live long enough", type_test.generic_kind),
200+
self.buffer_error(self.infcx.tcx.sess.create_err(
201+
GenericDoesNotLiveLongEnough {
202+
kind: type_test.generic_kind.to_string(),
203+
span: type_test_span,
204+
},
202205
));
203206
}
204207
}

compiler/rustc_borrowck/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ mod places_conflict;
7676
mod prefixes;
7777
mod region_infer;
7878
mod renumber;
79+
mod session_diagnostics;
7980
mod type_check;
8081
mod universal_regions;
8182
mod used_muts;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
use rustc_macros::{SessionDiagnostic, SessionSubdiagnostic};
2+
use rustc_middle::ty::Ty;
3+
use rustc_span::Span;
4+
5+
#[derive(SessionDiagnostic)]
6+
#[error(borrowck::move_unsized, code = "E0161")]
7+
pub(crate) struct MoveUnsized<'tcx> {
8+
pub ty: Ty<'tcx>,
9+
#[primary_span]
10+
#[label]
11+
pub span: Span,
12+
}
13+
14+
#[derive(SessionDiagnostic)]
15+
#[error(borrowck::higher_ranked_lifetime_error)]
16+
pub(crate) struct HigherRankedLifetimeError {
17+
#[subdiagnostic]
18+
pub cause: Option<HigherRankedErrorCause>,
19+
#[primary_span]
20+
pub span: Span,
21+
}
22+
23+
#[derive(SessionSubdiagnostic)]
24+
pub(crate) enum HigherRankedErrorCause {
25+
#[note(borrowck::could_not_prove)]
26+
CouldNotProve { predicate: String },
27+
#[note(borrowck::could_not_normalize)]
28+
CouldNotNormalize { value: String },
29+
}
30+
31+
#[derive(SessionDiagnostic)]
32+
#[error(borrowck::higher_ranked_subtype_error)]
33+
pub(crate) struct HigherRankedSubtypeError {
34+
#[primary_span]
35+
pub span: Span,
36+
}
37+
38+
#[derive(SessionDiagnostic)]
39+
#[error(borrowck::generic_does_not_live_long_enough)]
40+
pub(crate) struct GenericDoesNotLiveLongEnough {
41+
pub kind: String,
42+
#[primary_span]
43+
pub span: Span,
44+
}

compiler/rustc_borrowck/src/type_check/mod.rs

+2-11
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use hir::OpaqueTyOrigin;
99
use rustc_data_structures::frozen::Frozen;
1010
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
1111
use rustc_data_structures::vec_map::VecMap;
12-
use rustc_errors::struct_span_err;
1312
use rustc_hir as hir;
1413
use rustc_hir::def::DefKind;
1514
use rustc_hir::def_id::LocalDefId;
@@ -48,6 +47,7 @@ use rustc_mir_dataflow::impls::MaybeInitializedPlaces;
4847
use rustc_mir_dataflow::move_paths::MoveData;
4948
use rustc_mir_dataflow::ResultsCursor;
5049

50+
use crate::session_diagnostics::MoveUnsized;
5151
use crate::{
5252
borrow_set::BorrowSet,
5353
constraints::{OutlivesConstraint, OutlivesConstraintSet},
@@ -1780,19 +1780,10 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
17801780
// slot or local, so to find all unsized rvalues it is enough
17811781
// to check all temps, return slots and locals.
17821782
if self.reported_errors.replace((ty, span)).is_none() {
1783-
let mut diag = struct_span_err!(
1784-
self.tcx().sess,
1785-
span,
1786-
E0161,
1787-
"cannot move a value of type {0}: the size of {0} \
1788-
cannot be statically determined",
1789-
ty
1790-
);
1791-
17921783
// While this is located in `nll::typeck` this error is not
17931784
// an NLL error, it's a required check to prevent creation
17941785
// of unsized rvalues in a call expression.
1795-
diag.emit();
1786+
self.tcx().sess.emit_err(MoveUnsized { ty, span });
17961787
}
17971788
}
17981789
}

compiler/rustc_const_eval/src/interpret/cast.rs

-16
Original file line numberDiff line numberDiff line change
@@ -366,22 +366,6 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
366366
}
367367
(&ty::Adt(def_a, _), &ty::Adt(def_b, _)) => {
368368
assert_eq!(def_a, def_b);
369-
if def_a.is_box() || def_b.is_box() {
370-
if !def_a.is_box() || !def_b.is_box() {
371-
span_bug!(
372-
self.cur_span(),
373-
"invalid unsizing between {:?} -> {:?}",
374-
src.layout.ty,
375-
cast_ty.ty
376-
);
377-
}
378-
return self.unsize_into_ptr(
379-
src,
380-
dest,
381-
src.layout.ty.boxed_ty(),
382-
cast_ty.ty.boxed_ty(),
383-
);
384-
}
385369

386370
// unsizing of generic struct with pointer fields
387371
// Example: `Arc<T>` -> `Arc<Trait>`

compiler/rustc_const_eval/src/interpret/validity.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,13 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
594594
Ok(true)
595595
}
596596
ty::Adt(def, ..) if def.is_box() => {
597-
self.check_safe_pointer(value, "box")?;
597+
let unique = self.ecx.operand_field(value, 0)?;
598+
let nonnull = self.ecx.operand_field(&unique, 0)?;
599+
let ptr = self.ecx.operand_field(&nonnull, 0)?;
600+
self.check_safe_pointer(&ptr, "box")?;
601+
602+
// Check other fields of Box
603+
self.walk_value(value)?;
598604
Ok(true)
599605
}
600606
ty::FnPtr(_sig) => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
borrowck-move-unsized =
2+
cannot move a value of type `{$ty}`
3+
.label = the size of `{$ty}` cannot be statically determined
4+
5+
borrowck-higher-ranked-lifetime-error =
6+
higher-ranked lifetime error
7+
8+
borrowck-could-not-prove =
9+
could not prove `{$predicate}`
10+
11+
borrowck-could-not-normalize =
12+
could not normalize `{$value}`
13+
14+
borrowck-higher-ranked-subtype-error =
15+
higher-ranked subtype error
16+
17+
generic-does-not-live-long-enough =
18+
`{$kind}` does not live long enough

compiler/rustc_error_messages/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ fluent_messages! {
3535
privacy => "../locales/en-US/privacy.ftl",
3636
typeck => "../locales/en-US/typeck.ftl",
3737
builtin_macros => "../locales/en-US/builtin_macros.ftl",
38+
borrowck => "../locales/en-US/borrowck.ftl",
3839
}
3940

4041
pub use fluent_generated::{self as fluent, DEFAULT_LOCALE_RESOURCES};

compiler/rustc_errors/src/emitter.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -281,9 +281,19 @@ pub trait Emitter {
281281
let message = bundle.get_message(&identifier).expect("missing diagnostic in fluent bundle");
282282
let value = match attr {
283283
Some(attr) => {
284-
message.get_attribute(attr).expect("missing attribute in fluent message").value()
284+
if let Some(attr) = message.get_attribute(attr) {
285+
attr.value()
286+
} else {
287+
panic!("missing attribute `{attr}` in fluent message `{identifier}`")
288+
}
289+
}
290+
None => {
291+
if let Some(value) = message.value() {
292+
value
293+
} else {
294+
panic!("missing value in fluent message `{identifier}`")
295+
}
285296
}
286-
None => message.value().expect("missing value in fluent message"),
287297
};
288298

289299
let mut err = vec![];

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -342,8 +342,8 @@ pub fn same_type_modulo_infer<'tcx>(a: Ty<'tcx>, b: Ty<'tcx>) -> bool {
342342
)
343343
| (&ty::Infer(ty::InferTy::TyVar(_)), _)
344344
| (_, &ty::Infer(ty::InferTy::TyVar(_))) => true,
345-
(&ty::Ref(reg_a, ty_a, mut_a), &ty::Ref(reg_b, ty_b, mut_b)) => {
346-
reg_a == reg_b && mut_a == mut_b && same_type_modulo_infer(*ty_a, *ty_b)
345+
(&ty::Ref(_, ty_a, mut_a), &ty::Ref(_, ty_b, mut_b)) => {
346+
mut_a == mut_b && same_type_modulo_infer(*ty_a, *ty_b)
347347
}
348348
_ => a == b,
349349
}

compiler/rustc_mir_transform/src/add_retag.rs

+18-5
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ fn is_stable(place: PlaceRef<'_>) -> bool {
3333
})
3434
}
3535

36-
/// Determine whether this type may be a reference (or box), and thus needs retagging.
37-
fn may_be_reference(ty: Ty<'_>) -> bool {
36+
/// Determine whether this type may contain a reference (or box), and thus needs retagging.
37+
/// We will only recurse `depth` times into Tuples/ADTs to bound the cost of this.
38+
fn may_contain_reference<'tcx>(ty: Ty<'tcx>, depth: u32, tcx: TyCtxt<'tcx>) -> bool {
3839
match ty.kind() {
3940
// Primitive types that are not references
4041
ty::Bool
@@ -50,8 +51,20 @@ fn may_be_reference(ty: Ty<'_>) -> bool {
5051
// References
5152
ty::Ref(..) => true,
5253
ty::Adt(..) if ty.is_box() => true,
53-
// Compound types are not references
54-
ty::Array(..) | ty::Slice(..) | ty::Tuple(..) | ty::Adt(..) => false,
54+
// Compound types: recurse
55+
ty::Array(ty, _) | ty::Slice(ty) => {
56+
// This does not branch so we keep the depth the same.
57+
may_contain_reference(*ty, depth, tcx)
58+
}
59+
ty::Tuple(tys) => {
60+
depth == 0 || tys.iter().any(|ty| may_contain_reference(ty, depth - 1, tcx))
61+
}
62+
ty::Adt(adt, subst) => {
63+
depth == 0
64+
|| adt.variants().iter().any(|v| {
65+
v.fields.iter().any(|f| may_contain_reference(f.ty(tcx, subst), depth - 1, tcx))
66+
})
67+
}
5568
// Conservative fallback
5669
_ => true,
5770
}
@@ -83,7 +96,7 @@ impl<'tcx> MirPass<'tcx> for AddRetag {
8396
// FIXME: Instead of giving up for unstable places, we should introduce
8497
// a temporary and retag on that.
8598
is_stable(place.as_ref())
86-
&& may_be_reference(place.ty(&*local_decls, tcx).ty)
99+
&& may_contain_reference(place.ty(&*local_decls, tcx).ty, /*depth*/ 3, tcx)
87100
&& is_not_temp(&local_decls[place.local])
88101
};
89102
let place_base_raw = |place: &Place<'tcx>| {

compiler/rustc_trait_selection/src/traits/const_evaluatable.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ impl<'tcx> AbstractConst<'tcx> {
236236
) -> Result<Option<AbstractConst<'tcx>>, ErrorGuaranteed> {
237237
let inner = tcx.thir_abstract_const_opt_const_arg(uv.def)?;
238238
debug!("AbstractConst::new({:?}) = {:?}", uv, inner);
239-
Ok(inner.map(|inner| AbstractConst { inner, substs: uv.substs }))
239+
Ok(inner.map(|inner| AbstractConst { inner, substs: tcx.erase_regions(uv.substs) }))
240240
}
241241

242242
pub fn from_const(
@@ -416,6 +416,7 @@ impl<'a, 'tcx> AbstractConstBuilder<'a, 'tcx> {
416416
// `AbstractConst`s should not contain any promoteds as they require references which
417417
// are not allowed.
418418
assert_eq!(ct.promoted, None);
419+
assert_eq!(ct, self.tcx.erase_regions(ct));
419420
}
420421
}
421422
}

0 commit comments

Comments
 (0)