Skip to content

Commit 37b55c8

Browse files
committed
Auto merge of #95250 - matthiaskrgr:rollup-ma4zl69, r=matthiaskrgr
Rollup of 5 pull requests Successful merges: - #94249 (Better errors when a Copy impl on a Struct is not self-consistent) - #95069 (Fix auto traits in rustdoc) - #95221 (interpret/memory: simplify check_and_deref_ptr) - #95225 (remove `[async output]` from `impl Future` pretty-printing) - #95238 (Stop emitting E0026 for struct enums with underscores) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 9f4dc0b + fab7a6a commit 37b55c8

34 files changed

+203
-66
lines changed

compiler/rustc_const_eval/src/interpret/memory.rs

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -427,22 +427,12 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
427427
}
428428
}
429429

430-
// Extract from the pointer an `Option<AllocId>` and an offset, which is relative to the
431-
// allocation or (if that is `None`) an absolute address.
432-
let ptr_or_addr = if size.bytes() == 0 {
433-
// Let's see what we can do, but don't throw errors if there's nothing there.
434-
self.ptr_try_get_alloc(ptr)
435-
} else {
436-
// A "real" access, we insist on getting an `AllocId`.
437-
Ok(self.ptr_get_alloc(ptr)?)
438-
};
439-
Ok(match ptr_or_addr {
430+
Ok(match self.ptr_try_get_alloc(ptr) {
440431
Err(addr) => {
441-
// No memory is actually being accessed.
442-
debug_assert!(size.bytes() == 0);
443-
// Must be non-null.
444-
if addr == 0 {
445-
throw_ub!(DanglingIntPointer(0, msg))
432+
// We couldn't get a proper allocation. This is only okay if the access size is 0,
433+
// and the address is not null.
434+
if size.bytes() > 0 || addr == 0 {
435+
throw_ub!(DanglingIntPointer(addr, msg));
446436
}
447437
// Must be aligned.
448438
if let Some(align) = align {

compiler/rustc_middle/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#![feature(new_uninit)]
4242
#![feature(nll)]
4343
#![feature(once_cell)]
44+
#![feature(let_chains)]
4445
#![feature(let_else)]
4546
#![feature(min_specialization)]
4647
#![feature(trusted_len)]

compiler/rustc_middle/src/ty/print/pretty.rs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -896,44 +896,48 @@ pub trait PrettyPrinter<'tcx>:
896896
);
897897

898898
if !generics.is_empty() || !assoc_items.is_empty() {
899-
p!("<");
900899
let mut first = true;
901900

902901
for ty in generics {
903-
if !first {
902+
if first {
903+
p!("<");
904+
first = false;
905+
} else {
904906
p!(", ");
905907
}
906908
p!(print(trait_ref.rebind(*ty)));
907-
first = false;
908909
}
909910

910911
for (assoc_item_def_id, term) in assoc_items {
911-
if !first {
912+
// Skip printing `<[generator@] as Generator<_>>::Return` from async blocks
913+
if let Some(ty) = term.skip_binder().ty() &&
914+
let ty::Projection(ty::ProjectionTy { item_def_id, .. }) = ty.kind() &&
915+
Some(*item_def_id) == self.tcx().lang_items().generator_return() {
916+
continue;
917+
}
918+
919+
if first {
920+
p!("<");
921+
first = false;
922+
} else {
912923
p!(", ");
913924
}
925+
914926
p!(write("{} = ", self.tcx().associated_item(assoc_item_def_id).name));
915927

916928
match term.skip_binder() {
917929
Term::Ty(ty) => {
918-
// Skip printing `<[generator@] as Generator<_>>::Return` from async blocks
919-
if matches!(
920-
ty.kind(), ty::Projection(ty::ProjectionTy { item_def_id, .. })
921-
if Some(*item_def_id) == self.tcx().lang_items().generator_return()
922-
) {
923-
p!("[async output]")
924-
} else {
925-
p!(print(ty))
926-
}
930+
p!(print(ty))
927931
}
928932
Term::Const(c) => {
929933
p!(print(c));
930934
}
931935
};
932-
933-
first = false;
934936
}
935937

936-
p!(">");
938+
if !first {
939+
p!(">");
940+
}
937941
}
938942

939943
first = false;

compiler/rustc_trait_selection/src/traits/misc.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::traits::error_reporting::InferCtxtExt;
1111

1212
#[derive(Clone)]
1313
pub enum CopyImplementationError<'tcx> {
14-
InfrigingFields(Vec<&'tcx ty::FieldDef>),
14+
InfrigingFields(Vec<(&'tcx ty::FieldDef, Ty<'tcx>)>),
1515
NotAnAdt,
1616
HasDestructor,
1717
}
@@ -67,7 +67,7 @@ pub fn can_type_implement_copy<'tcx>(
6767
match traits::fully_normalize(&infcx, ctx, cause, param_env, ty) {
6868
Ok(ty) => {
6969
if !infcx.type_is_copy_modulo_regions(param_env, ty, span) {
70-
infringing.push(field);
70+
infringing.push((field, ty));
7171
}
7272
}
7373
Err(errors) => {

compiler/rustc_trait_selection/src/traits/select/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,7 +1270,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
12701270
// the master cache. Since coherence executes pretty quickly,
12711271
// it's not worth going to more trouble to increase the
12721272
// hit-rate, I don't think.
1273-
if self.intercrate {
1273+
if self.intercrate || self.allow_negative_impls {
12741274
return false;
12751275
}
12761276

@@ -1287,7 +1287,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
12871287
// mode, so don't do any caching. In particular, we might
12881288
// re-use the same `InferCtxt` with both an intercrate
12891289
// and non-intercrate `SelectionContext`
1290-
if self.intercrate {
1290+
if self.intercrate || self.allow_negative_impls {
12911291
return None;
12921292
}
12931293
let tcx = self.tcx();

compiler/rustc_typeck/src/check/pat.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc_session::lint::builtin::NON_EXHAUSTIVE_OMITTED_PATTERNS;
1717
use rustc_span::hygiene::DesugaringKind;
1818
use rustc_span::lev_distance::find_best_match_for_name;
1919
use rustc_span::source_map::{Span, Spanned};
20-
use rustc_span::symbol::{sym, Ident};
20+
use rustc_span::symbol::{kw, sym, Ident};
2121
use rustc_span::{BytePos, MultiSpan, DUMMY_SP};
2222
use rustc_trait_selection::autoderef::Autoderef;
2323
use rustc_trait_selection::traits::{ObligationCause, Pattern};
@@ -1275,7 +1275,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12751275
.filter(|(_, ident)| !used_fields.contains_key(ident))
12761276
.collect::<Vec<_>>();
12771277

1278-
let inexistent_fields_err = if !(inexistent_fields.is_empty() || variant.is_recovered()) {
1278+
let inexistent_fields_err = if !(inexistent_fields.is_empty() || variant.is_recovered())
1279+
&& !inexistent_fields.iter().any(|field| field.name == kw::Underscore)
1280+
{
12791281
Some(self.error_inexistent_fields(
12801282
adt.variant_descr(),
12811283
&inexistent_fields,

compiler/rustc_typeck/src/coherence/builtin.rs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,40 @@ fn visit_implementation_of_copy(tcx: TyCtxt<'_>, impl_did: LocalDefId) {
9191
E0204,
9292
"the trait `Copy` may not be implemented for this type"
9393
);
94-
for span in fields.iter().map(|f| tcx.def_span(f.did)) {
95-
err.span_label(span, "this field does not implement `Copy`");
94+
for (field, ty) in fields {
95+
let field_span = tcx.def_span(field.did);
96+
err.span_label(field_span, "this field does not implement `Copy`");
97+
// Spin up a new FulfillmentContext, so we can get the _precise_ reason
98+
// why this field does not implement Copy. This is useful because sometimes
99+
// it is not immediately clear why Copy is not implemented for a field, since
100+
// all we point at is the field itself.
101+
tcx.infer_ctxt().enter(|infcx| {
102+
let mut fulfill_cx = traits::FulfillmentContext::new_ignoring_regions();
103+
fulfill_cx.register_bound(
104+
&infcx,
105+
param_env,
106+
ty,
107+
tcx.lang_items().copy_trait().unwrap(),
108+
traits::ObligationCause::dummy_with_span(field_span),
109+
);
110+
for error in fulfill_cx.select_all_or_error(&infcx) {
111+
let error_predicate = error.obligation.predicate;
112+
// Only note if it's not the root obligation, otherwise it's trivial and
113+
// should be self-explanatory (i.e. a field literally doesn't implement Copy).
114+
115+
// FIXME: This error could be more descriptive, especially if the error_predicate
116+
// contains a foreign type or if it's a deeply nested type...
117+
if error_predicate != error.root_obligation.predicate {
118+
err.span_note(
119+
error.obligation.cause.span,
120+
&format!(
121+
"the `Copy` impl for `{}` requires that `{}`",
122+
ty, error_predicate
123+
),
124+
);
125+
}
126+
}
127+
});
96128
}
97129
err.emit();
98130
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#![crate_name = "foo"]
2+
3+
// @has 'foo/struct.Foo.html'
4+
// @has - '//*[@id="impl-Send"]' 'impl !Send for Foo'
5+
// @has - '//*[@id="impl-Sync"]' 'impl !Sync for Foo'
6+
pub struct Foo(*const i8);
7+
pub trait Whatever: Send {}
8+
impl<T: Send + ?Sized> Whatever for T {}

src/test/ui/async-await/async-block-control-flow-static-semantics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ async fn return_targets_async_block_not_async_fn() -> u8 {
2424
return 0u8;
2525
};
2626
let _: &dyn Future<Output = ()> = &block;
27-
//~^ ERROR type mismatch resolving `<impl Future<Output = [async output]> as Future>::Output == ()`
27+
//~^ ERROR type mismatch resolving `<impl Future as Future>::Output == ()`
2828
}
2929

3030
fn no_break_in_async_block() {

src/test/ui/async-await/async-block-control-flow-static-semantics.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ LL | |
3131
LL | | }
3232
| |_^ expected `u8`, found `()`
3333

34-
error[E0271]: type mismatch resolving `<impl Future<Output = [async output]> as Future>::Output == ()`
34+
error[E0271]: type mismatch resolving `<impl Future as Future>::Output == ()`
3535
--> $DIR/async-block-control-flow-static-semantics.rs:26:39
3636
|
3737
LL | let _: &dyn Future<Output = ()> = &block;
@@ -47,7 +47,7 @@ LL | fn return_targets_async_block_not_fn() -> u8 {
4747
| |
4848
| implicitly returns `()` as its body has no tail or `return` expression
4949

50-
error[E0271]: type mismatch resolving `<impl Future<Output = [async output]> as Future>::Output == ()`
50+
error[E0271]: type mismatch resolving `<impl Future as Future>::Output == ()`
5151
--> $DIR/async-block-control-flow-static-semantics.rs:17:39
5252
|
5353
LL | let _: &dyn Future<Output = ()> = &block;

0 commit comments

Comments
 (0)