Skip to content

Commit 4781233

Browse files
committed
Auto merge of rust-lang#106945 - matthiaskrgr:rollup-c5or8z3, r=matthiaskrgr
Rollup of 5 pull requests Successful merges: - rust-lang#105954 (Update instrument-coverage.md) - rust-lang#106835 (new trait solver: rebase impl substs for gats correctly) - rust-lang#106912 (check -Z query-dep-graph is enabled if -Z dump-dep-graph (rust-lang#106736)) - rust-lang#106940 (Improve a TAIT error and add an error code plus documentation) - rust-lang#106942 (Update books) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 4817259 + 4fb9da1 commit 4781233

31 files changed

+166
-88
lines changed

compiler/rustc_borrowck/src/region_infer/opaque_types.rs

+9-11
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ use rustc_span::Span;
1212
use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt as _;
1313
use rustc_trait_selection::traits::ObligationCtxt;
1414

15+
use crate::session_diagnostics::NonGenericOpaqueTypeParam;
16+
1517
use super::RegionInferenceContext;
1618

1719
impl<'tcx> RegionInferenceContext<'tcx> {
@@ -389,17 +391,13 @@ fn check_opaque_type_parameter_valid(
389391
} else {
390392
// Prevent `fn foo() -> Foo<u32>` from being defining.
391393
let opaque_param = opaque_generics.param_at(i, tcx);
392-
tcx.sess
393-
.struct_span_err(span, "non-defining opaque type use in defining scope")
394-
.span_note(
395-
tcx.def_span(opaque_param.def_id),
396-
&format!(
397-
"used non-generic {} `{}` for generic parameter",
398-
opaque_param.kind.descr(),
399-
arg,
400-
),
401-
)
402-
.emit();
394+
let kind = opaque_param.kind.descr();
395+
tcx.sess.emit_err(NonGenericOpaqueTypeParam {
396+
ty: arg,
397+
kind,
398+
span,
399+
param_span: tcx.def_span(opaque_param.def_id),
400+
});
403401
return false;
404402
}
405403
}

compiler/rustc_borrowck/src/session_diagnostics.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use rustc_errors::{IntoDiagnosticArg, MultiSpan};
22
use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic};
3-
use rustc_middle::ty::Ty;
3+
use rustc_middle::ty::{GenericArg, Ty};
44
use rustc_span::Span;
55

66
use crate::diagnostics::RegionName;
@@ -240,3 +240,14 @@ pub(crate) struct MoveBorrow<'a> {
240240
#[label]
241241
pub borrow_span: Span,
242242
}
243+
244+
#[derive(Diagnostic)]
245+
#[diag(borrowck_opaque_type_non_generic_param, code = "E0792")]
246+
pub(crate) struct NonGenericOpaqueTypeParam<'a, 'tcx> {
247+
pub ty: GenericArg<'tcx>,
248+
pub kind: &'a str,
249+
#[primary_span]
250+
pub span: Span,
251+
#[label]
252+
pub param_span: Span,
253+
}

compiler/rustc_error_codes/src/error_codes.rs

+1
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,7 @@ E0787: include_str!("./error_codes/E0787.md"),
508508
E0788: include_str!("./error_codes/E0788.md"),
509509
E0790: include_str!("./error_codes/E0790.md"),
510510
E0791: include_str!("./error_codes/E0791.md"),
511+
E0792: include_str!("./error_codes/E0792.md"),
511512
;
512513
// E0006, // merged with E0005
513514
// E0008, // cannot bind by-move into a pattern guard
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
A type alias impl trait can only have its hidden type assigned
2+
when used fully generically (and within their defining scope).
3+
This means
4+
5+
```compile_fail,E0792
6+
#![feature(type_alias_impl_trait)]
7+
8+
type Foo<T> = impl std::fmt::Debug;
9+
10+
fn foo() -> Foo<u32> {
11+
5u32
12+
}
13+
```
14+
15+
is not accepted. If it were accepted, one could create unsound situations like
16+
17+
```compile_fail,E0792
18+
#![feature(type_alias_impl_trait)]
19+
20+
type Foo<T> = impl Default;
21+
22+
fn foo() -> Foo<u32> {
23+
5u32
24+
}
25+
26+
fn main() {
27+
let x = Foo::<&'static mut String>::default();
28+
}
29+
```
30+
31+
32+
Instead you need to make the function generic:
33+
34+
```
35+
#![feature(type_alias_impl_trait)]
36+
37+
type Foo<T> = impl std::fmt::Debug;
38+
39+
fn foo<U>() -> Foo<U> {
40+
5u32
41+
}
42+
```
43+
44+
This means that no matter the generic parameter to `foo`,
45+
the hidden type will always be `u32`.
46+
If you want to link the generic parameter to the hidden type,
47+
you can do that, too:
48+
49+
50+
```
51+
#![feature(type_alias_impl_trait)]
52+
53+
use std::fmt::Debug;
54+
55+
type Foo<T: Debug> = impl Debug;
56+
57+
fn foo<U: Debug>() -> Foo<U> {
58+
Vec::<U>::new()
59+
}
60+
```

compiler/rustc_error_messages/locales/en-US/borrowck.ftl

+4
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,7 @@ borrowck_cannot_move_when_borrowed =
120120
[value] value
121121
*[other] {$value_place}
122122
} occurs here
123+
124+
borrowck_opaque_type_non_generic_param =
125+
expected generic {$kind} parameter, found `{$ty}`
126+
.label = this generic parameter must be used with a generic {$kind} parameter

compiler/rustc_middle/src/ty/diagnostics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc_type_ir::sty::TyKind::*;
1717

1818
impl<'tcx> IntoDiagnosticArg for Ty<'tcx> {
1919
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
20-
format!("{}", self).into_diagnostic_arg()
20+
self.to_string().into_diagnostic_arg()
2121
}
2222
}
2323

compiler/rustc_middle/src/ty/subst.rs

+7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::ty::visit::{TypeVisitable, TypeVisitor};
77
use crate::ty::{self, Lift, List, ParamConst, Ty, TyCtxt};
88

99
use rustc_data_structures::intern::Interned;
10+
use rustc_errors::{DiagnosticArgValue, IntoDiagnosticArg};
1011
use rustc_hir::def_id::DefId;
1112
use rustc_macros::HashStable;
1213
use rustc_serialize::{self, Decodable, Encodable};
@@ -36,6 +37,12 @@ pub struct GenericArg<'tcx> {
3637
marker: PhantomData<(Ty<'tcx>, ty::Region<'tcx>, ty::Const<'tcx>)>,
3738
}
3839

40+
impl<'tcx> IntoDiagnosticArg for GenericArg<'tcx> {
41+
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
42+
self.to_string().into_diagnostic_arg()
43+
}
44+
}
45+
3946
const TAG_MASK: usize = 0b11;
4047
const TYPE_TAG: usize = 0b00;
4148
const REGION_TAG: usize = 0b01;

compiler/rustc_session/src/config.rs

+5
Original file line numberDiff line numberDiff line change
@@ -2459,6 +2459,11 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
24592459

24602460
let pretty = parse_pretty(&unstable_opts, error_format);
24612461

2462+
// query-dep-graph is required if dump-dep-graph is given #106736
2463+
if unstable_opts.dump_dep_graph && !unstable_opts.query_dep_graph {
2464+
early_error(error_format, "can't dump dependency graph without `-Z query-dep-graph`");
2465+
}
2466+
24622467
// Try to find a directory containing the Rust `src`, for more details see
24632468
// the doc comment on the `real_rust_source_base_dir` field.
24642469
let tmp_buf;

compiler/rustc_trait_selection/src/solve/project_goals.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ impl<'tcx> assembly::GoalKind<'tcx> for ProjectionPredicate<'tcx> {
171171
let impl_substs_with_gat = goal.predicate.projection_ty.substs.rebase_onto(
172172
tcx,
173173
goal_trait_ref.def_id,
174-
impl_trait_ref.substs,
174+
impl_substs,
175175
);
176176
let substs = translate_substs(
177177
acx.infcx,

src/doc/book

src/doc/rustc/src/instrument-coverage.md

+2
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,8 @@ $ RUSTFLAGS="-C instrument-coverage" \
201201
cargo test --tests
202202
```
203203

204+
> **Note**: The default for `LLVM_PROFILE_FILE` is `default_%m_%p.profraw`. Versions prior to 1.65 had a default of `default.profraw`, so if using those earlier versions, it is recommended to explicitly set `LLVM_PROFILE_FILE="default_%m_%p.profraw"` to avoid having multiple tests overwrite the `.profraw` files.
205+
204206
Make note of the test binary file paths, displayed after the word "`Running`" in the test output:
205207

206208
```text

tests/ui/dep-graph/dep-graph-dump.rs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Test dump-dep-graph requires query-dep-graph enabled
2+
3+
// incremental
4+
// compile-flags: -Z dump-dep-graph
5+
6+
fn main() {}
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
error: can't dump dependency graph without `-Z query-dep-graph`
2+

tests/ui/type-alias-impl-trait/bound_reduction2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ impl<W> Trait<W> for () {}
1414

1515
fn foo_desugared<T: TraitWithAssoc>(_: T) -> Foo<T::Assoc> {
1616
()
17-
//~^ ERROR non-defining opaque type use
17+
//~^ ERROR expected generic type parameter, found `<T as TraitWithAssoc>::Assoc`
1818
}
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
error: non-defining opaque type use in defining scope
1+
error[E0792]: expected generic type parameter, found `<T as TraitWithAssoc>::Assoc`
22
--> $DIR/bound_reduction2.rs:16:5
33
|
4+
LL | type Foo<V> = impl Trait<V>;
5+
| - this generic parameter must be used with a generic type parameter
6+
...
47
LL | ()
58
| ^^
6-
|
7-
note: used non-generic type `<T as TraitWithAssoc>::Assoc` for generic parameter
8-
--> $DIR/bound_reduction2.rs:9:10
9-
|
10-
LL | type Foo<V> = impl Trait<V>;
11-
| ^
129

1310
error: aborting due to previous error
1411

12+
For more information about this error, try `rustc --explain E0792`.

tests/ui/type-alias-impl-trait/generic_nondefining_use.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ type OneLifetime<'a> = impl Debug;
1010

1111
type OneConst<const X: usize> = impl Debug;
1212

13-
1413
// Not defining uses, because they doesn't define *all* possible generics.
1514

1615
fn concrete_ty() -> OneTy<u32> {
1716
5u32
18-
//~^ ERROR non-defining opaque type use in defining scope
17+
//~^ ERROR expected generic type parameter, found `u32`
1918
}
2019

2120
fn concrete_lifetime() -> OneLifetime<'static> {
@@ -25,5 +24,5 @@ fn concrete_lifetime() -> OneLifetime<'static> {
2524

2625
fn concrete_const() -> OneConst<{ 123 }> {
2726
7u32
28-
//~^ ERROR non-defining opaque type use in defining scope
27+
//~^ ERROR expected generic constant parameter, found `123`
2928
}
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,30 @@
1-
error: non-defining opaque type use in defining scope
2-
--> $DIR/generic_nondefining_use.rs:17:5
1+
error[E0792]: expected generic type parameter, found `u32`
2+
--> $DIR/generic_nondefining_use.rs:16:5
33
|
4+
LL | type OneTy<T> = impl Debug;
5+
| - this generic parameter must be used with a generic type parameter
6+
...
47
LL | 5u32
58
| ^^^^
6-
|
7-
note: used non-generic type `u32` for generic parameter
8-
--> $DIR/generic_nondefining_use.rs:7:12
9-
|
10-
LL | type OneTy<T> = impl Debug;
11-
| ^
129

1310
error: non-defining opaque type use in defining scope
14-
--> $DIR/generic_nondefining_use.rs:22:5
11+
--> $DIR/generic_nondefining_use.rs:21:5
1512
|
1613
LL | type OneLifetime<'a> = impl Debug;
1714
| -- cannot use static lifetime; use a bound lifetime instead or remove the lifetime parameter from the opaque type
1815
...
1916
LL | 6u32
2017
| ^^^^
2118

22-
error: non-defining opaque type use in defining scope
23-
--> $DIR/generic_nondefining_use.rs:27:5
19+
error[E0792]: expected generic constant parameter, found `123`
20+
--> $DIR/generic_nondefining_use.rs:26:5
2421
|
22+
LL | type OneConst<const X: usize> = impl Debug;
23+
| -------------- this generic parameter must be used with a generic constant parameter
24+
...
2525
LL | 7u32
2626
| ^^^^
27-
|
28-
note: used non-generic constant `123` for generic parameter
29-
--> $DIR/generic_nondefining_use.rs:11:15
30-
|
31-
LL | type OneConst<const X: usize> = impl Debug;
32-
| ^^^^^^^^^^^^^^
3327

3428
error: aborting due to 3 previous errors
3529

30+
For more information about this error, try `rustc --explain E0792`.

tests/ui/type-alias-impl-trait/generic_type_does_not_live_long_enough.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ fn main() {
44
let y = 42;
55
let x = wrong_generic(&y);
66
let z: i32 = x;
7-
//~^ ERROR non-defining opaque type use
7+
//~^ ERROR expected generic type parameter, found `&'static i32
88
}
99

1010
type WrongGeneric<T> = impl 'static;

tests/ui/type-alias-impl-trait/generic_type_does_not_live_long_enough.stderr

+5-7
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,14 @@ error: at least one trait must be specified
44
LL | type WrongGeneric<T> = impl 'static;
55
| ^^^^^^^^^^^^
66

7-
error: non-defining opaque type use in defining scope
7+
error[E0792]: expected generic type parameter, found `&'static i32`
88
--> $DIR/generic_type_does_not_live_long_enough.rs:6:18
99
|
1010
LL | let z: i32 = x;
1111
| ^
12-
|
13-
note: used non-generic type `&'static i32` for generic parameter
14-
--> $DIR/generic_type_does_not_live_long_enough.rs:10:19
15-
|
12+
...
1613
LL | type WrongGeneric<T> = impl 'static;
17-
| ^
14+
| - this generic parameter must be used with a generic type parameter
1815

1916
error[E0310]: the parameter type `T` may not live long enough
2017
--> $DIR/generic_type_does_not_live_long_enough.rs:14:5
@@ -29,4 +26,5 @@ LL | fn wrong_generic<T: 'static>(t: T) -> WrongGeneric<T> {
2926

3027
error: aborting due to 3 previous errors
3128

32-
For more information about this error, try `rustc --explain E0310`.
29+
Some errors have detailed explanations: E0310, E0792.
30+
For more information about an error, try `rustc --explain E0310`.

tests/ui/type-alias-impl-trait/issue-60564.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ where
1818
type BitsIter = IterBitsIter<T, E, u8>;
1919
fn iter_bits(self, n: u8) -> Self::BitsIter {
2020
(0u8..n).rev().map(move |shift| ((self >> T::from(shift)) & T::from(1)).try_into().unwrap())
21-
//~^ ERROR non-defining opaque type use in defining scope
21+
//~^ ERROR expected generic type parameter, found `u8`
2222
}
2323
}
2424

Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
error: non-defining opaque type use in defining scope
1+
error[E0792]: expected generic type parameter, found `u8`
22
--> $DIR/issue-60564.rs:20:9
33
|
4+
LL | type IterBitsIter<T, E, I> = impl std::iter::Iterator<Item = I>;
5+
| - this generic parameter must be used with a generic type parameter
6+
...
47
LL | (0u8..n).rev().map(move |shift| ((self >> T::from(shift)) & T::from(1)).try_into().unwrap())
58
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6-
|
7-
note: used non-generic type `u8` for generic parameter
8-
--> $DIR/issue-60564.rs:8:25
9-
|
10-
LL | type IterBitsIter<T, E, I> = impl std::iter::Iterator<Item = I>;
11-
| ^
129

1310
error: aborting due to previous error
1411

12+
For more information about this error, try `rustc --explain E0792`.

tests/ui/type-alias-impl-trait/issue-68368-non-defining-use-2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ trait Trait<T> {}
77
type Alias<'a, U> = impl Trait<U>;
88

99
fn f<'a>() -> Alias<'a, ()> {}
10-
//~^ ERROR non-defining opaque type use in defining scope
10+
//~^ ERROR expected generic type parameter, found `()`
1111

1212
fn main() {}
1313

0 commit comments

Comments
 (0)