Skip to content

[--bless] a Diagnotic Derive I wish to work for English words in descr #103469

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

Closed
wants to merge 1 commit into from
Closed
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
19 changes: 8 additions & 11 deletions compiler/rustc_borrowck/src/borrowck_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use rustc_errors::{
use rustc_middle::ty::{self, Ty, TyCtxt};
use rustc_span::Span;

use crate::session_diagnostics::ImmutSectionMutErr;

impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
pub(crate) fn cannot_move_when_borrowed(
&self,
Expand Down Expand Up @@ -348,21 +350,16 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
mutate_span: Span,
immutable_span: Span,
immutable_place: &str,
immutable_section: &str,
fake_read_cause: u8,
action: &str,
) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
let mut err = struct_span_err!(
self,
self.infcx.tcx.sess.create_err(ImmutSectionMutErr {
immutable_span,
mutate_span,
E0510,
"cannot {} {} in {}",
action,
immutable_place,
immutable_section,
);
err.span_label(mutate_span, format!("cannot {}", action));
err.span_label(immutable_span, format!("value is immutable in {}", immutable_section));
err
place_desc: immutable_place,
fake_read_cause,
})
}

pub(crate) fn cannot_borrow_across_generator_yield(
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2249,7 +2249,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
}

/// Describe the reason for the fake borrow that was assigned to `place`.
fn classify_immutable_section(&self, place: Place<'tcx>) -> Option<&'static str> {
fn classify_immutable_section(&self, place: Place<'tcx>) -> Option<u8> {
use rustc_middle::mir::visit::Visitor;
struct FakeReadCauseFinder<'tcx> {
place: Place<'tcx>,
Expand All @@ -2270,8 +2270,8 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
let mut visitor = FakeReadCauseFinder { place, cause: None };
visitor.visit_body(&self.body);
match visitor.cause {
Some(FakeReadCause::ForMatchGuard) => Some("match guard"),
Some(FakeReadCause::ForIndex) => Some("indexing expression"),
Some(FakeReadCause::ForMatchGuard) => Some(0),
Some(FakeReadCause::ForIndex) => Some(4),
_ => None,
}
}
Expand Down
13 changes: 13 additions & 0 deletions compiler/rustc_borrowck/src/session_diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,16 @@ pub(crate) enum RequireStaticErr {
multi_span: MultiSpan,
},
}

#[derive(Diagnostic)]
#[diag(borrowck_cannot_mutate_in_immutable_section, code = "E0510")]
pub(crate) struct ImmutSectionMutErr<'a> {
#[primary_span]
#[label]
pub mutate_span: Span,
#[label(immu_label)]
pub immutable_span: Span,
pub action: &'a str,
pub place_desc: &'a str,
pub fake_read_cause: u8,
}
23 changes: 23 additions & 0 deletions compiler/rustc_error_messages/locales/en-US/borrowck.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,26 @@ borrowck_returned_lifetime_short =

borrowck_used_impl_require_static =
the used `impl` has a `'static` requirement

borrowck_cannot_mutate_in_immutable_section =
cannot {-action} {-place_descr} in {-section_descr}
.label = cannot {-action}
.immu_label = value is immutable in {-section_descr}

-action = {$action ->
[assign] assign
*[other] mutably borrow
}

-place_descr =
{$place_desc ->
[value] value
*[other] {$place_desc}
}

-section_descr =
{$fake_read_cause ->
[0] match guard
[4] indexing expression
*[other] {""}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or maybe

borrowck_cannot_mutate_in_immutable_section =
    cannot {.action} {.place_descr} in {.section_descr}
    .label = cannot {.action}
    .immu_label = value is immutable in {.section_descr}
    .action = {$action ->
        [assign] assign
        *[other] mutably borrow
    }
    .place_descr =
    {$place_desc ->
        [value] value
        *[other] {$place_desc}
    }
    .section_descr =
    {$fake_read_cause ->
        [0] match guard
        [4] indexing expression
        *[other] {""}
    }

10 changes: 5 additions & 5 deletions src/test/ui/borrowck/borrowck-mutate-in-guard.stderr
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
error[E0510]: cannot assign `x` in match guard
error[E0510]: cannot mutably borrow {$place_desc} in
--> $DIR/borrowck-mutate-in-guard.rs:10:25
|
LL | match x {
| - value is immutable in match guard
| - value is immutable in
LL | Enum::A(_) if { x = Enum::B(false); false } => 1,
| ^^^^^^^^^^^^^^^^^^ cannot assign
| ^^^^^^^^^^^^^^^^^^ cannot mutably borrow

error[E0510]: cannot mutably borrow `x` in match guard
error[E0510]: cannot mutably borrow {$place_desc} in
--> $DIR/borrowck-mutate-in-guard.rs:12:33
|
LL | match x {
| - value is immutable in match guard
| - value is immutable in
...
LL | Enum::A(_) if { let y = &mut x; *y = Enum::B(false); false } => 1,
| ^^^^^^ cannot mutably borrow
Expand Down
24 changes: 12 additions & 12 deletions src/test/ui/borrowck/slice-index-bounds-check-invalidation.stderr
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
error[E0510]: cannot assign `x` in indexing expression
error[E0510]: cannot mutably borrow {$place_desc} in
--> $DIR/slice-index-bounds-check-invalidation.rs:36:12
|
LL | x[1][{ x = y; 2}]
| ---- ^^^^^ cannot assign
| ---- ^^^^^ cannot mutably borrow
| |
| value is immutable in indexing expression
| value is immutable in

error[E0510]: cannot assign `x` in indexing expression
error[E0510]: cannot mutably borrow {$place_desc} in
--> $DIR/slice-index-bounds-check-invalidation.rs:50:12
|
LL | x[1][{ x = y; 2}]
| ---- ^^^^^ cannot assign
| ---- ^^^^^ cannot mutably borrow
| |
| value is immutable in indexing expression
| value is immutable in

error[E0510]: cannot assign `x` in indexing expression
error[E0510]: cannot mutably borrow {$place_desc} in
--> $DIR/slice-index-bounds-check-invalidation.rs:64:12
|
LL | x[1][{ x = y; 2}][0]
| ---- ^^^^^ cannot assign
| ---- ^^^^^ cannot mutably borrow
| |
| value is immutable in indexing expression
| value is immutable in

error[E0510]: cannot assign `x` in indexing expression
error[E0510]: cannot mutably borrow {$place_desc} in
--> $DIR/slice-index-bounds-check-invalidation.rs:71:12
|
LL | x[1][{ x = y; 2}][0]
| ---- ^^^^^ cannot assign
| ---- ^^^^^ cannot mutably borrow
| |
| value is immutable in indexing expression
| value is immutable in

error: aborting due to 4 previous errors

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0510]: cannot mutably borrow `x` in match guard
error[E0510]: cannot mutably borrow {$place_desc} in
--> $DIR/issue-27282-mutate-before-diverging-arm-1.rs:21:14
|
LL | match x {
| - value is immutable in match guard
| - value is immutable in
...
LL | (|| { *x = None; drop(force_fn_once); })();
| ^^ -- borrow occurs due to use of `x` in closure
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0510]: cannot mutably borrow `x` in match guard
error[E0510]: cannot mutably borrow {$place_desc} in
--> $DIR/issue-27282-mutate-before-diverging-arm-2.rs:26:18
|
LL | match x {
| - value is immutable in match guard
| - value is immutable in
...
LL | (|| { *x = None; drop(force_fn_once); })();
| ^^ -- borrow occurs due to use of `x` in closure
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0510]: cannot mutably borrow `x` in match guard
error[E0510]: cannot mutably borrow {$place_desc} in
--> $DIR/issue-27282-mutate-before-diverging-arm-3.rs:20:14
|
LL | match **x {
| --- value is immutable in match guard
| --- value is immutable in
...
LL | (|| { *x = &None; drop(force_fn_once); })();
| ^^ -- borrow occurs due to use of `x` in closure
Expand Down
46 changes: 23 additions & 23 deletions src/test/ui/nll/match-guards-partially-borrow.stderr
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
error[E0510]: cannot assign `q` in match guard
error[E0510]: cannot mutably borrow {$place_desc} in
--> $DIR/match-guards-partially-borrow.rs:55:13
|
LL | match q {
| - value is immutable in match guard
| - value is immutable in
...
LL | q = true;
| ^^^^^^^^ cannot assign
| ^^^^^^^^ cannot mutably borrow

error[E0510]: cannot assign `r` in match guard
error[E0510]: cannot mutably borrow {$place_desc} in
--> $DIR/match-guards-partially-borrow.rs:67:13
|
LL | match r {
| - value is immutable in match guard
| - value is immutable in
...
LL | r = true;
| ^^^^^^^^ cannot assign
| ^^^^^^^^ cannot mutably borrow

error[E0510]: cannot assign `t` in match guard
error[E0510]: cannot mutably borrow {$place_desc} in
--> $DIR/match-guards-partially-borrow.rs:91:13
|
LL | match t {
| - value is immutable in match guard
| - value is immutable in
...
LL | t = true;
| ^^^^^^^^ cannot assign
| ^^^^^^^^ cannot mutably borrow

error[E0510]: cannot mutably borrow `x.0` in match guard
error[E0510]: cannot mutably borrow {$place_desc} in
--> $DIR/match-guards-partially-borrow.rs:105:22
|
LL | match x {
| - value is immutable in match guard
| - value is immutable in
...
LL | Some(ref mut r) => *r = None,
| ^^^^^^^^^ cannot mutably borrow
Expand All @@ -45,41 +45,41 @@ LL | false
LL | } => (), // What value should `s` have in the arm?
| - borrow later used here

error[E0510]: cannot assign `y` in match guard
error[E0510]: cannot mutably borrow {$place_desc} in
--> $DIR/match-guards-partially-borrow.rs:128:13
|
LL | match *y {
| -- value is immutable in match guard
| -- value is immutable in
...
LL | y = &true;
| ^^^^^^^^^ cannot assign
| ^^^^^^^^^ cannot mutably borrow

error[E0510]: cannot assign `z` in match guard
error[E0510]: cannot mutably borrow {$place_desc} in
--> $DIR/match-guards-partially-borrow.rs:139:13
|
LL | match z {
| - value is immutable in match guard
| - value is immutable in
...
LL | z = &true;
| ^^^^^^^^^ cannot assign
| ^^^^^^^^^ cannot mutably borrow

error[E0510]: cannot assign `a` in match guard
error[E0510]: cannot mutably borrow {$place_desc} in
--> $DIR/match-guards-partially-borrow.rs:151:13
|
LL | match a {
| - value is immutable in match guard
| - value is immutable in
...
LL | a = &true;
| ^^^^^^^^^ cannot assign
| ^^^^^^^^^ cannot mutably borrow

error[E0510]: cannot assign `b` in match guard
error[E0510]: cannot mutably borrow {$place_desc} in
--> $DIR/match-guards-partially-borrow.rs:162:13
|
LL | match b {
| - value is immutable in match guard
| - value is immutable in
...
LL | b = &true;
| ^^^^^^^^^ cannot assign
| ^^^^^^^^^ cannot mutably borrow

error: aborting due to 9 previous errors

Expand Down