From b1cc95da2338af399e65c7186540e2f412c8fbe0 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Mon, 24 Oct 2022 03:33:42 +0000 Subject: [PATCH 1/9] Remap early bound lifetimes too --- .../src/check/compare_method.rs | 2 +- src/test/ui/impl-trait/in-trait/early.rs | 23 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 src/test/ui/impl-trait/in-trait/early.rs diff --git a/compiler/rustc_hir_analysis/src/check/compare_method.rs b/compiler/rustc_hir_analysis/src/check/compare_method.rs index e72f18012ab33..32f66b06f8358 100644 --- a/compiler/rustc_hir_analysis/src/check/compare_method.rs +++ b/compiler/rustc_hir_analysis/src/check/compare_method.rs @@ -597,7 +597,7 @@ pub fn collect_trait_impl_trait_tys<'tcx>( let num_trait_substs = trait_to_impl_substs.len(); let num_impl_substs = tcx.generics_of(impl_m.container_id(tcx)).params.len(); let ty = tcx.fold_regions(ty, |region, _| { - let ty::ReFree(_) = region.kind() else { return region; }; + let (ty::ReFree(_) | ty::ReEarlyBound(_)) = region.kind() else { return region; }; let Some(ty::ReEarlyBound(e)) = map.get(®ion.into()).map(|r| r.expect_region().kind()) else { tcx diff --git a/src/test/ui/impl-trait/in-trait/early.rs b/src/test/ui/impl-trait/in-trait/early.rs new file mode 100644 index 0000000000000..9c1c2b5033904 --- /dev/null +++ b/src/test/ui/impl-trait/in-trait/early.rs @@ -0,0 +1,23 @@ +// check-pass +// edition:2021 + +#![feature(async_fn_in_trait, return_position_impl_trait_in_trait)] +#![allow(incomplete_features)] + +pub trait Foo { + async fn bar<'a: 'a>(&'a mut self); +} + +impl Foo for () { + async fn bar<'a: 'a>(&'a mut self) {} +} + +pub trait Foo2 { + fn bar<'a: 'a>(&'a mut self) -> impl Sized + 'a; +} + +impl Foo2 for () { + fn bar<'a: 'a>(&'a mut self) -> impl Sized + 'a {} +} + +fn main() {} From 0185be2ce45efdd988df2b2ee69b764f9e3c7f6c Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Thu, 27 Oct 2022 13:07:07 +0200 Subject: [PATCH 2/9] Migrate line numbers CSS to CSS variables --- src/librustdoc/html/static/css/rustdoc.css | 8 ++++++++ src/librustdoc/html/static/css/themes/ayu.css | 12 +++--------- src/librustdoc/html/static/css/themes/dark.css | 14 +++----------- src/librustdoc/html/static/css/themes/light.css | 14 +++----------- 4 files changed, 17 insertions(+), 31 deletions(-) diff --git a/src/librustdoc/html/static/css/rustdoc.css b/src/librustdoc/html/static/css/rustdoc.css index b60c77195639f..c20912dc69a3f 100644 --- a/src/librustdoc/html/static/css/rustdoc.css +++ b/src/librustdoc/html/static/css/rustdoc.css @@ -571,10 +571,18 @@ ul.block, .block li { padding: 13px 8px; border-top-left-radius: 5px; border-bottom-left-radius: 5px; + border-color: var(--example-line-numbers-border-color); } .src-line-numbers span { cursor: pointer; + color: var(--src-line-numbers-span-color); +} +.src-line-numbers .line-highlighted { + background-color: var(--src-line-number-highlighted-background-color); +} +.src-line-numbers :target { + background-color: transparent; } .search-loading { diff --git a/src/librustdoc/html/static/css/themes/ayu.css b/src/librustdoc/html/static/css/themes/ayu.css index 33817c16808c6..fdfdb3e196672 100644 --- a/src/librustdoc/html/static/css/themes/ayu.css +++ b/src/librustdoc/html/static/css/themes/ayu.css @@ -55,6 +55,9 @@ Original by Dempfi (https://github.com/dempfi/ayu) --code-highlight-question-mark-color: #ff9011; --code-highlight-comment-color: #788797; --code-highlight-doc-comment-color: #a1ac88; + --example-line-numbers-border-color: none; + --src-line-numbers-span-color: #5c6773; + --src-line-number-highlighted-background-color: rgba(255, 236, 164, 0.06); } .slider { @@ -112,10 +115,8 @@ pre, .rustdoc.source .example-wrap { color: #ff7733; } -.src-line-numbers span { color: #5c6773; } .src-line-numbers .line-highlighted { color: #708090; - background-color: rgba(255, 236, 164, 0.06); padding-right: 4px; border-right: 1px solid #ffb44c; } @@ -170,13 +171,6 @@ details.rustdoc-toggle > summary::before { color: #788797; } -.src-line-numbers :target { background-color: transparent; } - -pre.example-line-numbers { - color: #5c67736e; - border: none; -} - a.test-arrow { font-size: 100%; color: #788797; diff --git a/src/librustdoc/html/static/css/themes/dark.css b/src/librustdoc/html/static/css/themes/dark.css index d88710288b905..361d3d4a22597 100644 --- a/src/librustdoc/html/static/css/themes/dark.css +++ b/src/librustdoc/html/static/css/themes/dark.css @@ -50,6 +50,9 @@ --code-highlight-question-mark-color: #ff9011; --code-highlight-comment-color: #8d8d8b; --code-highlight-doc-comment-color: #8ca375; + --example-line-numbers-border-color: #4a4949; + --src-line-numbers-span-color: #3b91e2; + --src-line-number-highlighted-background-color: #0a042f; } .slider { @@ -69,11 +72,6 @@ input:focus + .slider { drop-shadow(0 -1px 0 #fff) } -.src-line-numbers span { color: #3B91E2; } -.src-line-numbers .line-highlighted { - background-color: #0a042f !important; -} - .content .item-info::before { color: #ccc; } body.source .example-wrap pre.rust a { @@ -95,12 +93,6 @@ details.rustdoc-toggle > summary::before { filter: invert(69%) sepia(60%) saturate(6613%) hue-rotate(184deg) brightness(100%) contrast(91%); } -.src-line-numbers :target { background-color: transparent; } - -pre.example-line-numbers { - border-color: #4a4949; -} - a.test-arrow { color: #dedede; background-color: rgba(78, 139, 202, 0.2); diff --git a/src/librustdoc/html/static/css/themes/light.css b/src/librustdoc/html/static/css/themes/light.css index cadc71dab9591..5eb4bbcf834bb 100644 --- a/src/librustdoc/html/static/css/themes/light.css +++ b/src/librustdoc/html/static/css/themes/light.css @@ -50,6 +50,9 @@ --code-highlight-question-mark-color: #ff9011; --code-highlight-comment-color: #8e908c; --code-highlight-doc-comment-color: #4d4d4c; + --example-line-numbers-border-color: #c7c7c7; + --src-line-numbers-span-color: #c67e2d; + --src-line-number-highlighted-background-color: #fdffd3; } .slider { @@ -68,11 +71,6 @@ input:focus + .slider { */ } -.src-line-numbers span { color: #c67e2d; } -.src-line-numbers .line-highlighted { - background-color: #FDFFD3 !important; -} - .content .item-info::before { color: #ccc; } body.source .example-wrap pre.rust a { @@ -90,12 +88,6 @@ body.source .example-wrap pre.rust a { filter: invert(44%) sepia(18%) saturate(23%) hue-rotate(317deg) brightness(96%) contrast(93%); } -.src-line-numbers :target { background-color: transparent; } - -pre.example-line-numbers { - border-color: #c7c7c7; -} - a.test-arrow { color: #f5f5f5; background-color: rgba(78, 139, 202, 0.2); From 71e824de26189c514150840cae493f596e362f1c Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Wed, 26 Oct 2022 17:51:20 +0200 Subject: [PATCH 3/9] Add tests for source line numbers colors --- src/test/rustdoc-gui/source-code-page.goml | 44 +++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/src/test/rustdoc-gui/source-code-page.goml b/src/test/rustdoc-gui/source-code-page.goml index 29d65fc7ebc7a..a2dac2aa681d5 100644 --- a/src/test/rustdoc-gui/source-code-page.goml +++ b/src/test/rustdoc-gui/source-code-page.goml @@ -1,5 +1,6 @@ // Checks that the interactions with the source code pages are working as expected. goto: "file://" + |DOC_PATH| + "/src/test_docs/lib.rs.html" +show-text: true // Check that we can click on the line number. click: ".src-line-numbers > span:nth-child(4)" // This is the span for line 4. // Ensure that the page URL was updated. @@ -12,6 +13,48 @@ assert-attribute: (".src-line-numbers > span:nth-child(4)", {"class": "line-high assert-attribute: (".src-line-numbers > span:nth-child(5)", {"class": "line-highlighted"}) assert-attribute: (".src-line-numbers > span:nth-child(6)", {"class": "line-highlighted"}) assert-attribute-false: (".src-line-numbers > span:nth-child(7)", {"class": "line-highlighted"}) + +define-function: ( + "check-colors", + (theme, color, background_color, highlight_color, highlight_background_color), + [ + ("local-storage", {"rustdoc-theme": |theme|, "rustdoc-use-system-theme": "false"}), + ("reload"), + ("assert-css", ( + ".src-line-numbers > span:not(.line-highlighted)", + {"color": |color|, "background-color": |background_color|}, + ALL, + )), + ("assert-css", ( + ".src-line-numbers > span.line-highlighted", + {"color": |highlight_color|, "background-color": |highlight_background_color|}, + ALL, + )), + ], +) + +call-function: ("check-colors", { + "theme": "ayu", + "color": "rgb(92, 103, 115)", + "background_color": "rgba(0, 0, 0, 0)", + "highlight_color": "rgb(112, 128, 144)", + "highlight_background_color": "rgba(255, 236, 164, 0.06)", +}) +call-function: ("check-colors", { + "theme": "dark", + "color": "rgb(59, 145, 226)", + "background_color": "rgba(0, 0, 0, 0)", + "highlight_color": "rgb(59, 145, 226)", + "highlight_background_color": "rgb(10, 4, 47)", +}) +call-function: ("check-colors", { + "theme": "light", + "color": "rgb(198, 126, 45)", + "background_color": "rgba(0, 0, 0, 0)", + "highlight_color": "rgb(198, 126, 45)", + "highlight_background_color": "rgb(253, 255, 211)", +}) + // This is to ensure that the content is correctly align with the line numbers. compare-elements-position: ("//*[@id='1']", ".rust > code > span", ("y")) @@ -20,7 +63,6 @@ assert-css: (".src-line-numbers", {"text-align": "right"}) // Now let's check that clicking on something else than the line number doesn't // do anything (and certainly not add a `#NaN` to the URL!). -show-text: true goto: "file://" + |DOC_PATH| + "/src/test_docs/lib.rs.html" // We use this assert-position to know where we will click. assert-position: ("//*[@id='1']", {"x": 104, "y": 112}) From f4ac137f3c7952c3b8e8278916ac030a6c3a9b6e Mon Sep 17 00:00:00 2001 From: Rageking8 Date: Thu, 27 Oct 2022 22:56:20 +0800 Subject: [PATCH 4/9] add test for issue 36007 --- src/test/ui/coercion/issue-36007.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 src/test/ui/coercion/issue-36007.rs diff --git a/src/test/ui/coercion/issue-36007.rs b/src/test/ui/coercion/issue-36007.rs new file mode 100644 index 0000000000000..78812df870d02 --- /dev/null +++ b/src/test/ui/coercion/issue-36007.rs @@ -0,0 +1,20 @@ +// check-pass +#![feature(coerce_unsized, unsize)] + +use std::marker::Unsize; +use std::ops::CoerceUnsized; + +struct Foo(Box); + +impl CoerceUnsized> for Foo where T: Unsize {} + +struct Bar; + +trait Baz {} + +impl Baz for Bar {} + +fn main() { + let foo = Foo(Box::new(Bar)); + let foobar: Foo = foo; +} From dd912add18e0a0498e3032781ea73ee846ee76cf Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Thu, 27 Oct 2022 09:48:06 -0700 Subject: [PATCH 5/9] rustdoc: stop hiding focus outlines on non-rustdoc-toggle details tags We really shouldn't be overriding this kind of stuff unless the browser default is really broken (like outlining the thing that isn't clickable). This directly reverts b8f4e74cbc938d3448507d422c98061c2b71c922. --- src/librustdoc/html/static/css/rustdoc.css | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/librustdoc/html/static/css/rustdoc.css b/src/librustdoc/html/static/css/rustdoc.css index 84a1fa2e28ece..5eb876b641c11 100644 --- a/src/librustdoc/html/static/css/rustdoc.css +++ b/src/librustdoc/html/static/css/rustdoc.css @@ -292,10 +292,6 @@ p:last-child { margin: 0; } -summary { - outline: none; -} - /* Fix some style changes due to normalize.css 8 */ button { @@ -1538,6 +1534,8 @@ details.rustdoc-toggle > summary.hideme { details.rustdoc-toggle > summary { list-style: none; + /* focus outline is shown on `::before` instead of this */ + outline: none; } details.rustdoc-toggle > summary::-webkit-details-marker, details.rustdoc-toggle > summary::marker { From 4d9114fe80613c304bc064d10b64d496cb7d3cdc Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Fri, 28 Oct 2022 01:18:26 +0800 Subject: [PATCH 6/9] Update cargo 6 commits in 9210810d1fd7b51ae0439a0a363cc50e36963455..7e484fc1a766f56dbc95380f45719698e0c82749 2022-10-25 22:31:50 +0000 to 2022-10-27 15:20:57 +0000 - fix(publish): Block until it is in index (rust-lang/cargo#11062) - Add Accept-Encoding request header to enable compression (rust-lang/cargo#11292) - Update contrib docs for highfive transition (rust-lang/cargo#11294) - Migrate from highfive to triagebot (rust-lang/cargo#11293) - Fix dupe word typos (rust-lang/cargo#11287) - Fix confusing error messages when using -Zsparse-registry (rust-lang/cargo#11283) --- src/tools/cargo | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tools/cargo b/src/tools/cargo index 9210810d1fd7b..7e484fc1a766f 160000 --- a/src/tools/cargo +++ b/src/tools/cargo @@ -1 +1 @@ -Subproject commit 9210810d1fd7b51ae0439a0a363cc50e36963455 +Subproject commit 7e484fc1a766f56dbc95380f45719698e0c82749 From c00ff9c4d01e8ba64bf03aaf3bb7940874d3e094 Mon Sep 17 00:00:00 2001 From: Boxy Date: Thu, 27 Oct 2022 21:48:41 +0100 Subject: [PATCH 7/9] DoIt --- .../locales/en-US/hir_analysis.ftl | 2 + .../rustc_hir_analysis/src/collect/type_of.rs | 32 ++++++++- compiler/rustc_hir_analysis/src/errors.rs | 7 ++ src/test/ui/resolve/issue-23305.rs | 2 +- src/test/ui/resolve/issue-23305.stderr | 20 +----- src/test/ui/resolve/resolve-self-in-impl.rs | 8 +-- .../ui/resolve/resolve-self-in-impl.stderr | 68 +++---------------- 7 files changed, 56 insertions(+), 83 deletions(-) diff --git a/compiler/rustc_error_messages/locales/en-US/hir_analysis.ftl b/compiler/rustc_error_messages/locales/en-US/hir_analysis.ftl index 7ac44312695d9..7cedfca5728c7 100644 --- a/compiler/rustc_error_messages/locales/en-US/hir_analysis.ftl +++ b/compiler/rustc_error_messages/locales/en-US/hir_analysis.ftl @@ -146,3 +146,5 @@ hir_analysis_const_impl_for_non_const_trait = hir_analysis_const_bound_for_non_const_trait = ~const can only be applied to `#[const_trait]` traits + +hir_analysis_self_in_impl_self = `Self` is not valid at this location diff --git a/compiler/rustc_hir_analysis/src/collect/type_of.rs b/compiler/rustc_hir_analysis/src/collect/type_of.rs index 32f359a815819..877cd75587be8 100644 --- a/compiler/rustc_hir_analysis/src/collect/type_of.rs +++ b/compiler/rustc_hir_analysis/src/collect/type_of.rs @@ -319,7 +319,37 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> { } } ItemKind::TyAlias(self_ty, _) => icx.to_ty(self_ty), - ItemKind::Impl(hir::Impl { self_ty, .. }) => icx.to_ty(*self_ty), + ItemKind::Impl( + hir::Impl { self_ty, .. } + ) => { + struct MyVisitor(bool); + impl<'v> hir::intravisit::Visitor<'v> for MyVisitor { + fn visit_ty(&mut self, t: &'v Ty<'v>) { + if matches!( + &t.kind, + TyKind::Path(hir::QPath::Resolved( + _, + Path { + res: hir::def::Res::SelfTyAlias { .. }, + .. + }, + )) + ) { + self.0 = true; + return; + } + hir::intravisit::walk_ty(self, t); + } + } + + let mut my_visitor = MyVisitor(false); + my_visitor.visit_ty(self_ty); + + match my_visitor.0 { + true => { tcx.sess.emit_err(crate::errors::SelfInImplSelf { span: self_ty.span}); tcx.ty_error() }, + false => icx.to_ty(*self_ty), + } + }, ItemKind::Fn(..) => { let substs = InternalSubsts::identity_for_item(tcx, def_id.to_def_id()); tcx.mk_fn_def(def_id.to_def_id(), substs) diff --git a/compiler/rustc_hir_analysis/src/errors.rs b/compiler/rustc_hir_analysis/src/errors.rs index bd0c1f5dd1098..c3bfd0437ada3 100644 --- a/compiler/rustc_hir_analysis/src/errors.rs +++ b/compiler/rustc_hir_analysis/src/errors.rs @@ -270,3 +270,10 @@ pub struct ConstBoundForNonConstTrait { #[primary_span] pub span: Span, } + +#[derive(Diagnostic)] +#[diag(hir_analysis_self_in_impl_self)] +pub struct SelfInImplSelf { + #[primary_span] + pub span: Span, +} diff --git a/src/test/ui/resolve/issue-23305.rs b/src/test/ui/resolve/issue-23305.rs index 95635e12a63b1..3e3d5ee2fda0e 100644 --- a/src/test/ui/resolve/issue-23305.rs +++ b/src/test/ui/resolve/issue-23305.rs @@ -3,6 +3,6 @@ pub trait ToNbt { } impl dyn ToNbt {} -//~^ ERROR cycle detected +//~^ ERROR `Self` is not valid at this location fn main() {} diff --git a/src/test/ui/resolve/issue-23305.stderr b/src/test/ui/resolve/issue-23305.stderr index 20aeb7b995a61..1b2745de321c0 100644 --- a/src/test/ui/resolve/issue-23305.stderr +++ b/src/test/ui/resolve/issue-23305.stderr @@ -1,22 +1,8 @@ -error[E0391]: cycle detected when computing type of `` - --> $DIR/issue-23305.rs:5:16 +error: `Self` is not valid at this location + --> $DIR/issue-23305.rs:5:6 | LL | impl dyn ToNbt {} - | ^^^^ - | - = note: ...which immediately requires computing type of `` again -note: cycle used when collecting item types in top-level module - --> $DIR/issue-23305.rs:1:1 - | -LL | / pub trait ToNbt { -LL | | fn new(val: T) -> Self; -LL | | } -LL | | -... | -LL | | -LL | | fn main() {} - | |____________^ + | ^^^^^^^^^^^^^^^ error: aborting due to previous error -For more information about this error, try `rustc --explain E0391`. diff --git a/src/test/ui/resolve/resolve-self-in-impl.rs b/src/test/ui/resolve/resolve-self-in-impl.rs index 024fdc51ea318..f845e780bbfe7 100644 --- a/src/test/ui/resolve/resolve-self-in-impl.rs +++ b/src/test/ui/resolve/resolve-self-in-impl.rs @@ -11,10 +11,10 @@ impl Tr for S where Self: Copy {} // OK impl Tr for S where S: Copy {} // OK impl Tr for S where Self::A: Copy {} // OK -impl Tr for Self {} //~ ERROR cycle detected -impl Tr for S {} //~ ERROR cycle detected -impl Self {} //~ ERROR cycle detected -impl S {} //~ ERROR cycle detected +impl Tr for Self {} //~ ERROR `Self` is not valid at this location +impl Tr for S {} //~ ERROR `Self` is not valid at this location +impl Self {} //~ ERROR `Self` is not valid at this location +impl S {} //~ ERROR `Self` is not valid at this location impl Tr for S {} //~ ERROR cycle detected fn main() {} diff --git a/src/test/ui/resolve/resolve-self-in-impl.stderr b/src/test/ui/resolve/resolve-self-in-impl.stderr index aa99c1a33352e..93e2b4695fc06 100644 --- a/src/test/ui/resolve/resolve-self-in-impl.stderr +++ b/src/test/ui/resolve/resolve-self-in-impl.stderr @@ -1,78 +1,26 @@ -error[E0391]: cycle detected when computing type of `` +error: `Self` is not valid at this location --> $DIR/resolve-self-in-impl.rs:14:13 | LL | impl Tr for Self {} | ^^^^ - | - = note: ...which immediately requires computing type of `` again -note: cycle used when collecting item types in top-level module - --> $DIR/resolve-self-in-impl.rs:1:1 - | -LL | / #![feature(associated_type_defaults)] -LL | | -LL | | struct S(T); -LL | | trait Tr { -... | -LL | | -LL | | fn main() {} - | |____________^ -error[E0391]: cycle detected when computing type of `` - --> $DIR/resolve-self-in-impl.rs:15:15 +error: `Self` is not valid at this location + --> $DIR/resolve-self-in-impl.rs:15:13 | LL | impl Tr for S {} - | ^^^^ - | - = note: ...which immediately requires computing type of `` again -note: cycle used when collecting item types in top-level module - --> $DIR/resolve-self-in-impl.rs:1:1 - | -LL | / #![feature(associated_type_defaults)] -LL | | -LL | | struct S(T); -LL | | trait Tr { -... | -LL | | -LL | | fn main() {} - | |____________^ + | ^^^^^^^ -error[E0391]: cycle detected when computing type of `` +error: `Self` is not valid at this location --> $DIR/resolve-self-in-impl.rs:16:6 | LL | impl Self {} | ^^^^ - | - = note: ...which immediately requires computing type of `` again -note: cycle used when collecting item types in top-level module - --> $DIR/resolve-self-in-impl.rs:1:1 - | -LL | / #![feature(associated_type_defaults)] -LL | | -LL | | struct S(T); -LL | | trait Tr { -... | -LL | | -LL | | fn main() {} - | |____________^ -error[E0391]: cycle detected when computing type of `` - --> $DIR/resolve-self-in-impl.rs:17:8 +error: `Self` is not valid at this location + --> $DIR/resolve-self-in-impl.rs:17:6 | LL | impl S {} - | ^^^^ - | - = note: ...which immediately requires computing type of `` again -note: cycle used when collecting item types in top-level module - --> $DIR/resolve-self-in-impl.rs:1:1 - | -LL | / #![feature(associated_type_defaults)] -LL | | -LL | | struct S(T); -LL | | trait Tr { -... | -LL | | -LL | | fn main() {} - | |____________^ + | ^^^^^^^ error[E0391]: cycle detected when computing trait implemented by `` --> $DIR/resolve-self-in-impl.rs:18:1 From ca5a6e43dd6fcfddf436d36f6907d8ef44c7105b Mon Sep 17 00:00:00 2001 From: Boxy Date: Thu, 27 Oct 2022 22:18:26 +0100 Subject: [PATCH 8/9] use proper spans --- .../locales/en-US/hir_analysis.ftl | 4 +- .../rustc_hir_analysis/src/collect/type_of.rs | 13 +++--- compiler/rustc_hir_analysis/src/errors.rs | 6 ++- src/test/ui/resolve/issue-23305.rs | 2 +- src/test/ui/resolve/issue-23305.stderr | 8 ++-- src/test/ui/resolve/resolve-self-in-impl.rs | 9 +++-- .../ui/resolve/resolve-self-in-impl.stderr | 40 +++++++++++++------ 7 files changed, 54 insertions(+), 28 deletions(-) diff --git a/compiler/rustc_error_messages/locales/en-US/hir_analysis.ftl b/compiler/rustc_error_messages/locales/en-US/hir_analysis.ftl index 7cedfca5728c7..855866be6279f 100644 --- a/compiler/rustc_error_messages/locales/en-US/hir_analysis.ftl +++ b/compiler/rustc_error_messages/locales/en-US/hir_analysis.ftl @@ -147,4 +147,6 @@ hir_analysis_const_impl_for_non_const_trait = hir_analysis_const_bound_for_non_const_trait = ~const can only be applied to `#[const_trait]` traits -hir_analysis_self_in_impl_self = `Self` is not valid at this location +hir_analysis_self_in_impl_self = + `Self` is not valid in the self type of an impl block + .note = replace `Self` with a different type \ No newline at end of file diff --git a/compiler/rustc_hir_analysis/src/collect/type_of.rs b/compiler/rustc_hir_analysis/src/collect/type_of.rs index 877cd75587be8..2032a4bce60cd 100644 --- a/compiler/rustc_hir_analysis/src/collect/type_of.rs +++ b/compiler/rustc_hir_analysis/src/collect/type_of.rs @@ -322,7 +322,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> { ItemKind::Impl( hir::Impl { self_ty, .. } ) => { - struct MyVisitor(bool); + struct MyVisitor(Vec); impl<'v> hir::intravisit::Visitor<'v> for MyVisitor { fn visit_ty(&mut self, t: &'v Ty<'v>) { if matches!( @@ -335,19 +335,22 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> { }, )) ) { - self.0 = true; + self.0.push(t.span); return; } hir::intravisit::walk_ty(self, t); } } - let mut my_visitor = MyVisitor(false); + let mut my_visitor = MyVisitor(vec![]); my_visitor.visit_ty(self_ty); match my_visitor.0 { - true => { tcx.sess.emit_err(crate::errors::SelfInImplSelf { span: self_ty.span}); tcx.ty_error() }, - false => icx.to_ty(*self_ty), + spans if spans.len() > 0 => { + tcx.sess.emit_err(crate::errors::SelfInImplSelf { span: spans.into(), note: (), }); + tcx.ty_error() + }, + _ => icx.to_ty(*self_ty), } }, ItemKind::Fn(..) => { diff --git a/compiler/rustc_hir_analysis/src/errors.rs b/compiler/rustc_hir_analysis/src/errors.rs index c3bfd0437ada3..6ed8244d119de 100644 --- a/compiler/rustc_hir_analysis/src/errors.rs +++ b/compiler/rustc_hir_analysis/src/errors.rs @@ -1,7 +1,7 @@ //! Errors emitted by `rustc_hir_analysis`. -use rustc_errors::IntoDiagnostic; use rustc_errors::{error_code, Applicability, DiagnosticBuilder, ErrorGuaranteed, Handler}; +use rustc_errors::{IntoDiagnostic, MultiSpan}; use rustc_macros::{Diagnostic, LintDiagnostic}; use rustc_middle::ty::Ty; use rustc_span::{symbol::Ident, Span, Symbol}; @@ -275,5 +275,7 @@ pub struct ConstBoundForNonConstTrait { #[diag(hir_analysis_self_in_impl_self)] pub struct SelfInImplSelf { #[primary_span] - pub span: Span, + pub span: MultiSpan, + #[note] + pub note: (), } diff --git a/src/test/ui/resolve/issue-23305.rs b/src/test/ui/resolve/issue-23305.rs index 3e3d5ee2fda0e..6d7fe7c50a230 100644 --- a/src/test/ui/resolve/issue-23305.rs +++ b/src/test/ui/resolve/issue-23305.rs @@ -3,6 +3,6 @@ pub trait ToNbt { } impl dyn ToNbt {} -//~^ ERROR `Self` is not valid at this location +//~^ ERROR `Self` is not valid in the self type of an impl block fn main() {} diff --git a/src/test/ui/resolve/issue-23305.stderr b/src/test/ui/resolve/issue-23305.stderr index 1b2745de321c0..aad1b583a3244 100644 --- a/src/test/ui/resolve/issue-23305.stderr +++ b/src/test/ui/resolve/issue-23305.stderr @@ -1,8 +1,10 @@ -error: `Self` is not valid at this location - --> $DIR/issue-23305.rs:5:6 +error: `Self` is not valid in the self type of an impl block + --> $DIR/issue-23305.rs:5:16 | LL | impl dyn ToNbt {} - | ^^^^^^^^^^^^^^^ + | ^^^^ + | + = note: replace `Self` with a different type error: aborting due to previous error diff --git a/src/test/ui/resolve/resolve-self-in-impl.rs b/src/test/ui/resolve/resolve-self-in-impl.rs index f845e780bbfe7..d0872d1b76f20 100644 --- a/src/test/ui/resolve/resolve-self-in-impl.rs +++ b/src/test/ui/resolve/resolve-self-in-impl.rs @@ -11,10 +11,11 @@ impl Tr for S where Self: Copy {} // OK impl Tr for S where S: Copy {} // OK impl Tr for S where Self::A: Copy {} // OK -impl Tr for Self {} //~ ERROR `Self` is not valid at this location -impl Tr for S {} //~ ERROR `Self` is not valid at this location -impl Self {} //~ ERROR `Self` is not valid at this location -impl S {} //~ ERROR `Self` is not valid at this location +impl Tr for Self {} //~ ERROR `Self` is not valid in the self type of an impl block +impl Tr for S {} //~ ERROR `Self` is not valid in the self type of an impl block +impl Self {} //~ ERROR `Self` is not valid in the self type of an impl block +impl S {} //~ ERROR `Self` is not valid in the self type of an impl block +impl (Self, Self) {} //~ ERROR `Self` is not valid in the self type of an impl block impl Tr for S {} //~ ERROR cycle detected fn main() {} diff --git a/src/test/ui/resolve/resolve-self-in-impl.stderr b/src/test/ui/resolve/resolve-self-in-impl.stderr index 93e2b4695fc06..9f9ed68898f6c 100644 --- a/src/test/ui/resolve/resolve-self-in-impl.stderr +++ b/src/test/ui/resolve/resolve-self-in-impl.stderr @@ -1,34 +1,50 @@ -error: `Self` is not valid at this location +error: `Self` is not valid in the self type of an impl block --> $DIR/resolve-self-in-impl.rs:14:13 | LL | impl Tr for Self {} | ^^^^ + | + = note: replace `Self` with a different type -error: `Self` is not valid at this location - --> $DIR/resolve-self-in-impl.rs:15:13 +error: `Self` is not valid in the self type of an impl block + --> $DIR/resolve-self-in-impl.rs:15:15 | LL | impl Tr for S {} - | ^^^^^^^ + | ^^^^ + | + = note: replace `Self` with a different type -error: `Self` is not valid at this location +error: `Self` is not valid in the self type of an impl block --> $DIR/resolve-self-in-impl.rs:16:6 | LL | impl Self {} | ^^^^ + | + = note: replace `Self` with a different type -error: `Self` is not valid at this location - --> $DIR/resolve-self-in-impl.rs:17:6 +error: `Self` is not valid in the self type of an impl block + --> $DIR/resolve-self-in-impl.rs:17:8 | LL | impl S {} - | ^^^^^^^ + | ^^^^ + | + = note: replace `Self` with a different type + +error: `Self` is not valid in the self type of an impl block + --> $DIR/resolve-self-in-impl.rs:18:7 + | +LL | impl (Self, Self) {} + | ^^^^ ^^^^ + | + = note: replace `Self` with a different type -error[E0391]: cycle detected when computing trait implemented by `` - --> $DIR/resolve-self-in-impl.rs:18:1 +error[E0391]: cycle detected when computing trait implemented by `` + --> $DIR/resolve-self-in-impl.rs:19:1 | LL | impl Tr for S {} | ^^^^^^^^^^^^^^^^^^^^^^ | - = note: ...which immediately requires computing trait implemented by `` again + = note: ...which immediately requires computing trait implemented by `` again note: cycle used when collecting item types in top-level module --> $DIR/resolve-self-in-impl.rs:1:1 | @@ -41,6 +57,6 @@ LL | | LL | | fn main() {} | |____________^ -error: aborting due to 5 previous errors +error: aborting due to 6 previous errors For more information about this error, try `rustc --explain E0391`. From b3425587a669d81b45f0d14f7fd6ae6108bf9444 Mon Sep 17 00:00:00 2001 From: Boxy Date: Thu, 27 Oct 2022 22:29:16 +0100 Subject: [PATCH 9/9] tidy + move logic to fn --- .../locales/en-US/hir_analysis.ftl | 4 +-- compiler/rustc_hir/src/hir.rs | 24 ++++++++++++++ .../rustc_hir_analysis/src/collect/type_of.rs | 33 +++---------------- 3 files changed, 30 insertions(+), 31 deletions(-) diff --git a/compiler/rustc_error_messages/locales/en-US/hir_analysis.ftl b/compiler/rustc_error_messages/locales/en-US/hir_analysis.ftl index 855866be6279f..62f004da0cae3 100644 --- a/compiler/rustc_error_messages/locales/en-US/hir_analysis.ftl +++ b/compiler/rustc_error_messages/locales/en-US/hir_analysis.ftl @@ -147,6 +147,6 @@ hir_analysis_const_impl_for_non_const_trait = hir_analysis_const_bound_for_non_const_trait = ~const can only be applied to `#[const_trait]` traits -hir_analysis_self_in_impl_self = +hir_analysis_self_in_impl_self = `Self` is not valid in the self type of an impl block - .note = replace `Self` with a different type \ No newline at end of file + .note = replace `Self` with a different type diff --git a/compiler/rustc_hir/src/hir.rs b/compiler/rustc_hir/src/hir.rs index bc149e48d89e8..9a9e40a3f43a7 100644 --- a/compiler/rustc_hir/src/hir.rs +++ b/compiler/rustc_hir/src/hir.rs @@ -2418,6 +2418,30 @@ impl<'hir> Ty<'hir> { } final_ty } + + pub fn find_self_aliases(&self) -> Vec { + use crate::intravisit::Visitor; + struct MyVisitor(Vec); + impl<'v> Visitor<'v> for MyVisitor { + fn visit_ty(&mut self, t: &'v Ty<'v>) { + if matches!( + &t.kind, + TyKind::Path(QPath::Resolved( + _, + Path { res: crate::def::Res::SelfTyAlias { .. }, .. }, + )) + ) { + self.0.push(t.span); + return; + } + crate::intravisit::walk_ty(self, t); + } + } + + let mut my_visitor = MyVisitor(vec![]); + my_visitor.visit_ty(self); + my_visitor.0 + } } /// Not represented directly in the AST; referred to by name through a `ty_path`. diff --git a/compiler/rustc_hir_analysis/src/collect/type_of.rs b/compiler/rustc_hir_analysis/src/collect/type_of.rs index 2032a4bce60cd..20903a68d6d93 100644 --- a/compiler/rustc_hir_analysis/src/collect/type_of.rs +++ b/compiler/rustc_hir_analysis/src/collect/type_of.rs @@ -319,36 +319,11 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> { } } ItemKind::TyAlias(self_ty, _) => icx.to_ty(self_ty), - ItemKind::Impl( - hir::Impl { self_ty, .. } - ) => { - struct MyVisitor(Vec); - impl<'v> hir::intravisit::Visitor<'v> for MyVisitor { - fn visit_ty(&mut self, t: &'v Ty<'v>) { - if matches!( - &t.kind, - TyKind::Path(hir::QPath::Resolved( - _, - Path { - res: hir::def::Res::SelfTyAlias { .. }, - .. - }, - )) - ) { - self.0.push(t.span); - return; - } - hir::intravisit::walk_ty(self, t); - } - } - - let mut my_visitor = MyVisitor(vec![]); - my_visitor.visit_ty(self_ty); - - match my_visitor.0 { - spans if spans.len() > 0 => { + ItemKind::Impl(hir::Impl { self_ty, .. }) => { + match self_ty.find_self_aliases() { + spans if spans.len() > 0 => { tcx.sess.emit_err(crate::errors::SelfInImplSelf { span: spans.into(), note: (), }); - tcx.ty_error() + tcx.ty_error() }, _ => icx.to_ty(*self_ty), }