Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: rust-lang/rust
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: compiler-errors/rust
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: invalid-res-call
Choose a head ref
  • 1 commit
  • 3 files changed
  • 1 contributor

Commits on Jun 25, 2025

  1. Don't suggest rewriting call if resolution was invalid

    compiler-errors committed Jun 25, 2025
    Copy the full SHA
    26377fd View commit details
Showing with 45 additions and 3 deletions.
  1. +8 −3 compiler/rustc_resolve/src/late/diagnostics.rs
  2. +18 −0 tests/ui/privacy/ctor-private-from-impl.rs
  3. +19 −0 tests/ui/privacy/ctor-private-from-impl.stderr
11 changes: 8 additions & 3 deletions compiler/rustc_resolve/src/late/diagnostics.rs
Original file line number Diff line number Diff line change
@@ -816,8 +816,13 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
return (true, suggested_candidates, candidates);
}

// If the first argument in call is `self` suggest calling a method.
if let Some((call_span, args_span)) = self.call_has_self_arg(source) {
// If the first argument in call is `self` suggest calling a method,
// even if we didn't find an associated item. However, only suggest
// this if there's no `Res`, since if there was a resolution but it
// was invalid, it's more likely not a typo of this form.
if let Some((call_span, args_span)) = self.call_has_self_arg(source)
&& res.is_none()
{
let mut args_snippet = String::new();
if let Some(args_span) = args_span {
if let Ok(snippet) = self.r.tcx.sess.source_map().span_to_snippet(args_span) {
@@ -829,7 +834,7 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
call_span,
format!("try calling `{ident}` as a method"),
format!("self.{path_str}({args_snippet})"),
Applicability::MachineApplicable,
Applicability::MaybeIncorrect,
);
return (true, suggested_candidates, candidates);
}
18 changes: 18 additions & 0 deletions tests/ui/privacy/ctor-private-from-impl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
pub struct MyStruct {}

mod submodule {
use super::MyStruct;

pub struct MyRef<'a>(&'a MyStruct);
}

use self::submodule::MyRef;

impl MyStruct {
pub fn method(&self) -> MyRef {
MyRef(self)
//~^ ERROR cannot initialize a tuple struct which contains private fields
}
}

fn main() {}
19 changes: 19 additions & 0 deletions tests/ui/privacy/ctor-private-from-impl.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
error[E0423]: cannot initialize a tuple struct which contains private fields
--> $DIR/ctor-private-from-impl.rs:13:9
|
LL | MyRef(self)
| ^^^^^
|
note: constructor is not visible here due to private fields
--> $DIR/ctor-private-from-impl.rs:6:26
|
LL | pub struct MyRef<'a>(&'a MyStruct);
| ^^^^^^^^^^^^ private field
help: consider making the field publicly accessible
|
LL | pub struct MyRef<'a>(pub &'a MyStruct);
| +++

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0423`.