Skip to content

Commit 7102506

Browse files
committed
Fix unwrap panic
1 parent 88cf9cc commit 7102506

File tree

2 files changed

+21
-18
lines changed

2 files changed

+21
-18
lines changed

compiler/rustc_mir/src/dataflow/impls/available_locals.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ impl<'a, 'tcx> AvailableLocals {
6464
&self,
6565
local: Local,
6666
location: Location,
67-
) -> LocalWithLocationIndex {
67+
) -> Option<LocalWithLocationIndex> {
6868
self.local_with_location_map.local_with_specific_location(local, Some(location))
6969
}
7070

@@ -103,7 +103,9 @@ impl AnalysisDomain<'tcx> for AvailableLocals {
103103

104104
// only the function parameters are available
105105
for arg in body.args_iter() {
106-
state.0.insert(self.local_with_location_map.local_with_specific_location(arg, None));
106+
if let Some(l) = self.local_with_location_map.local_with_specific_location(arg, None) {
107+
state.0.insert(l);
108+
}
107109
}
108110
}
109111
}
@@ -197,14 +199,10 @@ impl LocalWithLocationMap {
197199
&self,
198200
local: Local,
199201
location: Option<Location>,
200-
) -> LocalWithLocationIndex {
202+
) -> Option<LocalWithLocationIndex> {
201203
debug!("Looking for {:?} in {:?}", (local, location), self.0);
202-
let locations = self.0.get(&local).unwrap();
203-
*locations
204-
.iter()
205-
.find(|(_, l)| *l == location)
206-
.map(|(location_idx, _)| location_idx)
207-
.unwrap()
204+
let locations = self.0.get(&local)?;
205+
locations.iter().find(|(_, l)| *l == location).map(|(location_idx, _)| *location_idx)
208206
}
209207

210208
fn local_with_locations(
@@ -281,10 +279,12 @@ where
281279
_ => {}
282280
}
283281

284-
let index =
285-
self.local_with_location_map.local_with_specific_location(local, Some(location));
286-
debug!("Inserting {:?} which corresponds to {:?}", index, (local, Some(location)));
287-
self.available.gen(index);
282+
if let Some(index) =
283+
self.local_with_location_map.local_with_specific_location(local, Some(location))
284+
{
285+
debug!("Inserting {:?} which corresponds to {:?}", index, (local, Some(location)));
286+
self.available.gen(index);
287+
};
288288
}
289289
}
290290

compiler/rustc_mir/src/transform/unneeded_deref.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,14 @@ impl<'a, 'tcx> ResultsVisitor<'a, 'tcx> for UnneededDerefVisitor<'a, 'tcx> {
142142
if let Some(place_taken_ref_of_idx) =
143143
analysis.is_available(place_taken_ref_of.local, state)
144144
{
145-
let lhs_idx = analysis.get_local_with_location_index(lhs.local, location);
146-
self.refs.insert(
147-
lhs.local,
148-
(place_taken_ref_of.clone(), (lhs_idx, place_taken_ref_of_idx)),
149-
);
145+
if let Some(lhs_idx) =
146+
analysis.get_local_with_location_index(lhs.local, location)
147+
{
148+
self.refs.insert(
149+
lhs.local,
150+
(place_taken_ref_of.clone(), (lhs_idx, place_taken_ref_of_idx)),
151+
);
152+
};
150153
}
151154
}
152155
StatementKind::Assign(box (_, rvalue)) => match rvalue {

0 commit comments

Comments
 (0)