Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

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

Lines changed: 9 additions & 11 deletions
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

Lines changed: 12 additions & 1 deletion
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

Lines changed: 1 addition & 0 deletions
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
Lines changed: 60 additions & 0 deletions
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

Lines changed: 4 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 7 additions & 0 deletions
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

Lines changed: 5 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

0 commit comments

Comments
 (0)