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

+5-15
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

+1
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

+20-16
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

+2-2
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

+2-2
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

+4-2
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

+34-2
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
}
+8
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

+1-1
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

+2-2
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;

src/test/ui/async-await/generator-desc.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ LL | pub const fn from_generator<T>(gen: T) -> impl Future<Output = T::Return>
4646
| the expected opaque type
4747
| the found opaque type
4848
|
49-
= note: expected opaque type `impl Future<Output = [async output]>` (`async` closure body)
50-
found opaque type `impl Future<Output = [async output]>` (`async` closure body)
49+
= note: expected opaque type `impl Future` (`async` closure body)
50+
found opaque type `impl Future` (`async` closure body)
5151

5252
error: aborting due to 3 previous errors
5353

src/test/ui/async-await/issue-67252-unnamed-future.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: future cannot be sent between threads safely
44
LL | spawn(async {
55
| ^^^^^ future created by async block is not `Send`
66
|
7-
= help: within `impl Future<Output = [async output]>`, the trait `Send` is not implemented for `*mut ()`
7+
= help: within `impl Future`, the trait `Send` is not implemented for `*mut ()`
88
note: future is not `Send` as this value is used across an await
99
--> $DIR/issue-67252-unnamed-future.rs:20:16
1010
|

src/test/ui/async-await/issue-68112.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ LL | require_send(send_fut);
4444
= note: required because of the requirements on the impl of `Send` for `Arc<RefCell<i32>>`
4545
= note: required because it appears within the type `[static generator@$DIR/issue-68112.rs:47:31: 47:36]`
4646
= note: required because it appears within the type `from_generator::GenFuture<[static generator@$DIR/issue-68112.rs:47:31: 47:36]>`
47-
= note: required because it appears within the type `impl Future<Output = [async output]>`
47+
= note: required because it appears within the type `impl Future`
4848
= note: required because it appears within the type `impl Future<Output = Arc<RefCell<i32>>>`
4949
= note: required because it appears within the type `impl Future<Output = Arc<RefCell<i32>>>`
5050
= note: required because it appears within the type `{ResumeTy, impl Future<Output = Arc<RefCell<i32>>>, (), i32, Ready<i32>}`
5151
= note: required because it appears within the type `[static generator@$DIR/issue-68112.rs:55:26: 59:6]`
5252
= note: required because it appears within the type `from_generator::GenFuture<[static generator@$DIR/issue-68112.rs:55:26: 59:6]>`
53-
= note: required because it appears within the type `impl Future<Output = [async output]>`
53+
= note: required because it appears within the type `impl Future`
5454
note: required by a bound in `require_send`
5555
--> $DIR/issue-68112.rs:11:25
5656
|

src/test/ui/async-await/issues/issue-65436-raw-ptr-not-send.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: future cannot be sent between threads safely
44
LL | assert_send(async {
55
| ^^^^^^^^^^^ future created by async block is not `Send`
66
|
7-
= help: within `impl Future<Output = [async output]>`, the trait `Send` is not implemented for `*const u8`
7+
= help: within `impl Future`, the trait `Send` is not implemented for `*const u8`
88
note: future is not `Send` as this value is used across an await
99
--> $DIR/issue-65436-raw-ptr-not-send.rs:14:35
1010
|

src/test/ui/async-await/partial-drop-partial-reinit.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ LL | async fn foo() {
1414
= note: required because it appears within the type `{ResumeTy, (NotSend,), impl Future<Output = ()>, ()}`
1515
= note: required because it appears within the type `[static generator@$DIR/partial-drop-partial-reinit.rs:22:16: 27:2]`
1616
= note: required because it appears within the type `from_generator::GenFuture<[static generator@$DIR/partial-drop-partial-reinit.rs:22:16: 27:2]>`
17-
= note: required because it appears within the type `impl Future<Output = [async output]>`
17+
= note: required because it appears within the type `impl Future`
1818
= note: required because it appears within the type `impl Future<Output = ()>`
1919
note: required by a bound in `gimme_send`
2020
--> $DIR/partial-drop-partial-reinit.rs:10:18

src/test/ui/chalkify/bugs/async.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ note: required by a bound in `from_generator`
2828
LL | T: Generator<ResumeTy, Yield = ()>,
2929
| ^^^^^^^^^^ required by this bound in `from_generator`
3030

31-
error[E0280]: the requirement `<impl Future<Output = [async output]> as Future>::Output == u32` is not satisfied
31+
error[E0280]: the requirement `<impl Future as Future>::Output == u32` is not satisfied
3232
--> $DIR/async.rs:7:25
3333
|
3434
LL | async fn foo(x: u32) -> u32 {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#![feature(extern_types)]
2+
3+
extern "Rust" {
4+
type OpaqueListContents;
5+
}
6+
7+
pub struct ListS<T> {
8+
len: usize,
9+
data: [T; 0],
10+
opaque: OpaqueListContents,
11+
}
12+
13+
pub struct Interned<'a, T>(&'a T);
14+
15+
impl<'a, T> Clone for Interned<'a, T> {
16+
fn clone(&self) -> Self {
17+
*self
18+
}
19+
}
20+
21+
impl<'a, T> Copy for Interned<'a, T> {}
22+
23+
pub struct List<'tcx, T>(Interned<'tcx, ListS<T>>);
24+
//~^ NOTE this field does not implement `Copy`
25+
//~| NOTE the `Copy` impl for `Interned<'tcx, ListS<T>>` requires that `OpaqueListContents: Sized`
26+
27+
impl<'tcx, T> Clone for List<'tcx, T> {
28+
fn clone(&self) -> Self {
29+
*self
30+
}
31+
}
32+
33+
impl<'tcx, T> Copy for List<'tcx, T> {}
34+
//~^ ERROR the trait `Copy` may not be implemented for this type
35+
36+
fn assert_is_copy<T: Copy>() {}
37+
38+
fn main() {
39+
assert_is_copy::<List<'static, ()>>();
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error[E0204]: the trait `Copy` may not be implemented for this type
2+
--> $DIR/deep-bad-copy-reason.rs:33:15
3+
|
4+
LL | pub struct List<'tcx, T>(Interned<'tcx, ListS<T>>);
5+
| ------------------------ this field does not implement `Copy`
6+
...
7+
LL | impl<'tcx, T> Copy for List<'tcx, T> {}
8+
| ^^^^
9+
|
10+
note: the `Copy` impl for `Interned<'tcx, ListS<T>>` requires that `OpaqueListContents: Sized`
11+
--> $DIR/deep-bad-copy-reason.rs:23:26
12+
|
13+
LL | pub struct List<'tcx, T>(Interned<'tcx, ListS<T>>);
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^
15+
16+
error: aborting due to previous error
17+
18+
For more information about this error, try `rustc --explain E0204`.

src/test/ui/consts/const-deref-ptr.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0080]: could not evaluate static initializer
22
--> $DIR/const-deref-ptr.rs:4:29
33
|
44
LL | static C: u64 = unsafe {*(0xdeadbeef as *const u64)};
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ 0xdeadbeef is not a valid pointer
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ dereferencing pointer failed: 0xdeadbeef is not a valid pointer
66

77
error: aborting due to previous error
88

src/test/ui/consts/const-eval/const_raw_ptr_ops2.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ error[E0080]: evaluation of constant value failed
22
--> $DIR/const_raw_ptr_ops2.rs:7:26
33
|
44
LL | const Z2: i32 = unsafe { *(42 as *const i32) };
5-
| ^^^^^^^^^^^^^^^^^^^ 0x2a is not a valid pointer
5+
| ^^^^^^^^^^^^^^^^^^^ dereferencing pointer failed: 0x2a is not a valid pointer
66

77
error[E0080]: evaluation of constant value failed
88
--> $DIR/const_raw_ptr_ops2.rs:9:26
99
|
1010
LL | const Z3: i32 = unsafe { *(44 as *const i32) };
11-
| ^^^^^^^^^^^^^^^^^^^ 0x2c is not a valid pointer
11+
| ^^^^^^^^^^^^^^^^^^^ dereferencing pointer failed: 0x2c is not a valid pointer
1212

1313
error: aborting due to 2 previous errors
1414

src/test/ui/consts/const-eval/ub-wide-ptr.32bit.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ error[E0080]: could not evaluate static initializer
296296
--> $DIR/ub-wide-ptr.rs:135:5
297297
|
298298
LL | mem::transmute::<_, &dyn Trait>((&92u8, 0usize))
299-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ null pointer is not a valid pointer for this operation
299+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ memory access failed: null pointer is not a valid pointer
300300

301301
error[E0080]: could not evaluate static initializer
302302
--> $DIR/ub-wide-ptr.rs:139:5

0 commit comments

Comments
 (0)