@@ -188,7 +188,7 @@ impl Conflicts {
188
188
. filter ( |set| set. contains_item ( & canonical_item) )
189
189
{
190
190
for sub in & subs {
191
- let mut new_set = conflict_set. replaced_item ( & canonical_item, ( * * sub) . clone ( ) ) ;
191
+ let mut new_set = conflict_set. replaced_item ( & canonical_item, ( * * sub) . clone ( ) ) . expect ( "`ConflictItem` should be in `ConflictSet`" ) ;
192
192
if !direct_conflict_sets. contains ( & new_set) {
193
193
new_set. set_as_inferred_conflict ( ) ;
194
194
if !transitive_conflict_sets. contains ( & new_set) {
@@ -259,14 +259,17 @@ impl ConflictSet {
259
259
// FIXME: Error if old is not present
260
260
/// Replace an old [`ConflictItem`] with a new one.
261
261
#[ must_use]
262
- pub fn replaced_item ( & self , old : & ConflictItem , new : ConflictItem ) -> Self {
262
+ pub fn replaced_item ( & self , old : & ConflictItem , new : ConflictItem ) -> Result < Self , ConflictError > {
263
263
let mut new_set = self . set . clone ( ) ;
264
+ if !new_set. contains ( old) {
265
+ return Err ( ConflictError :: ReplaceMissingConflictItem ) ;
266
+ }
264
267
new_set. remove ( old) ;
265
268
new_set. insert ( new) ;
266
- Self {
269
+ Ok ( Self {
267
270
set : new_set,
268
271
is_inferred_conflict : false ,
269
- }
272
+ } )
270
273
}
271
274
272
275
/// Mark this [`ConflictSet`] as being inferred from directly
@@ -555,6 +558,8 @@ pub enum ConflictError {
555
558
FoundExtraAndGroup ,
556
559
#[ error( "Cycle detected in transitive conflict inclusion" ) ]
557
560
ConflictInclusionCycle ,
561
+ #[ error( "Expected `ConflictSet` to contain `ConflictItem` to replace" ) ]
562
+ ReplaceMissingConflictItem
558
563
}
559
564
560
565
/// Like [`Conflicts`], but for deserialization in `pyproject.toml`.
0 commit comments