-
Notifications
You must be signed in to change notification settings - Fork 167
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
Fixed #2500 #3390
base: master
Are you sure you want to change the base?
Fixed #2500 #3390
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Copyright (C) 2020-2025 Free Software Foundation, Inc. | ||
|
||
// This file is part of GCC. | ||
|
||
// GCC is free software; you can redistribute it and/or modify it under | ||
// the terms of the GNU General Public License as published by the Free | ||
// Software Foundation; either version 3, or (at your option) any later | ||
// version. | ||
|
||
// GCC is distributed in the hope that it will be useful, but WITHOUT ANY | ||
// WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
// for more details. | ||
|
||
// You should have received a copy of the GNU General Public License | ||
// along with GCC; see the file COPYING3. If not see | ||
// <http://www.gnu.org/licenses/>. | ||
#ifndef RUST_HIR_INHERENT_IMPL_ITEM_CHECK_H | ||
#define RUST_HIR_INHERENT_IMPL_ITEM_CHECK_H | ||
|
||
#include "rust-diagnostics.h" | ||
#include "rust-hir-item.h" | ||
#include "rust-hir-type-check-base.h" | ||
#include "rust-mapping-common.h" | ||
#include "rust-type-util.h" | ||
|
||
namespace Rust { | ||
namespace Resolver { | ||
|
||
class PrimitiveImplCheck : public TypeCheckBase | ||
{ | ||
public: | ||
static void go () | ||
{ | ||
PrimitiveImplCheck pass; | ||
|
||
pass.scan (); | ||
} | ||
|
||
private: | ||
void scan () | ||
|
||
{ | ||
std::vector<HIR::ImplBlock *> possible_primitive_impl; | ||
mappings.iterate_impl_blocks ([&] (HirId id, HIR::ImplBlock *impl) -> bool { | ||
// filtering trait-impl-blocks | ||
if (impl->has_trait_ref ()) | ||
return true; | ||
HirId impl_ty_id = impl->get_type ().get_mappings ().get_hirid (); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think some line breaks here will help make this easier to read maybe after those returns |
||
TyTy::BaseType *impl_type = nullptr; | ||
if (!query_type (impl_ty_id, &impl_type)) | ||
return true; | ||
DefId defid = impl->get_mappings ().get_defid (); | ||
// ignore lang item | ||
if (mappings.lookup_lang_item (defid)) | ||
return true; | ||
if (is_primitive_type_kind (impl_type->get_kind ())) | ||
{ | ||
possible_primitive_impl.push_back (impl); | ||
} | ||
Comment on lines
+57
to
+60
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is a good start, but we must also check if the implementation is a lang item. In Rust 1.49, this is the implementation for the official #[lang = "i8"]
impl i8 {
int_impl! { i8, i8, u8, 8, -128, 127, "", "", 2, "-0x7e", "0xa", "0x12", "0x12", "0x48",
"[0x12]", "[0x12]", "", "" }
} because the standard library does define methods for the Here is another primitive implementation if you want to take a look: https://github.com/rust-lang/rust/blob/e1884a8e3c3e813aada8254edfa120e85bf5ffca/library/core/src/ptr/mut_ptr.rs#L6 We can maybe address this in a later PR if this doesn't cause any regressions though There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. :), i'm not sure if all these failed regressions just because missing lang item check, so many! sorry for that! :) maybe some of them need modify , I will take the time to do some reseach about your suggestions, thx! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes some of the regressions are due to this, e.g. in gcc/testsuite/rust/compile/const-issue1440.rs where we define the primitive implementation for integers (which I assume will cause your error to trigger). but you'll need to fix the testcase as well to add the attribute |
||
return true; | ||
}); | ||
|
||
for (auto impl : possible_primitive_impl) | ||
{ | ||
report_error (impl); | ||
} | ||
} | ||
|
||
void report_error (HIR::ImplBlock *impl) | ||
{ | ||
rich_location r (line_table, impl->get_locus ()); | ||
std::string msg = "consider using an extension trait instead"; | ||
r.add_fixit_replace (impl->get_locus (), msg.c_str ()); | ||
r.add_range (impl->get_locus ()); | ||
std::string err = "impl"; | ||
err = "cannot define inherent `" + err + "` for primitive types"; | ||
rust_error_at (r, ErrorCode::E0390, "%s", err.c_str ()); | ||
} | ||
}; | ||
|
||
} // namespace Resolver | ||
} // namespace Rust | ||
|
||
#endif // RUST_HIR_INHERENT_IMPL_ITEM_CHECK_H |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ | |
#include "rust-hir-type-check.h" | ||
#include "rust-hir-full.h" | ||
#include "rust-hir-inherent-impl-overlap.h" | ||
#include "rust-hir-inherent-impl-check.h" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Duplicate includes |
||
#include "rust-hir-pattern.h" | ||
#include "rust-hir-type-check-expr.h" | ||
#include "rust-hir-type-check-item.h" | ||
|
@@ -77,6 +78,10 @@ TypeResolution::Resolve (HIR::Crate &crate) | |
if (saw_errors ()) | ||
return; | ||
|
||
PrimitiveImplCheck::go (); | ||
if (saw_errors ()) | ||
return; | ||
|
||
OverlappingImplItemPass::go (); | ||
if (saw_errors ()) | ||
return; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
#[lang = "sized"] | ||
pub trait Sized {} | ||
|
||
#[lang = "const_ptr"] | ||
impl<T> *const T { | ||
fn test(self) {} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
impl u8 { | ||
// { dg-error "cannot define inherent `impl` for primitive types" "" { target *-*-* } .-1 } | ||
pub const B: u8 = 0; | ||
} | ||
|
||
impl str { | ||
// { dg-error "cannot define inherent `impl` for primitive types" "" { target *-*-* } .-1 } | ||
fn foo() {} | ||
} | ||
|
||
impl char { | ||
// { dg-error "cannot define inherent `impl` for primitive types" "" { target *-*-* } .-1 } | ||
pub const B: u8 = 0; | ||
pub const C: u8 = 0; | ||
fn foo() {} | ||
fn bar(self) {} | ||
} | ||
|
||
struct MyType; | ||
impl &MyType { | ||
// { dg-error "cannot define inherent `impl` for primitive types" "" { target *-*-* } .-1 } | ||
pub fn for_ref(self) {} | ||
} | ||
|
||
fn main() {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#![allow(unused)] | ||
fn main() { | ||
struct Foo { | ||
x: i32 | ||
} | ||
|
||
impl *mut Foo {} | ||
// { dg-error "cannot define inherent `impl` for primitive types" "" { target *-*-* } .-1 } | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ mod core { | |
} | ||
} | ||
|
||
#[lang = "i32"] | ||
impl i32 { | ||
fn max(self, other: i32) -> i32 { | ||
if self > other { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.