Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions gcc/rust/ast/rust-ast.cc
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ Attribute::as_string () const
bool
Attribute::is_derive () const
{
return has_attr_input () && get_path () == Values::Attributes::DERIVE;
return has_attr_input () && get_path () == Values::Attributes::DERIVE_ATTR;
}

/**
Expand Down Expand Up @@ -3834,6 +3834,9 @@ MetaItemLitExpr::check_cfg_predicate (const Session &) const
{
/* as far as I can tell, a literal expr can never be a valid cfg body, so
* false */
rust_error_at (this->get_locus (), "'%s' predicate key cannot be a literal",
this->as_string ().c_str ());

return false;
}

Expand Down Expand Up @@ -4179,10 +4182,19 @@ Attribute::check_cfg_predicate (const Session &session) const

auto &meta_item = static_cast<AttrInputMetaItemContainer &> (*attr_input);
if (meta_item.get_items ().empty ()
&& string_path == Values::Attributes::CFG_ATTR)
&& (string_path == Values::Attributes::CFG
|| string_path == Values::Attributes::CFG_ATTR))
{
rust_error_at (path.get_locus (), "malformed %<%s%> attribute input",
string_path.c_str ());
return false;
}

if (string_path == Values::Attributes::CFG
&& meta_item.get_items ().size () != 1)
{
rust_error_at (path.get_locus (),
"malformed %<cfg_attr%> attribute input");
rust_error_at (path.get_locus (), "multiple %qs predicates are specified",
path.as_string ().c_str ());
return false;
}
return meta_item.get_items ().front ()->check_cfg_predicate (session);
Expand Down
2 changes: 1 addition & 1 deletion gcc/rust/ast/rust-pattern.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ tokenid_to_rangekind (TokenId id)
std::string
LiteralPattern::as_string () const
{
return lit.as_string ();
return (has_minus ? "-" : "") + lit.as_string ();
}

std::string
Expand Down
22 changes: 20 additions & 2 deletions gcc/rust/ast/rust-pattern.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,42 @@ class LiteralPattern : public Pattern
Literal lit;
location_t locus;
NodeId node_id;
bool has_minus;

public:
std::string as_string () const override;

// Constructor for a literal pattern
LiteralPattern (Literal lit, location_t locus)
: lit (std::move (lit)), locus (locus),
node_id (Analysis::Mappings::get ().get_next_node_id ())
node_id (Analysis::Mappings::get ().get_next_node_id ()),
has_minus (false)
{}

LiteralPattern (Literal lit, location_t locus, bool has_minus)
: lit (std::move (lit)), locus (locus),
node_id (Analysis::Mappings::get ().get_next_node_id ()),
has_minus (has_minus)
{}

LiteralPattern (std::string val, Literal::LitType type, location_t locus,
PrimitiveCoreType type_hint)
: lit (Literal (std::move (val), type, type_hint)), locus (locus),
node_id (Analysis::Mappings::get ().get_next_node_id ())
node_id (Analysis::Mappings::get ().get_next_node_id ()),
has_minus (false)
{}

LiteralPattern (std::string val, Literal::LitType type, location_t locus,
PrimitiveCoreType type_hint, bool has_minus)
: lit (Literal (std::move (val), type, type_hint)), locus (locus),
node_id (Analysis::Mappings::get ().get_next_node_id ()),
has_minus (has_minus)
{}

location_t get_locus () const override final { return locus; }

bool get_has_minus () const { return has_minus; }

void accept_vis (ASTVisitor &vis) override;

NodeId get_node_id () const override { return node_id; }
Expand Down
11 changes: 11 additions & 0 deletions gcc/rust/backend/rust-compile-item.cc
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,17 @@ CompileItem::visit (HIR::ConstantItem &constant)
// canonical path
Resolver::CanonicalPath canonical_path
= nr_ctx.to_canonical_path (mappings.get_nodeid ());
if (constant_type->is<const TyTy::FnType> ())
{
if (concrete == nullptr)
return;

rust_assert (concrete->get_kind () == TyTy::TypeKind::FNDEF);
TyTy::FnType *concrete_fnty = static_cast<TyTy::FnType *> (concrete);

concrete_fnty->override_context ();
constant_type = expr_type = concrete_fnty->get_return_type ();
}

ctx->push_const_context ();
tree const_expr
Expand Down
28 changes: 28 additions & 0 deletions gcc/rust/backend/rust-compile-pattern.cc
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ CompilePatternCheckExpr::visit (HIR::LiteralPattern &pattern)
auto litexpr = std::make_unique<HIR::LiteralExpr> (
HIR::LiteralExpr (pattern.get_mappings (), pattern.get_literal (),
pattern.get_locus (), std::vector<AST::Attribute> ()));
if (pattern.get_has_minus ())
litexpr->set_negative ();

// Note: Floating point literals are currently accepted but will likely be
// forbidden in LiteralPatterns in a future version of Rust.
Expand Down Expand Up @@ -119,6 +121,8 @@ compile_range_pattern_bound (HIR::RangePatternBound &bound,

HIR::LiteralExpr litexpr (mappings, ref.get_literal (), locus,
std::vector<AST::Attribute> ());
if (ref.get_has_minus ())
litexpr.set_negative ();

result = CompileExpr::Compile (litexpr, ctx);
}
Expand Down Expand Up @@ -159,6 +163,30 @@ CompilePatternCheckExpr::visit (HIR::RangePattern &pattern)
pattern.get_mappings (),
pattern.get_locus (), ctx);

rust_assert (
(TREE_CODE (upper) == REAL_CST && TREE_CODE (lower) == REAL_CST)
|| (TREE_CODE (upper) == INTEGER_CST && TREE_CODE (lower) == INTEGER_CST));

bool error_E0579 = false;
if (TREE_CODE (upper) == REAL_CST)
{
REAL_VALUE_TYPE upper_r = TREE_REAL_CST (upper);
REAL_VALUE_TYPE lower_r = TREE_REAL_CST (lower);
if (real_compare (GE_EXPR, &lower_r, &upper_r))
error_E0579 = true;
}
else if (TREE_CODE (upper) == INTEGER_CST)
{
auto upper_wi = wi::to_wide (upper).to_shwi ();
auto lower_wi = wi::to_wide (lower).to_shwi ();
if (lower_wi >= upper_wi)
error_E0579 = true;
}

if (error_E0579)
rust_error_at (pattern.get_locus (), ErrorCode::E0579,
"lower range bound must be less than upper");

ComparisonOperator upper_cmp = pattern.is_inclusive_range ()
? ComparisonOperator::LESS_OR_EQUAL
: ComparisonOperator::LESS_THAN;
Expand Down
21 changes: 14 additions & 7 deletions gcc/rust/backend/rust-compile-resolve-path.cc
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,8 @@ ResolvePathRef::resolve_with_node_id (
tl::optional<HirId> hid
= ctx->get_mappings ().lookup_node_to_hir (resolved_node_id);
if (!hid.has_value ())
{
rust_error_at (expr_locus, "reverse call path lookup failure");
return error_mark_node;
}
return error_mark_node;

auto ref = hid.value ();

// might be a constant
Expand Down Expand Up @@ -189,6 +187,17 @@ ResolvePathRef::resolve_with_node_id (
}
}

// possibly a const expr value
if (lookup->get_kind () == TyTy::TypeKind::CONST)
{
auto d = lookup->destructure ();
rust_assert (d->get_kind () == TyTy::TypeKind::CONST);
auto c = d->as_const_type ();
rust_assert (c->const_kind () == TyTy::BaseConstType::ConstKind::Value);
auto val = static_cast<TyTy::ConstValueType *> (c);
return val->get_value ();
}

// Handle unit struct
tree resolved_item = error_mark_node;
if (lookup->get_kind () == TyTy::TypeKind::ADT)
Expand All @@ -203,9 +212,7 @@ ResolvePathRef::resolve_with_node_id (
resolved_item = query_compile (ref, lookup, final_segment, mappings,
expr_locus, is_qualified_path);
if (resolved_item != error_mark_node)
{
TREE_USED (resolved_item) = 1;
}
TREE_USED (resolved_item) = 1;

return resolved_item;
}
Expand Down
3 changes: 2 additions & 1 deletion gcc/rust/hir/rust-ast-lower-pattern.cc
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,8 @@ ASTLoweringPattern::visit (AST::LiteralPattern &pattern)

HIR::Literal l = lower_literal (pattern.get_literal ());
translated
= new HIR::LiteralPattern (mapping, std::move (l), pattern.get_locus ());
= new HIR::LiteralPattern (mapping, std::move (l), pattern.get_locus (),
pattern.get_has_minus ());
}

void
Expand Down
14 changes: 12 additions & 2 deletions gcc/rust/hir/tree/rust-hir-pattern.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,27 @@ class LiteralPattern : public Pattern
Literal lit;
location_t locus;
Analysis::NodeMapping mappings;
bool has_minus;

public:
std::string as_string () const override;

// Constructor for a literal pattern
LiteralPattern (Analysis::NodeMapping mappings, Literal lit, location_t locus)
: lit (std::move (lit)), locus (locus), mappings (mappings)
: lit (std::move (lit)), locus (locus), mappings (mappings),
has_minus (false)
{}

LiteralPattern (Analysis::NodeMapping mappings, Literal lit, location_t locus,
bool has_minus)
: lit (std::move (lit)), locus (locus), mappings (mappings),
has_minus (has_minus)
{}

LiteralPattern (Analysis::NodeMapping mappings, std::string val,
Literal::LitType type, location_t locus)
: lit (Literal (std::move (val), type, PrimitiveCoreType::CORETYPE_STR)),
locus (locus), mappings (mappings)
locus (locus), mappings (mappings), has_minus (false)
{}

location_t get_locus () const override { return locus; }
Expand All @@ -65,6 +73,8 @@ class LiteralPattern : public Pattern
Literal &get_literal () { return lit; }
const Literal &get_literal () const { return lit; }

bool get_has_minus () const { return has_minus; }

protected:
/* Use covariance to implement clone function as returning this object rather
* than base */
Expand Down
2 changes: 1 addition & 1 deletion gcc/rust/hir/tree/rust-hir.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2634,7 +2634,7 @@ StructPattern::as_string () const
std::string
LiteralPattern::as_string () const
{
return lit.as_string ();
return (has_minus ? "-" : "") + lit.as_string ();
}

std::string
Expand Down
9 changes: 4 additions & 5 deletions gcc/rust/metadata/rust-export-metadata.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "rust-ast-dump.h"
#include "rust-abi.h"
#include "rust-item.h"
#include "rust-macro.h"
#include "rust-object-export.h"

#include "md5.h"
Expand Down Expand Up @@ -111,14 +112,12 @@ ExportContext::emit_function (const HIR::Function &fn)
}

void
ExportContext::emit_macro (NodeId macro)
ExportContext::emit_macro (AST::MacroRulesDefinition &macro)
{
std::stringstream oss;
AST::Dump dumper (oss);

AST::Item *item = mappings.lookup_ast_item (macro).value ();

dumper.go (*item);
dumper.go (macro);

public_interface_buffer += oss.str ();
}
Expand Down Expand Up @@ -195,7 +194,7 @@ PublicInterface::gather_export_data ()
vis_item.accept_vis (visitor);
}

for (const auto &macro : mappings.get_exported_macros ())
for (auto &macro : mappings.get_exported_macros ())
context.emit_macro (macro);
}

Expand Down
2 changes: 1 addition & 1 deletion gcc/rust/metadata/rust-export-metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class ExportContext
* directly refer to them using their NodeId. There's no need to keep an HIR
* node for them.
*/
void emit_macro (NodeId macro);
void emit_macro (AST::MacroRulesDefinition &macro);

const std::string &get_interface_buffer () const;

Expand Down
45 changes: 40 additions & 5 deletions gcc/rust/parse/rust-parse-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -3262,21 +3262,42 @@ Parser<ManagedTokenSource>::parse_generic_params (EndTokenPred is_end_token)

// Did we parse a generic type param yet
auto type_seen = false;
// Did we parse a const param with a default value yet
auto const_with_default_seen = false;
// Did the user write a lifetime parameter after a type one
auto order_error = false;
// Did the user write a const param with a default value after a type one
auto const_with_default_order_error = false;

// parse lifetime params
while (!is_end_token (lexer.peek_token ()->get_id ()))
{
auto param = parse_generic_param (is_end_token);
if (param)
{
// TODO: Handle `Const` here as well if necessary
if (param->get_kind () == AST::GenericParam::Kind::Type)
type_seen = true;
{
type_seen = true;
if (const_with_default_seen)
const_with_default_order_error = true;
}
else if (param->get_kind () == AST::GenericParam::Kind::Lifetime
&& type_seen)
order_error = true;
{
order_error = true;
if (const_with_default_seen)
const_with_default_order_error = true;
}
else if (param->get_kind () == AST::GenericParam::Kind::Const)
{
type_seen = true;
AST::ConstGenericParam *const_param
= static_cast<AST::ConstGenericParam *> (param.get ());
if (const_param->has_default_value ())
const_with_default_seen = true;
else if (const_with_default_seen)
const_with_default_order_error = true;
}

generic_params.emplace_back (std::move (param));
maybe_skip_token (COMMA);
Expand All @@ -3293,6 +3314,13 @@ Parser<ManagedTokenSource>::parse_generic_params (EndTokenPred is_end_token)
"must be declared prior to type and const parameters");
add_error (std::move (error));
}
if (const_with_default_order_error)
{
Error error (generic_params.front ()->get_locus (),
"invalid order for generic parameters: generic parameters "
"with a default must be trailing");
add_error (std::move (error));
}

generic_params.shrink_to_fit ();
return generic_params;
Expand Down Expand Up @@ -8206,7 +8234,14 @@ Parser<ManagedTokenSource>::parse_while_let_loop_expr (
// parse predicate patterns
std::vector<std::unique_ptr<AST::Pattern>> predicate_patterns
= parse_match_arm_patterns (EQUAL);
// TODO: have to ensure that there is at least 1 pattern?
// ensure that there is at least 1 pattern
if (predicate_patterns.empty ())
{
Error error (lexer.peek_token ()->get_locus (),
"should be at least 1 pattern");
add_error (std::move (error));
return nullptr;
}

if (!skip_token (EQUAL))
{
Expand Down Expand Up @@ -10345,7 +10380,7 @@ Parser<ManagedTokenSource>::parse_literal_or_range_pattern ()
return std::unique_ptr<AST::LiteralPattern> (
new AST::LiteralPattern (range_lower->get_str (), type,
range_lower->get_locus (),
range_lower->get_type_hint ()));
range_lower->get_type_hint (), has_minus));
}
}

Expand Down
Loading