From b35c9bb289e0569b747a6ffd862701f133a64e7e Mon Sep 17 00:00:00 2001 From: Margret Riegert Date: Thu, 19 Dec 2024 14:30:36 -0500 Subject: [PATCH] Attach docs to cstruct/union field members --- .../crystal/semantic/type_declaration_visitor.cr | 14 ++++++++++++-- src/compiler/crystal/syntax/parser.cr | 4 ++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/compiler/crystal/semantic/type_declaration_visitor.cr b/src/compiler/crystal/semantic/type_declaration_visitor.cr index 819816d504b6..d19902ffe4e1 100644 --- a/src/compiler/crystal/semantic/type_declaration_visitor.cr +++ b/src/compiler/crystal/semantic/type_declaration_visitor.cr @@ -206,15 +206,25 @@ class Crystal::TypeDeclarationVisitor < Crystal::SemanticVisitor if type.lookup_instance_var?(var_name) node.raise "#{type.type_desc} #{type} already defines a field named '#{field_name}'" end + ivar = MetaTypeVar.new(var_name, field_type) + ivar.doc = node.var.as(Var).doc ivar.owner = type + declare_c_struct_or_union_field(type, field_name, ivar, node.location) end def declare_c_struct_or_union_field(type, field_name, var, location) type.instance_vars[var.name] = var - type.add_def Def.new("#{field_name}=", [Arg.new("value")], Primitive.new("struct_or_union_set").at(location)) - type.add_def Def.new(field_name, body: InstanceVar.new(var.name)) + + setter = Def.new("#{field_name}=", [Arg.new("value")], Primitive.new("struct_or_union_set").at(location)).at(location) + setter.doc = var.doc + + getter = Def.new(field_name, body: InstanceVar.new(var.name)).at(location) + getter.doc = var.doc + + type.add_def setter + type.add_def getter end def declare_instance_var(node, var) diff --git a/src/compiler/crystal/syntax/parser.cr b/src/compiler/crystal/syntax/parser.cr index 0eb1f9aad022..1a416c691fcc 100644 --- a/src/compiler/crystal/syntax/parser.cr +++ b/src/compiler/crystal/syntax/parser.cr @@ -6056,8 +6056,8 @@ module Crystal skip_statement_end vars.each do |var| - exps << TypeDeclaration.new(var, type) - .at(var).at_end(type).tap(&.doc=(doc)) + var.doc = doc + exps << TypeDeclaration.new(var, type).at(var).at_end(type) end end