Skip to content

Commit d2a8a9f

Browse files
authored
Rollup merge of rust-lang#106940 - oli-obk:tait_error, r=compiler-errors
Improve a TAIT error and add an error code plus documentation cc rust-lang#106858
2 parents 37378ee + 6b69b5e commit d2a8a9f

21 files changed

+145
-82
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;

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

Lines changed: 1 addition & 1 deletion
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
}
Lines changed: 5 additions & 7 deletions
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

Lines changed: 2 additions & 3 deletions
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
}

0 commit comments

Comments
 (0)