Skip to content

Commit fe8b12f

Browse files
committed
only avoid blaming assignments from argument patterns
1 parent 1b2281a commit fe8b12f

14 files changed

+54
-67
lines changed

Diff for: compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2911,7 +2911,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
29112911
(
29122912
name,
29132913
BorrowExplanation::MustBeValidFor {
2914-
category: ConstraintCategory::Assignment { .. },
2914+
category: ConstraintCategory::Assignment,
29152915
from_closure: false,
29162916
region_name:
29172917
RegionName {

Diff for: compiler/rustc_borrowck/src/diagnostics/region_errors.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ impl<'tcx> ConstraintDescription for ConstraintCategory<'tcx> {
4141
fn description(&self) -> &'static str {
4242
// Must end with a space. Allows for empty names to be provided.
4343
match self {
44-
ConstraintCategory::Assignment { .. } => "assignment ",
44+
ConstraintCategory::Assignment => "assignment ",
4545
ConstraintCategory::Return(_) => "returning this value ",
4646
ConstraintCategory::Yield => "yielding this value ",
4747
ConstraintCategory::UseAsConst => "using this value as a constant ",
@@ -481,7 +481,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
481481
(ConstraintCategory::Return(kind), true, false) if self.is_closure_fn_mut(fr) => {
482482
self.report_fnmut_error(&errci, kind)
483483
}
484-
(ConstraintCategory::Assignment { .. }, true, false)
484+
(ConstraintCategory::Assignment, true, false)
485485
| (ConstraintCategory::CallArgument(_), true, false) => {
486486
let mut db = self.report_escaping_data_error(&errci);
487487

@@ -673,7 +673,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
673673
// Revert to the normal error in these cases.
674674
// Assignments aren't "escapes" in function items.
675675
if (fr_name_and_span.is_none() && outlived_fr_name_and_span.is_none())
676-
|| (matches!(category, ConstraintCategory::Assignment { .. })
676+
|| (*category == ConstraintCategory::Assignment
677677
&& self.regioncx.universal_regions().defining_ty.is_fn_def())
678678
|| self.regioncx.universal_regions().defining_ty.is_const()
679679
{

Diff for: compiler/rustc_borrowck/src/region_infer/mod.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -2080,25 +2080,22 @@ impl<'tcx> RegionInferenceContext<'tcx> {
20802080
| ConstraintCategory::CallArgument(_)
20812081
| ConstraintCategory::CopyBound
20822082
| ConstraintCategory::SizedBound
2083-
| ConstraintCategory::Assignment { has_interesting_ty: true }
2083+
| ConstraintCategory::Assignment
20842084
| ConstraintCategory::Usage
20852085
| ConstraintCategory::ClosureUpvar(_) => 2,
2086-
// Give assignments a lower priority when flagged as less likely to be interesting.
2087-
// In particular, de-prioritize MIR assignments lowered from argument patterns.
2088-
ConstraintCategory::Assignment { has_interesting_ty: false } => 3,
20892086
// Generic arguments are unlikely to be what relates regions together
2090-
ConstraintCategory::TypeAnnotation(AnnotationSource::GenericArg) => 4,
2087+
ConstraintCategory::TypeAnnotation(AnnotationSource::GenericArg) => 3,
20912088
// We handle predicates and opaque types specially; don't prioritize them here.
2092-
ConstraintCategory::Predicate(_) | ConstraintCategory::OpaqueType => 5,
2089+
ConstraintCategory::Predicate(_) | ConstraintCategory::OpaqueType => 4,
20932090
// `Boring` constraints can correspond to user-written code and have useful spans,
20942091
// but don't provide any other useful information for diagnostics.
2095-
ConstraintCategory::Boring => 6,
2092+
ConstraintCategory::Boring => 5,
20962093
// `BoringNoLocation` constraints can point to user-written code, but are less
20972094
// specific, and are not used for relations that would make sense to blame.
2098-
ConstraintCategory::BoringNoLocation => 7,
2095+
ConstraintCategory::BoringNoLocation => 6,
20992096
// Do not blame internal constraints.
2100-
ConstraintCategory::Internal => 8,
2101-
ConstraintCategory::IllegalUniverse => 9,
2097+
ConstraintCategory::Internal => 7,
2098+
ConstraintCategory::IllegalUniverse => 8,
21022099
}
21032100
};
21042101

Diff for: compiler/rustc_borrowck/src/type_check/mod.rs

+15-13
Original file line numberDiff line numberDiff line change
@@ -892,18 +892,20 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
892892
Some(l) if !body.local_decls[l].is_user_variable() => {
893893
ConstraintCategory::Boring
894894
}
895-
Some(l) => ConstraintCategory::Assignment {
896-
has_interesting_ty: body.local_decls[l].user_ty.is_some()
897-
|| matches!(
898-
body.local_decls[l].local_info(),
899-
LocalInfo::User(BindingForm::Var(VarBindingForm {
900-
opt_ty_info: Some(_),
901-
..
902-
}))
903-
),
904-
},
905-
// Assignments to projections should be considered interesting.
906-
_ => ConstraintCategory::Assignment { has_interesting_ty: true },
895+
Some(_)
896+
if let Some(body_id) = tcx
897+
.hir_node_by_def_id(body.source.def_id().expect_local())
898+
.body_id()
899+
&& let params = tcx.hir().body(body_id).params
900+
&& params
901+
.iter()
902+
.any(|param| param.span.contains(stmt.source_info.span)) =>
903+
{
904+
// Assignments generated from lowering argument patterns shouldn't be called
905+
// "assignments" in diagnostics and aren't interesting to blame for errors.
906+
ConstraintCategory::Boring
907+
}
908+
_ => ConstraintCategory::Assignment,
907909
};
908910
debug!(
909911
"assignment category: {:?} {:?}",
@@ -1238,7 +1240,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
12381240
ConstraintCategory::Boring
12391241
}
12401242
// The return type of a call is interesting for diagnostics.
1241-
_ => ConstraintCategory::Assignment { has_interesting_ty: true },
1243+
_ => ConstraintCategory::Assignment,
12421244
};
12431245

12441246
let locations = term_location.to_locations();

Diff for: compiler/rustc_middle/src/mir/query.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -242,12 +242,7 @@ pub enum ConstraintCategory<'tcx> {
242242
CallArgument(Option<Ty<'tcx>>),
243243
CopyBound,
244244
SizedBound,
245-
Assignment {
246-
/// Whether this assignment is likely to be interesting to refer to in diagnostics.
247-
/// Currently, this is true when it's assigning to a projection, when it's assigning from
248-
/// the return value of a call, and when it has a user-provided type annotation.
249-
has_interesting_ty: bool,
250-
},
245+
Assignment,
251246
/// A constraint that came from a usage of a variable (e.g. in an ADT expression
252247
/// like `Foo { field: my_val }`)
253248
Usage,

Diff for: tests/mir-opt/nll/region_subtyping_basic.main.nll.0.32bit.mir

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
| '?2 live at {bb1[0]}
1818
| '?3 live at {bb1[1..=3]}
1919
| '?4 live at {bb1[4..=7], bb2[0..=2]}
20-
| '?2: '?3 due to Assignment { has_interesting_ty: false } at Single(bb1[0]) ($DIR/region_subtyping_basic.rs:19:13: 19:18 (#0)
21-
| '?3: '?4 due to Assignment { has_interesting_ty: false } at Single(bb1[3]) ($DIR/region_subtyping_basic.rs:20:13: 20:14 (#0)
20+
| '?2: '?3 due to Assignment at Single(bb1[0]) ($DIR/region_subtyping_basic.rs:19:13: 19:18 (#0)
21+
| '?3: '?4 due to Assignment at Single(bb1[3]) ($DIR/region_subtyping_basic.rs:20:13: 20:14 (#0)
2222
|
2323
| Borrows
2424
| bw0: issued at bb1[0] in '?2

Diff for: tests/mir-opt/nll/region_subtyping_basic.main.nll.0.64bit.mir

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
| '?2 live at {bb1[0]}
1818
| '?3 live at {bb1[1..=3]}
1919
| '?4 live at {bb1[4..=7], bb2[0..=2]}
20-
| '?2: '?3 due to Assignment { has_interesting_ty: false } at Single(bb1[0]) ($DIR/region_subtyping_basic.rs:19:13: 19:18 (#0)
21-
| '?3: '?4 due to Assignment { has_interesting_ty: false } at Single(bb1[3]) ($DIR/region_subtyping_basic.rs:20:13: 20:14 (#0)
20+
| '?2: '?3 due to Assignment at Single(bb1[0]) ($DIR/region_subtyping_basic.rs:19:13: 19:18 (#0)
21+
| '?3: '?4 due to Assignment at Single(bb1[3]) ($DIR/region_subtyping_basic.rs:20:13: 20:14 (#0)
2222
|
2323
| Borrows
2424
| bw0: issued at bb1[0] in '?2

Diff for: tests/mir-opt/storage_ranges.main.nll.0.mir

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
| '?1 live at {bb0[0..=22]}
1616
| '?2 live at {bb0[10]}
1717
| '?3 live at {bb0[11]}
18-
| '?2: '?3 due to Assignment { has_interesting_ty: false } at Single(bb0[10]) ($DIR/storage_ranges.rs:7:17: 7:25 (#0)
18+
| '?2: '?3 due to Assignment at Single(bb0[10]) ($DIR/storage_ranges.rs:7:17: 7:25 (#0)
1919
|
2020
| Borrows
2121
| bw0: issued at bb0[10] in '?2

Diff for: tests/ui/fn/fn_def_coercion.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ fn j<'a, 'b, 'c: 'a + 'b>(a: &'a (), b: &'b (), c: &'c ()) {
4747
fn k<'a, 'b, 'c: 'a + 'b>(a: &'a (), b: &'b (), c: &'c ()) {
4848
let x = match true {
4949
true => foo::<&'c ()>, //~ ERROR lifetime may not live long enough
50-
false => foo::<&'a ()>,
50+
false => foo::<&'a ()>, //~ ERROR lifetime may not live long enough
5151
};
5252

5353
x(a);
54-
x(b); //~ ERROR lifetime may not live long enough
54+
x(b);
5555
x(c);
5656
}
5757

Diff for: tests/ui/fn/fn_def_coercion.stderr

+6-3
Original file line numberDiff line numberDiff line change
@@ -133,17 +133,20 @@ LL | true => foo::<&'c ()>,
133133
= help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance
134134

135135
error: lifetime may not live long enough
136-
--> $DIR/fn_def_coercion.rs:54:5
136+
--> $DIR/fn_def_coercion.rs:50:18
137137
|
138138
LL | fn k<'a, 'b, 'c: 'a + 'b>(a: &'a (), b: &'b (), c: &'c ()) {
139139
| -- -- lifetime `'b` defined here
140140
| |
141141
| lifetime `'a` defined here
142142
...
143-
LL | x(b);
144-
| ^^^^ argument requires that `'b` must outlive `'a`
143+
LL | false => foo::<&'a ()>,
144+
| ^^^^^^^^^^^^^ assignment requires that `'b` must outlive `'a`
145145
|
146146
= help: consider adding the following bound: `'b: 'a`
147+
= note: requirement occurs because of a function pointer to `foo`
148+
= note: the function `foo` is invariant over the parameter `T`
149+
= help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance
147150

148151
help: the following changes may resolve your lifetime errors
149152
|

Diff for: tests/ui/nll/user-annotations/adt-tuple-struct-calls.stderr

+6-12
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@ error[E0597]: `c` does not live long enough
44
LL | let c = 66;
55
| - binding `c` declared here
66
LL | let f = SomeStruct::<&'static u32>;
7+
| -------------------------- assignment requires that `c` is borrowed for `'static`
78
LL | f(&c);
8-
| --^^-
9-
| | |
10-
| | borrowed value does not live long enough
11-
| argument requires that `c` is borrowed for `'static`
9+
| ^^ borrowed value does not live long enough
1210
LL | }
1311
| - `c` dropped here while still borrowed
1412

@@ -20,11 +18,9 @@ LL | fn annot_reference_named_lifetime<'a>(_d: &'a u32) {
2018
LL | let c = 66;
2119
| - binding `c` declared here
2220
LL | let f = SomeStruct::<&'a u32>;
21+
| --------------------- assignment requires that `c` is borrowed for `'a`
2322
LL | f(&c);
24-
| --^^-
25-
| | |
26-
| | borrowed value does not live long enough
27-
| argument requires that `c` is borrowed for `'a`
23+
| ^^ borrowed value does not live long enough
2824
LL | }
2925
| - `c` dropped here while still borrowed
3026

@@ -37,11 +33,9 @@ LL | let _closure = || {
3733
LL | let c = 66;
3834
| - binding `c` declared here
3935
LL | let f = SomeStruct::<&'a u32>;
36+
| --------------------- assignment requires that `c` is borrowed for `'a`
4037
LL | f(&c);
41-
| --^^-
42-
| | |
43-
| | borrowed value does not live long enough
44-
| argument requires that `c` is borrowed for `'a`
38+
| ^^ borrowed value does not live long enough
4539
LL | };
4640
| - `c` dropped here while still borrowed
4741

Diff for: tests/ui/nll/user-annotations/method-ufcs-1.stderr

+3-4
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ error[E0597]: `a` does not live long enough
44
LL | let a = 22;
55
| - binding `a` declared here
66
...
7+
LL | let x = <&'static u32 as Bazoom<_>>::method;
8+
| ----------------------------------- assignment requires that `a` is borrowed for `'static`
79
LL | x(&a, b, c);
8-
| --^^-------
9-
| | |
10-
| | borrowed value does not live long enough
11-
| argument requires that `a` is borrowed for `'static`
10+
| ^^ borrowed value does not live long enough
1211
LL | }
1312
| - `a` dropped here while still borrowed
1413

Diff for: tests/ui/nll/user-annotations/method-ufcs-2.stderr

+3-4
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ error[E0597]: `a` does not live long enough
44
LL | let a = 22;
55
| - binding `a` declared here
66
...
7+
LL | let x = <&'static u32 as Bazoom<_>>::method;
8+
| ----------------------------------- assignment requires that `a` is borrowed for `'static`
79
LL | x(&a, b, c);
8-
| --^^-------
9-
| | |
10-
| | borrowed value does not live long enough
11-
| argument requires that `a` is borrowed for `'static`
10+
| ^^ borrowed value does not live long enough
1211
LL | }
1312
| - `a` dropped here while still borrowed
1413

Diff for: tests/ui/nll/user-annotations/promoted-annotation.stderr

+2-4
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@ LL | fn foo<'a>() {
66
LL | let x = 0;
77
| - binding `x` declared here
88
LL | let f = &drop::<&'a i32>;
9+
| ---------------- assignment requires that `x` is borrowed for `'a`
910
LL | f(&x);
10-
| --^^-
11-
| | |
12-
| | borrowed value does not live long enough
13-
| argument requires that `x` is borrowed for `'a`
11+
| ^^ borrowed value does not live long enough
1412
LL |
1513
LL | }
1614
| - `x` dropped here while still borrowed

0 commit comments

Comments
 (0)