Skip to content

Commit 4413bc0

Browse files
committed
Fix bad inherent overlap error
When we examine HIR::ImplBlock's we determine if an impl might overlap another impl based on the Self type. So for example you might have a generic structure Foo<T>(T), and an associated impl block for Foo<i32>, but then go on to define an associated impl of Foo<T> the generic one will overlap any associated impl hiding the generic implementation. In this case we have two generic impl blocks *const [T] *const T This means the *const T might overlap with the slice one since it is generic. As bjorn3 pointed out in #1075, the correct implementation is to observe that [T] is constrained by size but untill we have the auto trait of Sized we must example the two generic impls and just determine that they are not-equal so for now this is the best implementation we can do. Fixes #1075
1 parent e43a5c5 commit 4413bc0

File tree

7 files changed

+74
-33
lines changed

7 files changed

+74
-33
lines changed

gcc/rust/typecheck/rust-hir-inherent-impl-overlap.h

+22-3
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,26 @@ class OverlappingImplItemPass : public TypeCheckBase
123123
continue;
124124

125125
if (query->can_eq (candidate, false))
126-
possible_collision (it->second, iy->second);
126+
{
127+
// we might be in the case that we have:
128+
//
129+
// *const T vs *const [T]
130+
//
131+
// so lets use an equality check when the
132+
// candidates are both generic to be sure we dont emit a false
133+
// positive
134+
135+
bool a = query->is_concrete ();
136+
bool b = candidate->is_concrete ();
137+
bool both_generic = !a && !b;
138+
if (both_generic)
139+
{
140+
if (!query->is_equal (*candidate))
141+
continue;
142+
}
143+
144+
possible_collision (it->second, iy->second);
145+
}
127146
}
128147
}
129148
}
@@ -152,8 +171,8 @@ class OverlappingImplItemPass : public TypeCheckBase
152171
void collision_detected (HIR::ImplItem *query, HIR::ImplItem *dup,
153172
const std::string &name)
154173
{
155-
RichLocation r (query->get_locus ());
156-
r.add_range (dup->get_locus ());
174+
RichLocation r (dup->get_locus ());
175+
r.add_range (query->get_locus ());
157176
rust_error_at (r, "duplicate definitions with name %s", name.c_str ());
158177
}
159178

gcc/rust/typecheck/rust-hir-path-probe.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,10 @@ class PathProbeType : public TypeCheckBase
284284
return;
285285

286286
if (!receiver->can_eq (impl_block_ty, false))
287-
return;
287+
{
288+
if (!impl_block_ty->can_eq (receiver, false))
289+
return;
290+
}
288291

289292
// lets visit the impl_item
290293
item->accept_vis (*this);

gcc/rust/typecheck/rust-tyty-bounds.cc

+4-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ TypeBoundsProbe::scan ()
4141
return true;
4242

4343
if (!receiver->can_eq (impl_type, false))
44-
return true;
44+
{
45+
if (!impl_type->can_eq (receiver, false))
46+
return true;
47+
}
4548

4649
possible_trait_paths.push_back ({impl->get_trait_ref ().get (), impl});
4750
return true;

gcc/rust/typecheck/rust-tyty-cmp.h

-26
Original file line numberDiff line numberDiff line change
@@ -886,8 +886,6 @@ class ArrayCmp : public BaseCmp
886886
ok = true;
887887
}
888888

889-
void visit (const ParamType &type) override { ok = true; }
890-
891889
private:
892890
const BaseType *get_base () const override { return base; }
893891
const ArrayType *base;
@@ -916,8 +914,6 @@ class SliceCmp : public BaseCmp
916914
ok = true;
917915
}
918916

919-
void visit (const ParamType &type) override { ok = true; }
920-
921917
private:
922918
const BaseType *get_base () const override { return base; }
923919
const SliceType *base;
@@ -939,8 +935,6 @@ class BoolCmp : public BaseCmp
939935
ok = type.get_infer_kind () == InferType::InferTypeKind::GENERAL;
940936
}
941937

942-
void visit (const ParamType &type) override { ok = true; }
943-
944938
private:
945939
const BaseType *get_base () const override { return base; }
946940
const BoolType *base;
@@ -965,8 +959,6 @@ class IntCmp : public BaseCmp
965959
ok = type.get_int_kind () == base->get_int_kind ();
966960
}
967961

968-
void visit (const ParamType &type) override { ok = true; }
969-
970962
private:
971963
const BaseType *get_base () const override { return base; }
972964
const IntType *base;
@@ -991,8 +983,6 @@ class UintCmp : public BaseCmp
991983
ok = type.get_uint_kind () == base->get_uint_kind ();
992984
}
993985

994-
void visit (const ParamType &type) override { ok = true; }
995-
996986
private:
997987
const BaseType *get_base () const override { return base; }
998988
const UintType *base;
@@ -1017,8 +1007,6 @@ class FloatCmp : public BaseCmp
10171007
ok = type.get_float_kind () == base->get_float_kind ();
10181008
}
10191009

1020-
void visit (const ParamType &type) override { ok = true; }
1021-
10221010
private:
10231011
const BaseType *get_base () const override { return base; }
10241012
const FloatType *base;
@@ -1120,8 +1108,6 @@ class TupleCmp : public BaseCmp
11201108
ok = true;
11211109
}
11221110

1123-
void visit (const ParamType &type) override { ok = true; }
1124-
11251111
private:
11261112
const BaseType *get_base () const override { return base; }
11271113
const TupleType *base;
@@ -1143,8 +1129,6 @@ class USizeCmp : public BaseCmp
11431129

11441130
void visit (const USizeType &type) override { ok = true; }
11451131

1146-
void visit (const ParamType &type) override { ok = true; }
1147-
11481132
private:
11491133
const BaseType *get_base () const override { return base; }
11501134
const USizeType *base;
@@ -1166,8 +1150,6 @@ class ISizeCmp : public BaseCmp
11661150

11671151
void visit (const ISizeType &type) override { ok = true; }
11681152

1169-
void visit (const ParamType &type) override { ok = true; }
1170-
11711153
private:
11721154
const BaseType *get_base () const override { return base; }
11731155
const ISizeType *base;
@@ -1189,8 +1171,6 @@ class CharCmp : public BaseCmp
11891171

11901172
void visit (const CharType &type) override { ok = true; }
11911173

1192-
void visit (const ParamType &type) override { ok = true; }
1193-
11941174
private:
11951175
const BaseType *get_base () const override { return base; }
11961176
const CharType *base;
@@ -1365,8 +1345,6 @@ class StrCmp : public BaseCmp
13651345

13661346
void visit (const StrType &type) override { ok = true; }
13671347

1368-
void visit (const ParamType &type) override { ok = true; }
1369-
13701348
private:
13711349
const BaseType *get_base () const override { return base; }
13721350
const StrType *base;
@@ -1383,8 +1361,6 @@ class NeverCmp : public BaseCmp
13831361

13841362
void visit (const NeverType &type) override { ok = true; }
13851363

1386-
void visit (const ParamType &type) override { ok = true; }
1387-
13881364
private:
13891365
const BaseType *get_base () const override { return base; }
13901366
const NeverType *base;
@@ -1478,8 +1454,6 @@ class DynamicCmp : public BaseCmp
14781454
ok = base->bounds_compatible (type, ref_locus, false);
14791455
}
14801456

1481-
void visit (const ParamType &type) override { ok = true; }
1482-
14831457
private:
14841458
const BaseType *get_base () const override { return base; }
14851459

gcc/testsuite/rust/compile/generics7.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ impl Foo<char> {
1515
}
1616

1717
impl<T> Foo<T> {
18-
fn bar(self) -> T { // { dg-error "duplicate definitions with name bar" }
18+
fn bar(self) -> T {
1919
self.a
2020
}
2121
}

gcc/testsuite/rust/compile/generics8.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
struct Foo<A, B>(A, B);
22

33
impl<T> Foo<i32, T> {
4-
fn test(a: T) -> T { // { dg-error "duplicate definitions with name test" }
4+
fn test(a: T) -> T {
55
a
66
}
77
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// { dg-additional-options "-w" }
2+
extern "rust-intrinsic" {
3+
pub fn offset<T>(dst: *const T, offset: isize) -> *const T;
4+
}
5+
6+
struct FatPtr<T> {
7+
data: *const T,
8+
len: usize,
9+
}
10+
11+
union Repr<T> {
12+
rust: *const [T],
13+
rust_mut: *mut [T],
14+
raw: FatPtr<T>,
15+
}
16+
17+
impl<T> *const [T] {
18+
pub const fn len(self) -> usize {
19+
// SAFETY: this is safe because `*const [T]` and `FatPtr<T>` have the same layout.
20+
// Only `std` can make this guarantee.
21+
let a = unsafe { Repr { rust: self }.raw };
22+
a.len
23+
}
24+
25+
pub const fn as_ptr(self) -> *const T {
26+
self as *const T
27+
}
28+
}
29+
30+
impl<T> *const T {
31+
pub const unsafe fn offset(self, count: isize) -> *const T {
32+
unsafe { offset(self, count) }
33+
}
34+
35+
pub const unsafe fn add(self, count: usize) -> Self {
36+
unsafe { self.offset(count as isize) }
37+
}
38+
39+
pub const fn as_ptr(self) -> *const T {
40+
self as *const T
41+
}
42+
}

0 commit comments

Comments
 (0)