-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Replace #[default_method_body_is_const]
with #[const_trait]
#96964
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
257f065
Remove `#[default..]` and add `#[const_trait]`
fee1-dead 4d390de
Add a helper function for checking whether a default function in a tr…
oli-obk ecaf7b7
Reduce the scope of a mutable variable
oli-obk 0e3d8d2
Default methods of traits are also AssocFn defs as they essentially d…
oli-obk 2f96fbe
update diagnostic message on removed attribute
oli-obk 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,6 @@ use rustc_hir as hir; | |
use rustc_hir::def_id::LocalDefId; | ||
use rustc_hir::intravisit::{self, Visitor}; | ||
use rustc_middle::hir::nested_filter; | ||
use rustc_middle::ty; | ||
use rustc_middle::ty::query::Providers; | ||
use rustc_middle::ty::TyCtxt; | ||
use rustc_session::parse::feature_err; | ||
|
@@ -64,66 +63,6 @@ pub(crate) fn provide(providers: &mut Providers) { | |
*providers = Providers { check_mod_const_bodies, ..*providers }; | ||
} | ||
|
||
fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) { | ||
let _: Option<_> = try { | ||
if let hir::ItemKind::Impl(ref imp) = item.kind && let hir::Constness::Const = imp.constness { | ||
let trait_def_id = imp.of_trait.as_ref()?.trait_def_id()?; | ||
let ancestors = tcx | ||
.trait_def(trait_def_id) | ||
.ancestors(tcx, item.def_id.to_def_id()) | ||
.ok()?; | ||
let mut to_implement = Vec::new(); | ||
|
||
for trait_item in tcx.associated_items(trait_def_id).in_definition_order() | ||
{ | ||
if let ty::AssocItem { | ||
kind: ty::AssocKind::Fn, | ||
defaultness, | ||
def_id: trait_item_id, | ||
.. | ||
} = *trait_item | ||
{ | ||
// we can ignore functions that do not have default bodies: | ||
// if those are unimplemented it will be caught by typeck. | ||
if !defaultness.has_value() | ||
|| tcx | ||
.has_attr(trait_item_id, sym::default_method_body_is_const) | ||
{ | ||
continue; | ||
} | ||
|
||
let is_implemented = ancestors | ||
.leaf_def(tcx, trait_item_id) | ||
.map(|node_item| !node_item.defining_node.is_from_trait()) | ||
.unwrap_or(false); | ||
|
||
if !is_implemented { | ||
to_implement.push(trait_item_id); | ||
} | ||
} | ||
} | ||
|
||
// all nonconst trait functions (not marked with #[default_method_body_is_const]) | ||
// must be implemented | ||
if !to_implement.is_empty() { | ||
let not_implemented = to_implement | ||
.into_iter() | ||
.map(|did| tcx.item_name(did).to_string()) | ||
.collect::<Vec<_>>() | ||
.join("`, `"); | ||
tcx | ||
.sess | ||
.struct_span_err( | ||
item.span, | ||
"const trait implementations may not use non-const default functions", | ||
) | ||
.note(&format!("`{}` not implemented", not_implemented)) | ||
.emit(); | ||
} | ||
} | ||
Comment on lines
-70
to
-123
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. I think we forgot to actually forbid |
||
}; | ||
} | ||
|
||
#[derive(Copy, Clone)] | ||
struct CheckConstVisitor<'tcx> { | ||
tcx: TyCtxt<'tcx>, | ||
|
@@ -254,7 +193,6 @@ impl<'tcx> Visitor<'tcx> for CheckConstVisitor<'tcx> { | |
|
||
fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) { | ||
intravisit::walk_item(self, item); | ||
check_item(self.tcx, item); | ||
} | ||
|
||
fn visit_anon_const(&mut self, anon: &'tcx hir::AnonConst) { | ||
|
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.
I wish
if
chains could be booleans, sighThere 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.
I'm not sure what this means :)
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.
sorry, meant
let
chains. just don't like how this has to be a mutable assignment lol. not an actionable comment.