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

location / scoping of Impl #120753

Closed
stroucki opened this issue Feb 7, 2024 · 1 comment · Fixed by #120393
Closed

location / scoping of Impl #120753

stroucki opened this issue Feb 7, 2024 · 1 comment · Fixed by #120393
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-feature-request Category: A feature request, i.e: not implemented / a PR. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@stroucki
Copy link

stroucki commented Feb 7, 2024

Hi,

I am learning Rust via rust-book.cs.brown.edu and I think one of the examples had an Impl for a struct placed within a function. So I was thinking about where Impls can be placed, and whether they can be out of scope. I wrote up the below code (without the added scoping) and it worked. However, it still worked when I added a layer of scoping as below.

I consulted a friend who said this may warrant a warning from the compiler, and I should create an issue for it.

struct Foo {
  x: i32,
}

pub fn main() {
{ // will still work with or without this extra scope
  impl Foo {
    fn inc(&mut self) {
      self.x+=1;
    }
  }
} // extra scope

  let mut test = Foo{ x:1 };
  test.inc();
  println!("Foo: {}", test.x);
}

Rustc version: rustc 1.75.0 (82e1608 2023-12-21) (built from a source tarball)

@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Feb 7, 2024
@Urgau
Copy link
Member

Urgau commented Feb 7, 2024

See RFC 3373: Avoid non-local definitions in functions.

It's currently being implemented in #120393.

@saethlin saethlin added A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. C-feature-request Category: A feature request, i.e: not implemented / a PR. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Feb 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-feature-request Category: A feature request, i.e: not implemented / a PR. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants