Skip to content

Commit 313d474

Browse files
committed
Migrate OpaqueHiddenType, E0282, E0283, E0284, E0698
1 parent 1cff564 commit 313d474

File tree

7 files changed

+440
-130
lines changed

7 files changed

+440
-130
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3983,6 +3983,7 @@ dependencies = [
39833983
"rustc_macros",
39843984
"rustc_middle",
39853985
"rustc_serialize",
3986+
"rustc_session",
39863987
"rustc_span",
39873988
"rustc_target",
39883989
"smallvec",
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
infer_opaque_hidden_type =
2+
opaque type's hidden type cannot be another opaque type from the same scope
3+
.label = one of the two opaque types used here has to be outside its defining scope
4+
.opaque_type = opaque type whose hidden type is being assigned
5+
.hidden_type = opaque type being used as hidden type
6+
7+
infer_type_annotations_needed = {$source_kind ->
8+
[closure] type annotations needed for the closure `{$source_name}`
9+
[normal] type annotations needed for `{$source_name}`
10+
*[other] type annotations needed
11+
}
12+
.label = type must be known at this point
13+
14+
infer_label_bad = {$bad_kind ->
15+
*[other] cannot infer type
16+
[more_info] cannot infer {$prefix_kind ->
17+
*[type] type for {$prefix}
18+
[const_with_param] the value of const parameter
19+
[const] the value of the constant
20+
} `{$name}`{$has_parent ->
21+
[true] {" "}declared on the {$parent_prefix} `{$parent_name}`
22+
*[false] {""}
23+
}
24+
}
25+
26+
infer_source_kind_subdiag_let = {$kind ->
27+
[with_pattern] consider giving `{$name}` an explicit type
28+
[closure] consider giving this closure parameter an explicit type
29+
*[other] consider giving this pattern a type
30+
}{$x_kind ->
31+
[has_name] , where the {$prefix_kind ->
32+
*[type] type for {$prefix}
33+
[const_with_param] the value of const parameter
34+
[const] the value of the constant
35+
} `{$arg_name}` is specified
36+
[underscore] , where the placeholders `_` are specified
37+
*[empty] {""}
38+
}
39+
40+
infer_source_kind_subdiag_generic_label =
41+
cannot infer {$is_type ->
42+
[true] type
43+
*[false] the value
44+
} of the {$is_type ->
45+
[true] type
46+
*[false] const
47+
} {$parent_exists ->
48+
[true] parameter `{$param_name}` declared on the {$parent_prefix} `{$parent_name}`
49+
*[false] parameter {$param_name}
50+
}
51+
52+
infer_source_kind_subdiag_generic_suggestion =
53+
consider specifying the generic {$arg_count ->
54+
[one] argument
55+
*[other] arguments
56+
}
57+
58+
infer_source_kind_fully_qualified =
59+
try using a fully qualified path to specify the expected types
60+
61+
infer_source_kind_closure_return =
62+
try giving this closure an explicit return type
63+
64+
infer_need_type_info_in_generator =
65+
type inside {$generator_kind} must be known in this context

compiler/rustc_error_messages/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ fluent_messages! {
3838
const_eval => "../locales/en-US/const_eval.ftl",
3939
expand => "../locales/en-US/expand.ftl",
4040
interface => "../locales/en-US/interface.ftl",
41+
infer => "../locales/en-US/infer.ftl",
4142
lint => "../locales/en-US/lint.ftl",
4243
parser => "../locales/en-US/parser.ftl",
4344
passes => "../locales/en-US/passes.ftl",

compiler/rustc_infer/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ rustc_hir = { path = "../rustc_hir" }
1515
rustc_index = { path = "../rustc_index" }
1616
rustc_macros = { path = "../rustc_macros" }
1717
rustc_serialize = { path = "../rustc_serialize" }
18+
rustc_session = { path = "../rustc_session" }
1819
rustc_span = { path = "../rustc_span" }
1920
rustc_target = { path = "../rustc_target" }
2021
smallvec = { version = "1.8.1", features = ["union", "may_dangle"] }

compiler/rustc_infer/src/errors.rs

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
use rustc_errors::{fluent, AddSubdiagnostic};
2+
use rustc_hir::FnRetTy;
3+
use rustc_macros::SessionDiagnostic;
4+
use rustc_span::{BytePos, Span};
5+
6+
#[derive(SessionDiagnostic)]
7+
#[diag(infer::opaque_hidden_type)]
8+
pub struct OpaqueHiddenTypeDiag {
9+
#[primary_span]
10+
#[label]
11+
pub span: Span,
12+
#[note(infer::opaque_type)]
13+
pub opaque_type: Span,
14+
#[note(infer::hidden_type)]
15+
pub hidden_type: Span,
16+
}
17+
18+
#[derive(SessionDiagnostic)]
19+
#[diag(infer::type_annotations_needed, code = "E0282")]
20+
pub struct AnnotationRequired<'a> {
21+
#[primary_span]
22+
pub span: Span,
23+
pub source_kind: &'static str,
24+
pub source_name: &'a str,
25+
#[label]
26+
pub failure_span: Option<Span>,
27+
#[subdiagnostic]
28+
pub bad_label: Option<InferenceBadError<'a>>,
29+
#[subdiagnostic]
30+
pub infer_subdiags: Vec<SourceKindSubdiag<'a>>,
31+
#[subdiagnostic]
32+
pub multi_suggestions: Vec<SourceKindMultiSuggestion<'a>>,
33+
}
34+
35+
// Copy of `AnnotationRequired` for E0283
36+
#[derive(SessionDiagnostic)]
37+
#[diag(infer::type_annotations_needed, code = "E0283")]
38+
pub struct AmbigousImpl<'a> {
39+
#[primary_span]
40+
pub span: Span,
41+
pub source_kind: &'static str,
42+
pub source_name: &'a str,
43+
#[label]
44+
pub failure_span: Option<Span>,
45+
#[subdiagnostic]
46+
pub bad_label: Option<InferenceBadError<'a>>,
47+
#[subdiagnostic]
48+
pub infer_subdiags: Vec<SourceKindSubdiag<'a>>,
49+
#[subdiagnostic]
50+
pub multi_suggestions: Vec<SourceKindMultiSuggestion<'a>>,
51+
}
52+
53+
// Copy of `AnnotationRequired` for E0284
54+
#[derive(SessionDiagnostic)]
55+
#[diag(infer::type_annotations_needed, code = "E0284")]
56+
pub struct AmbigousReturn<'a> {
57+
#[primary_span]
58+
pub span: Span,
59+
pub source_kind: &'static str,
60+
pub source_name: &'a str,
61+
#[label]
62+
pub failure_span: Option<Span>,
63+
#[subdiagnostic]
64+
pub bad_label: Option<InferenceBadError<'a>>,
65+
#[subdiagnostic]
66+
pub infer_subdiags: Vec<SourceKindSubdiag<'a>>,
67+
#[subdiagnostic]
68+
pub multi_suggestions: Vec<SourceKindMultiSuggestion<'a>>,
69+
}
70+
71+
#[derive(SessionDiagnostic)]
72+
#[diag(infer::need_type_info_in_generator, code = "E0698")]
73+
pub struct NeedTypeInfoInGenerator<'a> {
74+
#[primary_span]
75+
pub span: Span,
76+
pub generator_kind: String,
77+
#[subdiagnostic]
78+
pub bad_label: InferenceBadError<'a>,
79+
}
80+
81+
// Used when a better one isn't available
82+
#[derive(SessionSubdiagnostic)]
83+
#[label(infer::label_bad)]
84+
pub struct InferenceBadError<'a> {
85+
#[primary_span]
86+
pub span: Span,
87+
pub bad_kind: &'static str,
88+
pub prefix_kind: &'static str,
89+
pub has_parent: bool,
90+
pub prefix: &'a str,
91+
pub parent_prefix: &'a str,
92+
pub parent_name: String,
93+
pub name: String,
94+
}
95+
96+
#[derive(SessionSubdiagnostic)]
97+
pub enum SourceKindSubdiag<'a> {
98+
#[suggestion_verbose(
99+
infer::source_kind_subdiag_let,
100+
code = ": {type_name}",
101+
applicability = "has-placeholders"
102+
)]
103+
LetLike {
104+
#[primary_span]
105+
span: Span,
106+
name: String,
107+
type_name: String,
108+
kind: &'static str,
109+
x_kind: &'static str,
110+
prefix_kind: &'static str,
111+
prefix: &'a str,
112+
arg_name: String,
113+
},
114+
#[label(infer::source_kind_subdiag_generic_label)]
115+
GenericLabel {
116+
#[primary_span]
117+
span: Span,
118+
is_type: bool,
119+
param_name: String,
120+
parent_exists: bool,
121+
parent_prefix: String,
122+
parent_name: String,
123+
},
124+
#[suggestion_verbose(
125+
infer::source_kind_subdiag_generic_suggestion,
126+
code = "::<{args}>",
127+
applicability = "has-placeholders"
128+
)]
129+
GenericSuggestion {
130+
#[primary_span]
131+
span: Span,
132+
arg_count: usize,
133+
args: String,
134+
},
135+
}
136+
137+
// Has to be implemented manually because multipart suggestions are not supported by the derive macro.
138+
// Would be a part of `SourceKindSubdiag` otherwise.
139+
pub enum SourceKindMultiSuggestion<'a> {
140+
FullyQualified {
141+
span: Span,
142+
def_path: String,
143+
adjustment: &'a str,
144+
successor: (&'a str, BytePos),
145+
},
146+
ClosureReturn {
147+
ty_info: String,
148+
data: &'a FnRetTy<'a>,
149+
should_wrap_expr: Option<Span>,
150+
},
151+
}
152+
153+
impl AddSubdiagnostic for SourceKindMultiSuggestion<'_> {
154+
fn add_to_diagnostic(self, diag: &mut rustc_errors::Diagnostic) {
155+
match self {
156+
Self::FullyQualified { span, def_path, adjustment, successor } => {
157+
let suggestion = vec![
158+
(span.shrink_to_lo(), format!("{def_path}({adjustment}")),
159+
(span.shrink_to_hi().with_hi(successor.1), successor.0.to_string()),
160+
];
161+
diag.multipart_suggestion_verbose(
162+
fluent::infer::source_kind_fully_qualified,
163+
suggestion,
164+
rustc_errors::Applicability::HasPlaceholders,
165+
);
166+
}
167+
Self::ClosureReturn { ty_info, data, should_wrap_expr } => {
168+
let (arrow, post) = match data {
169+
FnRetTy::DefaultReturn(_) => ("-> ", " "),
170+
_ => ("", ""),
171+
};
172+
let suggestion = match should_wrap_expr {
173+
Some(end_span) => vec![
174+
(data.span(), format!("{}{}{}{{ ", arrow, ty_info, post)),
175+
(end_span, " }".to_string()),
176+
],
177+
None => vec![(data.span(), format!("{}{}{}", arrow, ty_info, post))],
178+
};
179+
diag.multipart_suggestion_verbose(
180+
fluent::infer::source_kind_closure_return,
181+
suggestion,
182+
rustc_errors::Applicability::HasPlaceholders,
183+
);
184+
}
185+
}
186+
}
187+
}

0 commit comments

Comments
 (0)