Skip to content

Commit 64e5f91

Browse files
committed
Handle diagnostics customization on the fluent side
1 parent 1a6ab6c commit 64e5f91

File tree

7 files changed

+24
-17
lines changed

7 files changed

+24
-17
lines changed

compiler/rustc_borrowck/src/region_infer/opaque_types.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -368,18 +368,6 @@ fn check_opaque_type_parameter_valid(
368368
for (i, arg) in opaque_type_key.substs.iter().enumerate() {
369369
let arg_is_param = match arg.unpack() {
370370
GenericArgKind::Type(ty) => matches!(ty.kind(), ty::Param(_)),
371-
GenericArgKind::Lifetime(lt) if lt.is_static() => {
372-
tcx.sess
373-
.struct_span_err(span, "non-defining opaque type use in defining scope")
374-
.span_label(
375-
tcx.def_span(opaque_generics.param_at(i, tcx).def_id),
376-
"cannot use static lifetime; use a bound lifetime \
377-
instead or remove the lifetime parameter from the \
378-
opaque type",
379-
)
380-
.emit();
381-
return false;
382-
}
383371
GenericArgKind::Lifetime(lt) => {
384372
matches!(*lt, ty::ReEarlyBound(_) | ty::ReFree(_))
385373
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,7 @@ borrowck_cannot_move_when_borrowed =
123123
124124
borrowck_opaque_type_non_generic_param =
125125
expected generic {$kind} parameter, found `{$ty}`
126-
.label = this generic parameter must be used with a generic {$kind} parameter
126+
.label = {STREQ($ty, "'static") ->
127+
[true] cannot use static lifetime; use a bound lifetime instead or remove the lifetime parameter from the opaque type
128+
*[other] this generic parameter must be used with a generic {$kind} parameter
129+
}

compiler/rustc_error_messages/src/lib.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,9 @@ pub fn fluent_bundle(
182182
trace!(?locale);
183183
let mut bundle = new_bundle(vec![locale]);
184184

185+
// Add convenience functions available to ftl authors.
186+
register_functions(&mut bundle);
187+
185188
// Fluent diagnostics can insert directionality isolation markers around interpolated variables
186189
// indicating that there may be a shift from right-to-left to left-to-right text (or
187190
// vice-versa). These are disabled because they are sometimes visible in the error output, but
@@ -244,6 +247,15 @@ pub fn fluent_bundle(
244247
Ok(Some(bundle))
245248
}
246249

250+
fn register_functions(bundle: &mut FluentBundle) {
251+
bundle
252+
.add_function("STREQ", |positional, _named| match positional {
253+
[FluentValue::String(a), FluentValue::String(b)] => format!("{}", (a == b)).into(),
254+
_ => FluentValue::Error,
255+
})
256+
.expect("Failed to add a function to the bundle.");
257+
}
258+
247259
/// Type alias for the result of `fallback_fluent_bundle` - a reference-counted pointer to a lazily
248260
/// evaluated fluent bundle.
249261
pub type LazyFallbackBundle = Lrc<Lazy<FluentBundle, impl FnOnce() -> FluentBundle>>;
@@ -256,6 +268,9 @@ pub fn fallback_fluent_bundle(
256268
) -> LazyFallbackBundle {
257269
Lrc::new(Lazy::new(move || {
258270
let mut fallback_bundle = new_bundle(vec![langid!("en-US")]);
271+
272+
register_functions(&mut fallback_bundle);
273+
259274
// See comment in `fluent_bundle`.
260275
fallback_bundle.set_use_isolating(with_directionality_markers);
261276

tests/ui/type-alias-impl-trait/bounds-are-checked.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ type X<'a> = impl Into<&'static str> + From<&'a str>;
88
fn f<'a: 'static>(t: &'a str) -> X<'a> {
99
//~^ WARNING unnecessary lifetime parameter
1010
t
11-
//~^ ERROR non-defining opaque type use
11+
//~^ ERROR expected generic lifetime parameter, found `'static`
1212
}
1313

1414
fn extend_lt<'a>(o: &'a str) -> &'static str {

tests/ui/type-alias-impl-trait/bounds-are-checked.stderr

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | fn f<'a: 'static>(t: &'a str) -> X<'a> {
66
|
77
= help: you can use the `'static` lifetime directly, in place of `'a`
88

9-
error: non-defining opaque type use in defining scope
9+
error[E0792]: expected generic lifetime parameter, found `'static`
1010
--> $DIR/bounds-are-checked.rs:10:5
1111
|
1212
LL | type X<'a> = impl Into<&'static str> + From<&'a str>;
@@ -17,3 +17,4 @@ LL | t
1717

1818
error: aborting due to previous error; 1 warning emitted
1919

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ fn concrete_ty() -> OneTy<u32> {
1919

2020
fn concrete_lifetime() -> OneLifetime<'static> {
2121
6u32
22-
//~^ ERROR non-defining opaque type use in defining scope
22+
//~^ ERROR expected generic lifetime parameter, found `'static`
2323
}
2424

2525
fn concrete_const() -> OneConst<{ 123 }> {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | type OneTy<T> = impl Debug;
77
LL | 5u32
88
| ^^^^
99

10-
error: non-defining opaque type use in defining scope
10+
error[E0792]: expected generic lifetime parameter, found `'static`
1111
--> $DIR/generic_nondefining_use.rs:21:5
1212
|
1313
LL | type OneLifetime<'a> = impl Debug;

0 commit comments

Comments
 (0)