Skip to content

Commit 20ad820

Browse files
committed
Auto merge of #97667 - matthiaskrgr:rollup-5cfxc85, r=matthiaskrgr
Rollup of 5 pull requests Successful merges: - #97502 (rustdoc: Add more test coverage) - #97627 (update explicit impls error msg) - #97640 (Fix wrong suggestion for adding where clauses) - #97645 (don't use a `span_note` for ignored impls) - #97655 (Improve documentation for constructors of pinned `Box`es) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 42bcd41 + 5b64aab commit 20ad820

File tree

12 files changed

+126
-51
lines changed

12 files changed

+126
-51
lines changed

compiler/rustc_passes/src/dead.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -722,11 +722,7 @@ impl<'tcx> DeadVisitor<'tcx> {
722722
traits_str,
723723
is_are
724724
);
725-
let multispan = ign_traits
726-
.iter()
727-
.map(|(_, impl_id)| self.tcx.def_span(*impl_id))
728-
.collect::<Vec<_>>();
729-
err.span_note(multispan, &msg);
725+
err.note(&msg);
730726
}
731727
err.emit();
732728
});

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

+16-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ use rustc_hir::lang_items::LangItem;
2121
use rustc_hir::{AsyncGeneratorKind, GeneratorKind, Node};
2222
use rustc_middle::hir::map;
2323
use rustc_middle::ty::{
24-
self, suggest_arbitrary_trait_bound, suggest_constraining_type_param, AdtKind, DefIdTree,
24+
self,
25+
subst::{GenericArgKind, SubstsRef},
26+
suggest_arbitrary_trait_bound, suggest_constraining_type_param, AdtKind, DefIdTree,
2527
GeneratorDiagnosticData, GeneratorInteriorTypeCause, Infer, InferTy, ToPredicate, Ty, TyCtxt,
2628
TypeFoldable,
2729
};
@@ -458,6 +460,16 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
458460
_ => (false, None),
459461
};
460462

463+
let generic_args_have_impl_trait = |args: SubstsRef<'tcx>| -> bool {
464+
args.iter().any(|arg| match arg.unpack() {
465+
GenericArgKind::Type(ty) => match ty.kind() {
466+
ty::Param(param) => param.name.as_str().starts_with("impl"),
467+
_ => false,
468+
},
469+
_ => false,
470+
})
471+
};
472+
461473
// FIXME: Add check for trait bound that is already present, particularly `?Sized` so we
462474
// don't suggest `T: Sized + ?Sized`.
463475
let mut hir_id = body_id;
@@ -588,7 +600,9 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
588600
| hir::ItemKind::TraitAlias(generics, _)
589601
| hir::ItemKind::OpaqueTy(hir::OpaqueTy { generics, .. }),
590602
..
591-
}) if !param_ty => {
603+
}) if !param_ty
604+
&& !generic_args_have_impl_trait(trait_pred.skip_binder().trait_ref.substs) =>
605+
{
592606
// Missing generic type parameter bound.
593607
let param_name = self_ty.to_string();
594608
let constraint = trait_pred.print_modifiers_and_trait_path().to_string();

compiler/rustc_typeck/src/coherence/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ fn enforce_trait_manually_implementable(
5757
E0322,
5858
"explicit impls for the `Pointee` trait are not permitted"
5959
)
60-
.span_label(span, "impl of 'Pointee' not allowed")
60+
.span_label(span, "impl of `Pointee` not allowed")
6161
.emit();
6262
return;
6363
}
@@ -70,7 +70,7 @@ fn enforce_trait_manually_implementable(
7070
E0322,
7171
"explicit impls for the `DiscriminantKind` trait are not permitted"
7272
)
73-
.span_label(span, "impl of 'DiscriminantKind' not allowed")
73+
.span_label(span, "impl of `DiscriminantKind` not allowed")
7474
.emit();
7575
return;
7676
}
@@ -83,7 +83,7 @@ fn enforce_trait_manually_implementable(
8383
E0322,
8484
"explicit impls for the `Sized` trait are not permitted"
8585
)
86-
.span_label(span, "impl of 'Sized' not allowed")
86+
.span_label(span, "impl of `Sized` not allowed")
8787
.emit();
8888
return;
8989
}

library/alloc/src/boxed.rs

+28-4
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,13 @@ impl<T> Box<T> {
284284
Self::new_zeroed_in(Global)
285285
}
286286

287-
/// Constructs a new `Pin<Box<T>>`. If `T` does not implement `Unpin`, then
287+
/// Constructs a new `Pin<Box<T>>`. If `T` does not implement [`Unpin`], then
288288
/// `x` will be pinned in memory and unable to be moved.
289+
///
290+
/// Constructing and pinning of the `Box` can also be done in two steps: `Box::pin(x)`
291+
/// does the same as <code>[Box::into_pin]\([Box::new]\(x))</code>. Consider using
292+
/// [`into_pin`](Box::into_pin) if you already have a `Box<T>`, or if you want to
293+
/// construct a (pinned) `Box` in a different way than with [`Box::new`].
289294
#[cfg(not(no_global_oom_handling))]
290295
#[stable(feature = "pin", since = "1.33.0")]
291296
#[must_use]
@@ -573,8 +578,13 @@ impl<T, A: Allocator> Box<T, A> {
573578
unsafe { Ok(Box::from_raw_in(ptr.as_ptr(), alloc)) }
574579
}
575580

576-
/// Constructs a new `Pin<Box<T, A>>`. If `T` does not implement `Unpin`, then
581+
/// Constructs a new `Pin<Box<T, A>>`. If `T` does not implement [`Unpin`], then
577582
/// `x` will be pinned in memory and unable to be moved.
583+
///
584+
/// Constructing and pinning of the `Box` can also be done in two steps: `Box::pin_in(x, alloc)`
585+
/// does the same as <code>[Box::into_pin]\([Box::new_in]\(x, alloc))</code>. Consider using
586+
/// [`into_pin`](Box::into_pin) if you already have a `Box<T, A>`, or if you want to
587+
/// construct a (pinned) `Box` in a different way than with [`Box::new_in`].
578588
#[cfg(not(no_global_oom_handling))]
579589
#[unstable(feature = "allocator_api", issue = "32838")]
580590
#[rustc_const_unstable(feature = "const_box", issue = "92521")]
@@ -1190,12 +1200,18 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
11901200
unsafe { &mut *mem::ManuallyDrop::new(b).0.as_ptr() }
11911201
}
11921202

1193-
/// Converts a `Box<T>` into a `Pin<Box<T>>`
1203+
/// Converts a `Box<T>` into a `Pin<Box<T>>`. If `T` does not implement [`Unpin`], then
1204+
/// `*boxed` will be pinned in memory and unable to be moved.
11941205
///
11951206
/// This conversion does not allocate on the heap and happens in place.
11961207
///
11971208
/// This is also available via [`From`].
11981209
///
1210+
/// Constructing and pinning a `Box` with <code>Box::into_pin([Box::new]\(x))</code>
1211+
/// can also be written more concisely using <code>[Box::pin]\(x)</code>.
1212+
/// This `into_pin` method is useful if you already have a `Box<T>`, or you are
1213+
/// constructing a (pinned) `Box` in a different way than with [`Box::new`].
1214+
///
11991215
/// # Notes
12001216
///
12011217
/// It's not recommended that crates add an impl like `From<Box<T>> for Pin<T>`,
@@ -1458,9 +1474,17 @@ impl<T: ?Sized, A: Allocator> const From<Box<T, A>> for Pin<Box<T, A>>
14581474
where
14591475
A: 'static,
14601476
{
1461-
/// Converts a `Box<T>` into a `Pin<Box<T>>`
1477+
/// Converts a `Box<T>` into a `Pin<Box<T>>`. If `T` does not implement [`Unpin`], then
1478+
/// `*boxed` will be pinned in memory and unable to be moved.
14621479
///
14631480
/// This conversion does not allocate on the heap and happens in place.
1481+
///
1482+
/// This is also available via [`Box::into_pin`].
1483+
///
1484+
/// Constructing and pinning a `Box` with <code><Pin<Box\<T>>>::from([Box::new]\(x))</code>
1485+
/// can also be written more concisely using <code>[Box::pin]\(x)</code>.
1486+
/// This `From` implementation is useful if you already have a `Box<T>`, or you are
1487+
/// constructing a (pinned) `Box` in a different way than with [`Box::new`].
14641488
fn from(boxed: Box<T, A>) -> Self {
14651489
Box::into_pin(boxed)
14661490
}

src/test/rustdoc/nested-modules.rs

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#![crate_name = "aCrate"]
2+
3+
mod a_module {
4+
pub fn private_function() {}
5+
6+
pub use a_module::private_function as other_private_function;
7+
8+
pub mod a_nested_module {
9+
// @has aCrate/a_nested_module/index.html '//a[@href="fn.a_nested_public_function.html"]' 'a_nested_public_function'
10+
// @has aCrate/a_nested_module/fn.a_nested_public_function.html 'pub fn a_nested_public_function()'
11+
pub fn a_nested_public_function() {}
12+
13+
// @has aCrate/a_nested_module/index.html '//a[@href="fn.another_nested_public_function.html"]' 'another_nested_public_function'
14+
// @has aCrate/a_nested_module/fn.another_nested_public_function.html 'pub fn another_nested_public_function()'
15+
pub use a_nested_module::a_nested_public_function as another_nested_public_function;
16+
}
17+
18+
// @!has aCrate/a_nested_module/index.html 'yet_another_nested_public_function'
19+
pub use a_nested_module::a_nested_public_function as yet_another_nested_public_function;
20+
21+
// @!has aCrate/a_nested_module/index.html 'one_last_nested_public_function'
22+
pub use a_nested_module::another_nested_public_function as one_last_nested_public_function;
23+
}
24+
25+
// @!has aCrate/index.html 'a_module'
26+
// @has aCrate/index.html '//a[@href="a_nested_module/index.html"]' 'a_nested_module'
27+
pub use a_module::a_nested_module;
28+
29+
// @has aCrate/index.html '//a[@href="fn.a_nested_public_function.html"]' 'a_nested_public_function'
30+
// @has aCrate/index.html '//a[@href="fn.another_nested_public_function.html"]' 'another_nested_public_function'
31+
// @has aCrate/index.html '//a[@href="fn.yet_another_nested_public_function.html"]' 'yet_another_nested_public_function'
32+
// @has aCrate/index.html '//a[@href="fn.one_last_nested_public_function.html"]' 'one_last_nested_public_function'
33+
pub use a_module::{
34+
a_nested_module::{a_nested_public_function, another_nested_public_function},
35+
one_last_nested_public_function, yet_another_nested_public_function,
36+
};
37+
38+
// @has aCrate/index.html '//a[@href="fn.private_function.html"]' 'private_function'
39+
// @!has aCrate/fn.private_function.html 'a_module'
40+
// @has aCrate/index.html '//a[@href="fn.other_private_function.html"]' 'other_private_function'
41+
// @!has aCrate/fn.other_private_function.html 'a_module'
42+
pub use a_module::{other_private_function, private_function};

src/test/ui/coherence/coherence-impls-sized.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -35,37 +35,37 @@ error[E0322]: explicit impls for the `Sized` trait are not permitted
3535
--> $DIR/coherence-impls-sized.rs:14:1
3636
|
3737
LL | impl Sized for TestE {}
38-
| ^^^^^^^^^^^^^^^^^^^^ impl of 'Sized' not allowed
38+
| ^^^^^^^^^^^^^^^^^^^^ impl of `Sized` not allowed
3939

4040
error[E0322]: explicit impls for the `Sized` trait are not permitted
4141
--> $DIR/coherence-impls-sized.rs:17:1
4242
|
4343
LL | impl Sized for MyType {}
44-
| ^^^^^^^^^^^^^^^^^^^^^ impl of 'Sized' not allowed
44+
| ^^^^^^^^^^^^^^^^^^^^^ impl of `Sized` not allowed
4545

4646
error[E0322]: explicit impls for the `Sized` trait are not permitted
4747
--> $DIR/coherence-impls-sized.rs:20:1
4848
|
4949
LL | impl Sized for (MyType, MyType) {}
50-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl of 'Sized' not allowed
50+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl of `Sized` not allowed
5151

5252
error[E0322]: explicit impls for the `Sized` trait are not permitted
5353
--> $DIR/coherence-impls-sized.rs:24:1
5454
|
5555
LL | impl Sized for &'static NotSync {}
56-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl of 'Sized' not allowed
56+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl of `Sized` not allowed
5757

5858
error[E0322]: explicit impls for the `Sized` trait are not permitted
5959
--> $DIR/coherence-impls-sized.rs:27:1
6060
|
6161
LL | impl Sized for [MyType] {}
62-
| ^^^^^^^^^^^^^^^^^^^^^^^ impl of 'Sized' not allowed
62+
| ^^^^^^^^^^^^^^^^^^^^^^^ impl of `Sized` not allowed
6363

6464
error[E0322]: explicit impls for the `Sized` trait are not permitted
6565
--> $DIR/coherence-impls-sized.rs:31:1
6666
|
6767
LL | impl Sized for &'static [NotSync] {}
68-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl of 'Sized' not allowed
68+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl of `Sized` not allowed
6969

7070
error: aborting due to 9 previous errors
7171

src/test/ui/derive-uninhabited-enum-38885.stderr

+1-6
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,7 @@ LL | Void(Void),
55
| ^^^^^^^^^^
66
|
77
= note: `-W dead-code` implied by `-W unused`
8-
note: `Foo` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis
9-
--> $DIR/derive-uninhabited-enum-38885.rs:10:10
10-
|
11-
LL | #[derive(Debug)]
12-
| ^^^^^
13-
= note: this warning originates in the derive macro `Debug` (in Nightly builds, run with -Z macro-backtrace for more info)
8+
= note: `Foo` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis
149

1510
warning: 1 warning emitted
1611

src/test/ui/derives/clone-debug-dead-code.stderr

+3-18
Original file line numberDiff line numberDiff line change
@@ -16,38 +16,23 @@ error: field is never read: `f`
1616
LL | struct B { f: () }
1717
| ^^^^^
1818
|
19-
note: `B` has a derived impl for the trait `Clone`, but this is intentionally ignored during dead code analysis
20-
--> $DIR/clone-debug-dead-code.rs:9:10
21-
|
22-
LL | #[derive(Clone)]
23-
| ^^^^^
24-
= note: this error originates in the derive macro `Clone` (in Nightly builds, run with -Z macro-backtrace for more info)
19+
= note: `B` has a derived impl for the trait `Clone`, but this is intentionally ignored during dead code analysis
2520

2621
error: field is never read: `f`
2722
--> $DIR/clone-debug-dead-code.rs:14:12
2823
|
2924
LL | struct C { f: () }
3025
| ^^^^^
3126
|
32-
note: `C` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis
33-
--> $DIR/clone-debug-dead-code.rs:13:10
34-
|
35-
LL | #[derive(Debug)]
36-
| ^^^^^
37-
= note: this error originates in the derive macro `Debug` (in Nightly builds, run with -Z macro-backtrace for more info)
27+
= note: `C` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis
3828

3929
error: field is never read: `f`
4030
--> $DIR/clone-debug-dead-code.rs:18:12
4131
|
4232
LL | struct D { f: () }
4333
| ^^^^^
4434
|
45-
note: `D` has derived impls for the traits `Clone` and `Debug`, but these are intentionally ignored during dead code analysis
46-
--> $DIR/clone-debug-dead-code.rs:17:10
47-
|
48-
LL | #[derive(Debug,Clone)]
49-
| ^^^^^ ^^^^^
50-
= note: this error originates in the derive macro `Debug` (in Nightly builds, run with -Z macro-backtrace for more info)
35+
= note: `D` has derived impls for the traits `Clone` and `Debug`, but these are intentionally ignored during dead code analysis
5136

5237
error: field is never read: `f`
5338
--> $DIR/clone-debug-dead-code.rs:21:12

src/test/ui/enum-discriminant/forbidden-discriminant-kind-impl.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0322]: explicit impls for the `DiscriminantKind` trait are not permitted
22
--> $DIR/forbidden-discriminant-kind-impl.rs:9:1
33
|
44
LL | impl DiscriminantKind for NewType {
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl of 'DiscriminantKind' not allowed
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl of `DiscriminantKind` not allowed
66

77
error: aborting due to previous error
88

src/test/ui/lint/dead-code/unused-variant.stderr

+1-6
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,7 @@ note: the lint level is defined here
99
|
1010
LL | #![deny(dead_code)]
1111
| ^^^^^^^^^
12-
note: `Enum` has a derived impl for the trait `Clone`, but this is intentionally ignored during dead code analysis
13-
--> $DIR/unused-variant.rs:3:10
14-
|
15-
LL | #[derive(Clone)]
16-
| ^^^^^
17-
= note: this error originates in the derive macro `Clone` (in Nightly builds, run with -Z macro-backtrace for more info)
12+
= note: `Enum` has a derived impl for the trait `Clone`, but this is intentionally ignored during dead code analysis
1813

1914
error: aborting due to previous error
2015

src/test/ui/traits/issue-97576.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
struct Foo {
2+
bar: String,
3+
}
4+
5+
impl Foo {
6+
pub fn new(bar: impl ToString) -> Self {
7+
Self {
8+
bar: bar.into(), //~ ERROR the trait bound `String: From<impl ToString>` is not satisfied
9+
}
10+
}
11+
}
12+
13+
fn main() {}

src/test/ui/traits/issue-97576.stderr

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0277]: the trait bound `String: From<impl ToString>` is not satisfied
2+
--> $DIR/issue-97576.rs:8:22
3+
|
4+
LL | bar: bar.into(),
5+
| ^^^^ the trait `From<impl ToString>` is not implemented for `String`
6+
|
7+
= note: required because of the requirements on the impl of `Into<String>` for `impl ToString`
8+
9+
error: aborting due to previous error
10+
11+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)