-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Const drop #88558
Merged
Merged
Const drop #88558
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
9c1a916
cleanup hir hack
fee1-dead d9797d2
Remove unused query
fee1-dead 104e40f
Const dropping
fee1-dead 9125fbf
Do not lint for ~const Drop bounds
fee1-dead f6f5180
Add a simple test case
fee1-dead 48a3ba9
fmt
fee1-dead 894ce92
better test
fee1-dead a13b13f
Const drop selection candidates
fee1-dead 4eab5c1
Failing test
fee1-dead f0a5212
fmt
fee1-dead 1ca83c6
Use trait select logic instead of query
fee1-dead 8211728
Remove the queries
fee1-dead 122e91e
do not require lang item
fee1-dead 49ac725
fix precise live drops
fee1-dead 146abdd
Add another test case + fmt
fee1-dead f749e05
Allow ~const bounds on inherent impls
fee1-dead File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,10 +3,14 @@ | |
//! See the `Qualif` trait for more info. | ||
|
||
use rustc_errors::ErrorReported; | ||
use rustc_hir as hir; | ||
use rustc_infer::infer::TyCtxtInferExt; | ||
use rustc_middle::mir::*; | ||
use rustc_middle::ty::{self, subst::SubstsRef, AdtDef, Ty}; | ||
use rustc_span::DUMMY_SP; | ||
use rustc_trait_selection::traits; | ||
use rustc_trait_selection::traits::{ | ||
self, ImplSource, Obligation, ObligationCause, SelectionContext, | ||
}; | ||
|
||
use super::ConstCx; | ||
|
||
|
@@ -17,7 +21,7 @@ pub fn in_any_value_of_ty( | |
) -> ConstQualifs { | ||
ConstQualifs { | ||
has_mut_interior: HasMutInterior::in_any_value_of_ty(cx, ty), | ||
needs_drop: NeedsDrop::in_any_value_of_ty(cx, ty), | ||
needs_drop: NeedsNonConstDrop::in_any_value_of_ty(cx, ty), | ||
custom_eq: CustomEq::in_any_value_of_ty(cx, ty), | ||
error_occured, | ||
} | ||
|
@@ -97,22 +101,48 @@ impl Qualif for HasMutInterior { | |
/// This must be ruled out (a) because we cannot run `Drop` during compile-time | ||
/// as that might not be a `const fn`, and (b) because implicit promotion would | ||
/// remove side-effects that occur as part of dropping that value. | ||
pub struct NeedsDrop; | ||
pub struct NeedsNonConstDrop; | ||
|
||
impl Qualif for NeedsDrop { | ||
const ANALYSIS_NAME: &'static str = "flow_needs_drop"; | ||
impl Qualif for NeedsNonConstDrop { | ||
const ANALYSIS_NAME: &'static str = "flow_needs_nonconst_drop"; | ||
const IS_CLEARED_ON_MOVE: bool = true; | ||
|
||
fn in_qualifs(qualifs: &ConstQualifs) -> bool { | ||
qualifs.needs_drop | ||
} | ||
|
||
fn in_any_value_of_ty(cx: &ConstCx<'_, 'tcx>, ty: Ty<'tcx>) -> bool { | ||
ty.needs_drop(cx.tcx, cx.param_env) | ||
let drop_trait = if let Some(did) = cx.tcx.lang_items().drop_trait() { | ||
did | ||
} else { | ||
// there is no way to define a type that needs non-const drop | ||
// without having the lang item present. | ||
return false; | ||
Comment on lines
+118
to
+120
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Beautiful |
||
}; | ||
let trait_ref = | ||
ty::TraitRef { def_id: drop_trait, substs: cx.tcx.mk_substs_trait(ty, &[]) }; | ||
let obligation = Obligation::new( | ||
ObligationCause::dummy(), | ||
cx.param_env, | ||
ty::Binder::dummy(ty::TraitPredicate { | ||
trait_ref, | ||
constness: ty::BoundConstness::ConstIfConst, | ||
}), | ||
); | ||
|
||
let implsrc = cx.tcx.infer_ctxt().enter(|infcx| { | ||
let mut selcx = SelectionContext::with_constness(&infcx, hir::Constness::Const); | ||
selcx.select(&obligation) | ||
}); | ||
match implsrc { | ||
Ok(Some(ImplSource::ConstDrop(_))) | ||
| Ok(Some(ImplSource::Param(_, ty::BoundConstness::ConstIfConst))) => false, | ||
_ => true, | ||
} | ||
} | ||
|
||
fn in_adt_inherently(cx: &ConstCx<'_, 'tcx>, adt: &'tcx AdtDef, _: SubstsRef<'tcx>) -> bool { | ||
adt.has_dtor(cx.tcx) | ||
adt.has_non_const_dtor(cx.tcx) | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe keep this for a fast reject?