Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle diagnostics customization on the fluent side (for one specific diagnostic) #106971

Merged
merged 2 commits into from
Jan 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions compiler/rustc_borrowck/src/region_infer/opaque_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,18 +368,6 @@ fn check_opaque_type_parameter_valid(
for (i, arg) in opaque_type_key.substs.iter().enumerate() {
let arg_is_param = match arg.unpack() {
GenericArgKind::Type(ty) => matches!(ty.kind(), ty::Param(_)),
GenericArgKind::Lifetime(lt) if lt.is_static() => {
tcx.sess
.struct_span_err(span, "non-defining opaque type use in defining scope")
.span_label(
tcx.def_span(opaque_generics.param_at(i, tcx).def_id),
"cannot use static lifetime; use a bound lifetime \
instead or remove the lifetime parameter from the \
opaque type",
)
.emit();
return false;
}
GenericArgKind::Lifetime(lt) => {
matches!(*lt, ty::ReEarlyBound(_) | ty::ReFree(_))
}
Expand Down
5 changes: 4 additions & 1 deletion compiler/rustc_error_messages/locales/en-US/borrowck.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,7 @@ borrowck_cannot_move_when_borrowed =

borrowck_opaque_type_non_generic_param =
expected generic {$kind} parameter, found `{$ty}`
.label = this generic parameter must be used with a generic {$kind} parameter
.label = {STREQ($ty, "'static") ->
[true] cannot use static lifetime; use a bound lifetime instead or remove the lifetime parameter from the opaque type
*[other] this generic parameter must be used with a generic {$kind} parameter
}
2 changes: 0 additions & 2 deletions compiler/rustc_error_messages/locales/en-US/infer.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,6 @@ infer_region_explanation = {$pref_kind ->
}{$desc_kind ->
*[should_not_happen] [{$desc_kind}]
[restatic] the static lifetime
[reempty] the empty lifetime
[reemptyuni] the empty lifetime in universe {$desc_arg}
[revar] lifetime {$desc_arg}

[as_defined] the lifetime `{$desc_arg}` as defined here
Expand Down
15 changes: 15 additions & 0 deletions compiler/rustc_error_messages/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ pub fn fluent_bundle(
trace!(?locale);
let mut bundle = new_bundle(vec![locale]);

// Add convenience functions available to ftl authors.
register_functions(&mut bundle);

// Fluent diagnostics can insert directionality isolation markers around interpolated variables
// indicating that there may be a shift from right-to-left to left-to-right text (or
// vice-versa). These are disabled because they are sometimes visible in the error output, but
Expand Down Expand Up @@ -244,6 +247,15 @@ pub fn fluent_bundle(
Ok(Some(bundle))
}

fn register_functions(bundle: &mut FluentBundle) {
bundle
.add_function("STREQ", |positional, _named| match positional {
[FluentValue::String(a), FluentValue::String(b)] => format!("{}", (a == b)).into(),
_ => FluentValue::Error,
})
.expect("Failed to add a function to the bundle.");
}

/// Type alias for the result of `fallback_fluent_bundle` - a reference-counted pointer to a lazily
/// evaluated fluent bundle.
pub type LazyFallbackBundle = Lrc<Lazy<FluentBundle, impl FnOnce() -> FluentBundle>>;
Expand All @@ -256,6 +268,9 @@ pub fn fallback_fluent_bundle(
) -> LazyFallbackBundle {
Lrc::new(Lazy::new(move || {
let mut fallback_bundle = new_bundle(vec![langid!("en-US")]);

register_functions(&mut fallback_bundle);

// See comment in `fluent_bundle`.
fallback_bundle.set_use_isolating(with_directionality_markers);

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/type-alias-impl-trait/bounds-are-checked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type X<'a> = impl Into<&'static str> + From<&'a str>;
fn f<'a: 'static>(t: &'a str) -> X<'a> {
//~^ WARNING unnecessary lifetime parameter
t
//~^ ERROR non-defining opaque type use
//~^ ERROR expected generic lifetime parameter, found `'static`
}

fn extend_lt<'a>(o: &'a str) -> &'static str {
Expand Down
3 changes: 2 additions & 1 deletion tests/ui/type-alias-impl-trait/bounds-are-checked.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ LL | fn f<'a: 'static>(t: &'a str) -> X<'a> {
|
= help: you can use the `'static` lifetime directly, in place of `'a`

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

error: aborting due to previous error; 1 warning emitted

For more information about this error, try `rustc --explain E0792`.
2 changes: 1 addition & 1 deletion tests/ui/type-alias-impl-trait/generic_nondefining_use.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn concrete_ty() -> OneTy<u32> {

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

fn concrete_const() -> OneConst<{ 123 }> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | type OneTy<T> = impl Debug;
LL | 5u32
| ^^^^

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