Skip to content
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 5 commits into from
May 30, 2022

Conversation

oli-obk
Copy link
Contributor

@oli-obk oli-obk commented May 12, 2022

pulled out of #96077

related issues: #67792 and #92158

cc @fee1-dead

This is groundwork to only allowing impl const Trait for traits that are marked with #[const_trait]. This is necessary to prevent adding a new default method from becoming a breaking change (as it could be a non-const fn).

@rust-highfive
Copy link
Collaborator

Hey! It looks like you've submitted a new PR for the library teams!

If this PR contains changes to any rust-lang/rust public library APIs then please comment with r? rust-lang/libs-api @rustbot label +T-libs-api -T-libs to request review from a libs-api team reviewer. If you're unsure where your change falls no worries, just leave it as is and the reviewer will take a look and make a decision to forward on if necessary.

Examples of T-libs-api changes:

  • Stabilizing library features
  • Introducing insta-stable changes such as new implementations of existing stable traits on existing stable types
  • Introducing new or changing existing unstable library APIs (excluding permanently unstable features / features without a tracking issue)
  • Changing public documentation in ways that create new stability guarantees
  • Changing observable runtime behavior of library APIs

@rustbot rustbot added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. labels May 12, 2022
@rust-highfive
Copy link
Collaborator

r? @compiler-errors

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 12, 2022
@bors
Copy link
Contributor

bors commented May 12, 2022

☔ The latest upstream changes (presumably #95562) made this pull request unmergeable. Please resolve the merge conflicts.

@fee1-dead
Copy link
Member

(Please do not r? me because I have written parts of this PR.)

Copy link
Member

@jhpratt jhpratt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Part of me would like the syntax to require const fn for the default methods, as it's not immediately clear that it's the case with this change. But I understand that the syntax/attributes is largely in flux, so that's not a deal-breaker for me.

Overall, the PR looks good. Just some minor nits.

compiler/rustc_metadata/src/rmeta/encoder.rs Outdated Show resolved Hide resolved
compiler/rustc_passes/src/check_attr.rs Outdated Show resolved Hide resolved
@rust-log-analyzer

This comment was marked as resolved.

@jhpratt

This comment was marked as off-topic.

@fee1-dead

This comment was marked as off-topic.

@jhpratt

This comment was marked as off-topic.

@bors
Copy link
Contributor

bors commented May 17, 2022

☔ The latest upstream changes (presumably #96825) made this pull request unmergeable. Please resolve the merge conflicts.

Copy link
Member

@jhpratt jhpratt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM pending rebase.

Copy link
Member

@compiler-errors compiler-errors left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r=me, left a few comments (or ignore them, that's fine too)

// At this point, it is only legal when the caller is in a trait
// marked with #[const_trait], and the callee is in the same trait.
let mut nonconst_call_permission = false;
if let Some(callee_trait) = tcx.trait_of_item(callee)
Copy link
Member

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, sigh

Copy link
Member

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 :)

Copy link
Member

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.

compiler/rustc_metadata/src/rmeta/encoder.rs Outdated Show resolved Hide resolved
compiler/rustc_span/src/symbol.rs Outdated Show resolved Hide resolved
@oli-obk
Copy link
Contributor Author

oli-obk commented May 30, 2022

@bors r=compiler-errors

@bors
Copy link
Contributor

bors commented May 30, 2022

📌 Commit 2f96fbe has been approved by compiler-errors

@bors bors removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 30, 2022
@bors bors added the S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. label May 30, 2022
@bors
Copy link
Contributor

bors commented May 30, 2022

⌛ Testing commit 2f96fbe with merge 5c780b9...

@bors
Copy link
Contributor

bors commented May 30, 2022

☀️ Test successful - checks-actions
Approved by: compiler-errors
Pushing 5c780b9 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label May 30, 2022
@bors bors merged commit 5c780b9 into rust-lang:master May 30, 2022
@rustbot rustbot added this to the 1.63.0 milestone May 30, 2022
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (5c780b9): comparison url.

Instruction count

  • Primary benchmarks: 😿 relevant regressions found
  • Secondary benchmarks: mixed results
mean1 max count2
Regressions 😿
(primary)
0.2% 0.3% 9
Regressions 😿
(secondary)
0.2% 0.3% 3
Improvements 🎉
(primary)
N/A N/A 0
Improvements 🎉
(secondary)
-1.1% -1.1% 3
All 😿🎉 (primary) 0.2% 0.3% 9

Max RSS (memory usage)

Results
  • Primary benchmarks: mixed results
  • Secondary benchmarks: mixed results
mean1 max count2
Regressions 😿
(primary)
1.5% 2.5% 2
Regressions 😿
(secondary)
3.6% 3.9% 2
Improvements 🎉
(primary)
-1.3% -1.3% 1
Improvements 🎉
(secondary)
-2.6% -3.2% 2
All 😿🎉 (primary) 0.6% 2.5% 3

Cycles

Results
  • Primary benchmarks: 😿 relevant regression found
  • Secondary benchmarks: no relevant changes found
mean1 max count2
Regressions 😿
(primary)
2.2% 2.2% 1
Regressions 😿
(secondary)
N/A N/A 0
Improvements 🎉
(primary)
N/A N/A 0
Improvements 🎉
(secondary)
N/A N/A 0
All 😿🎉 (primary) 2.2% 2.2% 1

If you disagree with this performance assessment, please file an issue in rust-lang/rustc-perf.

Next Steps: If you can justify the regressions found in this perf run, please indicate this with @rustbot label: +perf-regression-triaged along with sufficient written justification. If you cannot justify the regressions please open an issue or create a new PR that fixes the regressions, add a comment linking to the newly created issue or PR, and then add the perf-regression-triaged label to this PR.

@rustbot label: +perf-regression

Footnotes

  1. the arithmetic mean of the percent change 2 3

  2. number of relevant changes 2 3

@rustbot rustbot added the perf-regression Performance regression. label May 30, 2022
@oli-obk oli-obk deleted the const_trait_mvp branch May 30, 2022 13:34
@pnkfelix
Copy link
Member

pnkfelix commented Jun 1, 2022

  • The primary regressions are mostly in variants of stm32f4, with a two serde and one syn thrown in for good measure.
  • The improvements are solely to ctfe stress test, which I guess makes sense given the PR?
  • Anyway, the regressions seem minor, and they are contained to an unstable feature that the stdlib is using.
  • Marking as triaged.

@rustbot label: +perf-regression-triaged

@rustbot rustbot added the perf-regression-triaged The performance regression has been triaged. label Jun 1, 2022
Comment on lines -70 to -123
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();
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we forgot to actually forbid impl const on non #[const_trait] which would be here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. perf-regression Performance regression. perf-regression-triaged The performance regression has been triaged. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants