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

Proof of Concept: add #[defines] attribute and require it for all type-alias-impl-trait sites that register a hidden type #128440

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

oli-obk
Copy link
Contributor

@oli-obk oli-obk commented Jul 31, 2024

This PR is not meant to be merged (I will not be pushing changes in the next 4 months), but serves as a starting point for anyone wanting to make progress on this. It should hopefully be simple to rebase and not require you to edit 200+ tests, as these edits are done here and you can just cherry pick them out (no need for attribution, just squash as you want)

I left TODOs for everything that I think needs to be done.

  • differentiate between no attribute and an empty attribute (#[defines()]), the latter opting out of impl-trait-in-assoc types automatic opaque collection in the signature (or in general, any #[defines(SomeOhterTait)] attribute behaving as an override for the default collection, too).

An interesting follow-up could also be to automatically fill in RPITs into that list in ast lowering, so that opaque_types_defined_by doesn't need to walk for RPITs anymore.

@petrochenkov can you have a look at the resolver changes so that anyone picking this up will know what to change?

…ng from the signature to the list of opaques that are being defined
Comment on lines -250 to +232
self.tcx.dcx().emit_err(TaitForwardCompat2 {
let guar = self.tcx.dcx().emit_err(TaitForwardCompat2 {
span: self
.tcx
.def_ident_span(item_def_id)
.unwrap_or_else(|| self.tcx.def_span(item_def_id)),
opaque_type_span: self.tcx.def_span(self.def_id),
opaque_type: self.tcx.def_path_str(self.def_id),
});
// TODO: land this change as a separate PR on master, it works on its own.
// Avoid "opaque type not constrained" errors on the opaque itself.
self.found = Some(ty::OpaqueHiddenType {
span: DUMMY_SP,
ty: Ty::new_error(self.tcx, guar),
});
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This change should be a fairly trivial PR on its own (just removes some redundant errors)

@oli-obk oli-obk added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Jul 31, 2024
@rust-log-analyzer
Copy link
Collaborator

The job mingw-check-tidy failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)

COPY host-x86_64/mingw-check/validate-toolstate.sh /scripts/
COPY host-x86_64/mingw-check/validate-error-codes.sh /scripts/

# NOTE: intentionally uses python2 for x.py so we can test it still works.
# validate-toolstate only runs in our CI, so it's ok for it to only support python3.
ENV SCRIPT TIDY_PRINT_DIFF=1 python2.7 ../x.py test \
           --stage 0 src/tools/tidy tidyselftest --extra-checks=py:lint,cpp:fmt
# This file is autogenerated by pip-compile with Python 3.10
# by the following command:
#
#    pip-compile --allow-unsafe --generate-hashes reuse-requirements.in
---
#13 2.712 Building wheels for collected packages: reuse
#13 2.713   Building wheel for reuse (pyproject.toml): started
#13 2.960   Building wheel for reuse (pyproject.toml): finished with status 'done'
#13 2.961   Created wheel for reuse: filename=reuse-4.0.3-cp310-cp310-manylinux_2_35_x86_64.whl size=132715 sha256=dfa09868353292d98f811d3efdb0d54d07389e808efc71d68e3b93c514bf8bec
#13 2.961   Stored in directory: /tmp/pip-ephem-wheel-cache-j43iujuh/wheels/3d/8d/0a/e0fc6aba4494b28a967ab5eaf951c121d9c677958714e34532
#13 2.964 Installing collected packages: boolean-py, binaryornot, tomlkit, reuse, python-debian, markupsafe, license-expression, jinja2, chardet, attrs
#13 3.357 Successfully installed attrs-23.2.0 binaryornot-0.4.4 boolean-py-4.0 chardet-5.2.0 jinja2-3.1.4 license-expression-30.3.0 markupsafe-2.1.5 python-debian-0.1.49 reuse-4.0.3 tomlkit-0.13.0
#13 3.358 WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
#13 3.867 Collecting virtualenv
#13 3.867 Collecting virtualenv
#13 3.902   Downloading virtualenv-20.26.3-py3-none-any.whl (5.7 MB)
#13 3.992      ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 5.7/5.7 MB 64.4 MB/s eta 0:00:00
#13 4.032 Collecting distlib<1,>=0.3.7
#13 4.035   Downloading distlib-0.3.8-py2.py3-none-any.whl (468 kB)
#13 4.043      ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 468.9/468.9 KB 89.1 MB/s eta 0:00:00
#13 4.071 Collecting platformdirs<5,>=3.9.1
#13 4.074   Downloading platformdirs-4.2.2-py3-none-any.whl (18 kB)
#13 4.108 Collecting filelock<4,>=3.12.2
#13 4.111   Downloading filelock-3.15.4-py3-none-any.whl (16 kB)
#13 4.192 Installing collected packages: distlib, platformdirs, filelock, virtualenv
#13 4.378 Successfully installed distlib-0.3.8 filelock-3.15.4 platformdirs-4.2.2 virtualenv-20.26.3
#13 DONE 4.5s

#14 [7/8] COPY host-x86_64/mingw-check/validate-toolstate.sh /scripts/
#14 DONE 0.0s
---
DirectMap4k:      229312 kB
DirectMap2M:     7110656 kB
DirectMap1G:    11534336 kB
##[endgroup]
Executing TIDY_PRINT_DIFF=1 python2.7 ../x.py test            --stage 0 src/tools/tidy tidyselftest --extra-checks=py:lint,cpp:fmt
+ TIDY_PRINT_DIFF=1 python2.7 ../x.py test --stage 0 src/tools/tidy tidyselftest --extra-checks=py:lint,cpp:fmt
    Finished `dev` profile [unoptimized] target(s) in 0.04s
##[endgroup]
downloading https://ci-artifacts.rust-lang.org/rustc-builds-alt/0b5eb7ba7bd796fb39c8bb6acd9ef6c140f28b65/rust-dev-nightly-x86_64-unknown-linux-gnu.tar.xz
extracting /checkout/obj/build/cache/llvm-0b5eb7ba7bd796fb39c8bb6acd9ef6c140f28b65-true/rust-dev-nightly-x86_64-unknown-linux-gnu.tar.xz to /checkout/obj/build/x86_64-unknown-linux-gnu/ci-llvm
---
fmt check
fmt: checked 5409 files
tidy check
tidy: Skipping binary file check, read-only filesystem
##[error]tidy error: /checkout/compiler/rustc_ty_utils/src/opaque_types.rs:177: TODO is used for tasks that should be done before merging a PR; If you want to leave a message in the codebase use FIXME
##[error]tidy error: /checkout/compiler/rustc_ty_utils/src/opaque_types.rs:183: TODO is used for tasks that should be done before merging a PR; If you want to leave a message in the codebase use FIXME
##[error]tidy error: /checkout/compiler/rustc_ty_utils/src/opaque_types.rs:186: TODO is used for tasks that should be done before merging a PR; If you want to leave a message in the codebase use FIXME
##[error]tidy error: /checkout/compiler/rustc_ty_utils/src/opaque_types.rs:209: TODO is used for tasks that should be done before merging a PR; If you want to leave a message in the codebase use FIXME
##[error]tidy error: /checkout/compiler/rustc_ty_utils/src/opaque_types.rs:283: TODO is used for tasks that should be done before merging a PR; If you want to leave a message in the codebase use FIXME
##[error]tidy error: /checkout/compiler/rustc_ty_utils/src/opaque_types.rs:353: TODO is used for tasks that should be done before merging a PR; If you want to leave a message in the codebase use FIXME
##[error]tidy error: /checkout/compiler/rustc_ast_lowering/src/lib.rs:936: TODO is used for tasks that should be done before merging a PR; If you want to leave a message in the codebase use FIXME
##[error]tidy error: /checkout/tests/ui/type-alias-impl-trait/type-alias-impl-trait-assoc-impl-trait.rs:16: line longer than 100 chars
##[error]tidy error: /checkout/tests/ui/type-alias-impl-trait/type-alias-impl-trait-assoc-impl-trait.rs:16: TODO is used for tasks that should be done before merging a PR; If you want to leave a message in the codebase use FIXME
##[error]tidy error: /checkout/compiler/rustc_trait_selection/src/traits/select/mod.rs:2399: TODO is used for tasks that should be done before merging a PR; If you want to leave a message in the codebase use FIXME
##[error]tidy error: /checkout/compiler/rustc_trait_selection/src/error_reporting/infer/note_and_explain.rs:386: TODO is used for tasks that should be done before merging a PR; If you want to leave a message in the codebase use FIXME
##[error]tidy error: /checkout/compiler/rustc_hir_analysis/src/collect/type_of/opaque.rs:227: TODO is used for tasks that should be done before merging a PR; If you want to leave a message in the codebase use FIXME
##[error]tidy error: /checkout/compiler/rustc_resolve/src/late.rs:1357: TODO is used for tasks that should be done before merging a PR; If you want to leave a message in the codebase use FIXME
removing old virtual environment
creating virtual environment at '/checkout/obj/build/venv' using 'python3.10'
Requirement already satisfied: pip in ./build/venv/lib/python3.10/site-packages (24.1)
  Using cached pip-24.2-py3-none-any.whl.metadata (3.6 kB)
Downloading pip-24.2-py3-none-any.whl (1.8 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.8/1.8 MB 64.1 MB/s eta 0:00:00
Installing collected packages: pip
---
All checks passed!
checking C++ file formatting
some tidy checks failed

Command LD_LIBRARY_PATH="/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-bootstrap-tools/x86_64-unknown-linux-gnu/release/deps:/checkout/obj/build/x86_64-unknown-linux-gnu/stage0/lib" RUSTC="/checkout/obj/build/x86_64-unknown-linux-gnu/stage0/bin/rustc" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-tools-bin/rust-tidy" "/checkout" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage0/bin/cargo" "/checkout/obj/build" "4" "--extra-checks=py:lint,cpp:fmt" (failure_mode=DelayFail) did not execute successfully.
Created at: src/core/build_steps/tool.rs:1113:23
Executed at: src/core/build_steps/test.rs:1078:29

Build completed unsuccessfully in 0:01:19

@petrochenkov petrochenkov self-assigned this Jul 31, 2024
@petrochenkov petrochenkov added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 31, 2024
@bors
Copy link
Contributor

bors commented Aug 1, 2024

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

}
MetaItemKind::NameValue(_) => vec![],
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this function ever used?
The ids used to identify defines paths are created with next_node_id in resolver, so they do not match ids kept in meta-item paths (if such ids are filled to non-dummy values at all).

// TODO: error reporting and tests that go through these code paths
PathResult::Module(_) => todo!(),
PathResult::NonModule(partial_res) => {
let id = self.r.next_node_id();
Copy link
Contributor

Choose a reason for hiding this comment

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

Are these ids necessary?
Probably simpler to push the resolutions straight to self.r.defines.entry(attr.id), without doing record_partial_res.

match self.resolve_path(
&Segment::from_path(&item.path),
Some(Namespace::TypeNS),
None,
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not a speculative resolution, so there should be Some here, but the real fix would be using smart_resolve_path instead of resolve_path above.

fn check_defines(&self, attr: &Attribute, span: Span, target: Target) -> bool {
match target {
Target::Fn
| Target::Closure
Copy link
Contributor

Choose a reason for hiding this comment

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

The closure case is not processed in resolve.

@petrochenkov
Copy link
Contributor

Simple paths in attributes are not as bad as rust-lang/rfcs#3659, but still sort of meh, but probably acceptable.

We don't currently have any resolved paths (staying after macro expansion) in attributes in the proper language.
We have links in doc comments though, which are also simple paths processed during late resolution, and they also have to be kept in a separate special side table.

@petrochenkov petrochenkov added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 14, 2024
@petrochenkov petrochenkov removed their assignment Aug 14, 2024
@petrochenkov
Copy link
Contributor

Also, defines is probably a popular enough name, so the chance of regressions like #128888 may be high, if it becomes a built-in attribute.

@traviscross traviscross added the F-type_alias_impl_trait `#[feature(type_alias_impl_trait)]` label Aug 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F-type_alias_impl_trait `#[feature(type_alias_impl_trait)]` S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author.
Development

Successfully merging this pull request may close these issues.

6 participants