Skip to content

Commit 498758a

Browse files
bors[bot]philberty
andauthored
Merge #374
374: Add basic wrapper over gcc rich_location r=philberty a=philberty Adding rich location will improve our error message diagnostics, this is an initial building block to keep a wrapper over the GCC stuff we call. Addresses: #97 Fixes: #327 Co-authored-by: Philip Herron <[email protected]>
2 parents f958d17 + 5b8de2b commit 498758a

28 files changed

+216
-83
lines changed

gcc/rust/resolve/rust-ast-resolve-implitem.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ class ResolveToplevelImplItem : public ResolverBase
4444
resolver->get_name_scope ().insert (
4545
path, constant.get_node_id (), constant.get_locus (), false,
4646
[&] (const CanonicalPath &, NodeId, Location locus) -> void {
47-
rust_error_at (constant.get_locus (), "redefined multiple times");
48-
rust_error_at (locus, "was defined here");
47+
RichLocation r (constant.get_locus ());
48+
r.add_range (locus);
49+
rust_error_at (r, "redefined multiple times");
4950
});
5051
resolver->insert_new_definition (constant.get_node_id (),
5152
Definition{constant.get_node_id (),
@@ -59,8 +60,9 @@ class ResolveToplevelImplItem : public ResolverBase
5960
resolver->get_name_scope ().insert (
6061
path, function.get_node_id (), function.get_locus (), false,
6162
[&] (const CanonicalPath &, NodeId, Location locus) -> void {
62-
rust_error_at (function.get_locus (), "redefined multiple times");
63-
rust_error_at (locus, "was defined here");
63+
RichLocation r (function.get_locus ());
64+
r.add_range (locus);
65+
rust_error_at (r, "redefined multiple times");
6466
});
6567
resolver->insert_new_definition (function.get_node_id (),
6668
Definition{function.get_node_id (),
@@ -74,8 +76,9 @@ class ResolveToplevelImplItem : public ResolverBase
7476
resolver->get_name_scope ().insert (
7577
path, method.get_node_id (), method.get_locus (), false,
7678
[&] (const CanonicalPath &, NodeId, Location locus) -> void {
77-
rust_error_at (method.get_locus (), "redefined multiple times");
78-
rust_error_at (locus, "was defined here");
79+
RichLocation r (method.get_locus ());
80+
r.add_range (locus);
81+
rust_error_at (r, "redefined multiple times");
7982
});
8083
resolver->insert_new_definition (method.get_node_id (),
8184
Definition{method.get_node_id (),

gcc/rust/resolve/rust-ast-resolve-toplevel.h

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ class ResolveTopLevel : public ResolverBase
4545
CanonicalPath (alias.get_new_type_name ()), alias.get_node_id (),
4646
alias.get_locus (), false,
4747
[&] (const CanonicalPath &, NodeId, Location locus) -> void {
48-
rust_error_at (alias.get_locus (), "redefined multiple times");
49-
rust_error_at (locus, "was defined here");
48+
RichLocation r (alias.get_locus ());
49+
r.add_range (locus);
50+
rust_error_at (r, "redefined multiple times");
5051
});
5152
}
5253

@@ -56,8 +57,9 @@ class ResolveTopLevel : public ResolverBase
5657
CanonicalPath (struct_decl.get_identifier ()), struct_decl.get_node_id (),
5758
struct_decl.get_locus (), false,
5859
[&] (const CanonicalPath &, NodeId, Location locus) -> void {
59-
rust_error_at (struct_decl.get_locus (), "redefined multiple times");
60-
rust_error_at (locus, "was defined here");
60+
RichLocation r (struct_decl.get_locus ());
61+
r.add_range (locus);
62+
rust_error_at (r, "redefined multiple times");
6163
});
6264
}
6365

@@ -67,8 +69,9 @@ class ResolveTopLevel : public ResolverBase
6769
CanonicalPath (struct_decl.get_identifier ()), struct_decl.get_node_id (),
6870
struct_decl.get_locus (), false,
6971
[&] (const CanonicalPath &, NodeId, Location locus) -> void {
70-
rust_error_at (struct_decl.get_locus (), "redefined multiple times");
71-
rust_error_at (locus, "was defined here");
72+
RichLocation r (struct_decl.get_locus ());
73+
r.add_range (locus);
74+
rust_error_at (r, "redefined multiple times");
7275
});
7376
}
7477

@@ -78,8 +81,9 @@ class ResolveTopLevel : public ResolverBase
7881
CanonicalPath (var.get_identifier ()), var.get_node_id (),
7982
var.get_locus (), false,
8083
[&] (const CanonicalPath &, NodeId, Location locus) -> void {
81-
rust_error_at (var.get_locus (), "redefined multiple times");
82-
rust_error_at (locus, "was defined here");
84+
RichLocation r (var.get_locus ());
85+
r.add_range (locus);
86+
rust_error_at (r, "redefined multiple times");
8387
});
8488
resolver->insert_new_definition (var.get_node_id (),
8589
Definition{var.get_node_id (),
@@ -94,8 +98,9 @@ class ResolveTopLevel : public ResolverBase
9498
resolver->get_name_scope ().insert (
9599
path, constant.get_node_id (), constant.get_locus (), false,
96100
[&] (const CanonicalPath &, NodeId, Location locus) -> void {
97-
rust_error_at (constant.get_locus (), "redefined multiple times");
98-
rust_error_at (locus, "was defined here");
101+
RichLocation r (constant.get_locus ());
102+
r.add_range (locus);
103+
rust_error_at (r, "redefined multiple times");
99104
});
100105
resolver->insert_new_definition (constant.get_node_id (),
101106
Definition{constant.get_node_id (),
@@ -109,8 +114,9 @@ class ResolveTopLevel : public ResolverBase
109114
resolver->get_name_scope ().insert (
110115
path, function.get_node_id (), function.get_locus (), false,
111116
[&] (const CanonicalPath &, NodeId, Location locus) -> void {
112-
rust_error_at (function.get_locus (), "redefined multiple times");
113-
rust_error_at (locus, "was defined here");
117+
RichLocation r (function.get_locus ());
118+
r.add_range (locus);
119+
rust_error_at (r, "redefined multiple times");
114120
});
115121
resolver->insert_new_definition (function.get_node_id (),
116122
Definition{function.get_node_id (),

gcc/rust/rust-diagnostics.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,17 @@ rust_inform (const Location location, const char *fmt, ...)
186186
va_end (ap);
187187
}
188188

189+
// Rich Locations
190+
void
191+
rust_error_at (const RichLocation location, const char *fmt, ...)
192+
{
193+
va_list ap;
194+
195+
va_start (ap, fmt);
196+
rust_be_error_at (location, expand_message (fmt, ap));
197+
va_end (ap);
198+
}
199+
189200
// rust_debug uses normal printf formatting, not GCC diagnostic formatting.
190201

191202
void

gcc/rust/rust-diagnostics.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
// All other format specifiers are as defined by 'sprintf'. The final resulting
4949
// message is then sent to the back end via rust_be_error_at/rust_be_warning_at.
5050

51+
// simple location
5152
extern void
5253
rust_error_at (const Location, const char *fmt, ...)
5354
RUST_ATTRIBUTE_GCC_DIAG (2, 3);
@@ -61,6 +62,11 @@ extern void
6162
rust_inform (const Location, const char *fmt, ...)
6263
RUST_ATTRIBUTE_GCC_DIAG (2, 3);
6364

65+
// rich locations
66+
extern void
67+
rust_error_at (const RichLocation, const char *fmt, ...)
68+
RUST_ATTRIBUTE_GCC_DIAG (2, 3);
69+
6470
// These interfaces provide a way for the front end to ask for
6571
// the open/close quote characters it should use when formatting
6672
// diagnostics (warnings, errors).
@@ -78,6 +84,8 @@ rust_close_quote ();
7884
extern void
7985
rust_be_error_at (const Location, const std::string &errmsg);
8086
extern void
87+
rust_be_error_at (const RichLocation, const std::string &errmsg);
88+
extern void
8189
rust_be_warning_at (const Location, int opt, const std::string &warningmsg);
8290
extern void
8391
rust_be_fatal_error (const Location, const std::string &errmsg);

gcc/rust/rust-gcc-diagnostics.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ rust_be_inform (const Location location, const std::string &infomsg)
5050
inform (gcc_loc, "%s", infomsg.c_str ());
5151
}
5252

53+
void
54+
rust_be_error_at (const RichLocation location, const std::string &errmsg)
55+
{
56+
rich_location gcc_loc = location.get ();
57+
error_at (&gcc_loc, "%s", errmsg.c_str ());
58+
}
59+
5360
void
5461
rust_be_get_quotechars (const char **open_qu, const char **close_qu)
5562
{

gcc/rust/rust-linemap.cc

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,46 @@ rust_get_linemap ()
175175
{
176176
return new Gcc_linemap;
177177
}
178+
179+
RichLocation::RichLocation (Location root)
180+
: gcc_rich_loc (line_table, root.gcc_location ())
181+
{
182+
/*rich_location (line_maps *set, location_t loc,
183+
const range_label *label = NULL);*/
184+
}
185+
186+
RichLocation::~RichLocation () {}
187+
188+
void
189+
RichLocation::add_range (Location loc)
190+
{
191+
gcc_rich_loc.add_range (loc.gcc_location ());
192+
}
193+
194+
void
195+
RichLocation::add_fixit_insert_before (const std::string &new_parent)
196+
{
197+
gcc_rich_loc.add_fixit_insert_before (new_parent.c_str ());
198+
}
199+
200+
void
201+
RichLocation::add_fixit_insert_before (Location where,
202+
const std::string &new_parent)
203+
{
204+
gcc_rich_loc.add_fixit_insert_before (where.gcc_location (),
205+
new_parent.c_str ());
206+
}
207+
208+
void
209+
RichLocation::add_fixit_insert_after (const std::string &new_parent)
210+
{
211+
gcc_rich_loc.add_fixit_insert_after (new_parent.c_str ());
212+
}
213+
214+
void
215+
RichLocation::add_fixit_insert_after (Location where,
216+
const std::string &new_parent)
217+
{
218+
gcc_rich_loc.add_fixit_insert_after (where.gcc_location (),
219+
new_parent.c_str ());
220+
}

gcc/rust/rust-location.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,26 @@ operator- (Location lhs, location_t rhs)
8080
return lhs;
8181
}
8282

83+
class RichLocation
84+
{
85+
public:
86+
RichLocation (Location root);
87+
~RichLocation ();
88+
89+
void add_range (Location loc);
90+
91+
void add_fixit_insert_before (const std::string &new_parent);
92+
93+
void add_fixit_insert_before (Location where, const std::string &new_parent);
94+
95+
void add_fixit_insert_after (const std::string &new_parent);
96+
97+
void add_fixit_insert_after (Location where, const std::string &new_parent);
98+
99+
rich_location get () const { return gcc_rich_loc; }
100+
101+
private:
102+
rich_location gcc_rich_loc;
103+
};
104+
83105
#endif // !defined(RUST_LOCATION_H)

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -189,18 +189,17 @@ class OverlappingImplItemPass : public TypeCheckBase
189189
void collision_detected (HIR::InherentImplItem *query,
190190
HIR::InherentImplItem *dup, const std::string &name)
191191
{
192-
Location qlocus;
192+
Location qlocus; // query
193193
bool ok = GetLocusFromImplItem::Resolve (query, qlocus);
194194
rust_assert (ok);
195195

196-
Location dlocus;
196+
Location dlocus; // dup
197197
ok = GetLocusFromImplItem::Resolve (dup, dlocus);
198198
rust_assert (ok);
199199

200-
// this needs GCC Rich locations see
201-
// https://github.com/Rust-GCC/gccrs/issues/97
202-
rust_error_at (qlocus, "duplicate definitions with name %s", name.c_str ());
203-
rust_error_at (dlocus, "duplicate def associated with");
200+
RichLocation r (qlocus);
201+
r.add_range (dlocus);
202+
rust_error_at (r, "duplicate definitions with name %s", name.c_str ());
204203
}
205204

206205
private:
@@ -209,9 +208,6 @@ class OverlappingImplItemPass : public TypeCheckBase
209208
std::map<TyTy::BaseType *,
210209
std::set<std::pair<HIR::InherentImplItem *, std::string> > >
211210
impl_mappings;
212-
213-
std::map<TyTy::BaseType *, std::set<TyTy::BaseType *> >
214-
possible_colliding_impls;
215211
};
216212

217213
} // namespace Resolver

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -129,31 +129,34 @@ class ReportMultipleCandidateError : private TypeCheckBase
129129
static void Report (std::vector<PathProbeCandidate> &candidates,
130130
const HIR::PathIdentSegment &query, Location query_locus)
131131
{
132-
rust_error_at (query_locus, "multiple applicable items in scope for: %s",
133-
query.as_string ().c_str ());
134-
135-
ReportMultipleCandidateError visitor;
132+
RichLocation r (query_locus);
133+
ReportMultipleCandidateError visitor (r);
136134
for (auto &c : candidates)
137135
c.impl_item->accept_vis (visitor);
136+
137+
rust_error_at (r, "multiple applicable items in scope for: %s",
138+
query.as_string ().c_str ());
138139
}
139140

140141
void visit (HIR::ConstantItem &constant) override
141142
{
142-
rust_error_at (constant.get_locus (), "possible candidate");
143+
r.add_range (constant.get_locus ());
143144
}
144145

145146
void visit (HIR::Function &function) override
146147
{
147-
rust_error_at (function.get_locus (), "possible candidate");
148+
r.add_range (function.get_locus ());
148149
}
149150

150151
void visit (HIR::Method &method) override
151152
{
152-
rust_error_at (method.get_locus (), "possible candidate");
153+
r.add_range (method.get_locus ());
153154
}
154155

155156
private:
156-
ReportMultipleCandidateError () : TypeCheckBase () {}
157+
ReportMultipleCandidateError (RichLocation &r) : TypeCheckBase (), r (r) {}
158+
159+
RichLocation &r;
157160
};
158161

159162
} // namespace Resolver

gcc/rust/typecheck/rust-hir-type-check-expr.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -290,11 +290,7 @@ class TypeCheckExpr : public TypeCheckBase
290290

291291
auto result = lhs->unify (rhs);
292292
if (result->get_kind () == TyTy::TypeKind::ERROR)
293-
{
294-
rust_error_at (expr.get_locus (),
295-
"type resolution failure in AssignmentExpr");
296-
return;
297-
}
293+
return;
298294

299295
// in the case of declare first for an ADT Type:
300296
//

gcc/rust/typecheck/rust-hir-type-check-stmt.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,7 @@ class TypeCheckStmt : public TypeCheckBase
7676
{
7777
auto unified_ty = specified_ty->unify (init_expr_ty);
7878
if (unified_ty->get_kind () == TyTy::TypeKind::ERROR)
79-
{
80-
rust_fatal_error (stmt.get_locus (),
81-
"failure in setting up let stmt type");
82-
return;
83-
}
79+
return;
8480

8581
context->insert_type (stmt.get_mappings (), unified_ty);
8682
}

0 commit comments

Comments
 (0)